Skip to content

Commit

Permalink
Added configuration of sensor status uplink interval via LoRaWAN (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-bs committed Jun 7, 2024
1 parent 7bc926e commit 1b6a635
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 161 deletions.
13 changes: 11 additions & 2 deletions BresserWeatherSensorLW.ino
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
// 20242529 Fixed payload size calculation
// 20240530 Updated to RadioLib v6.6.0
// 20240603 Added AppLayer status uplink
// 20240606 Changed appStatusUplinkInterval from const to variable
//
// ToDo:
// - Fix restoring nonces/session buffers after examples in
Expand Down Expand Up @@ -272,7 +273,13 @@ void print_wakeup_reason()
*/
void loadSecrets(uint64_t &joinEUI, uint64_t &devEUI, uint8_t *nwkKey, uint8_t *appKey)
{
if (!LittleFS.begin())

if (!LittleFS.begin(
#if defined(ESP32)
// Format the LittleFS partition on error; parameter only available for ESP32
true
#endif
))
{
log_d("Could not initialize LittleFS.");
}
Expand Down Expand Up @@ -893,6 +900,8 @@ void setup()
}

// Set appStatusUplink flag if required
uint8_t appStatusUplinkInterval = appLayer.getAppStatusUplinkInterval();
log_i("App status uplink interval: %u", appStatusUplinkInterval);
if (appStatusUplinkInterval && (fCntUp % appStatusUplinkInterval == 0))
{
appStatusUplinkPending = true;
Expand Down Expand Up @@ -1031,7 +1040,7 @@ void setup()
uint8_t *persist = node.getBufferSession();
memcpy(LWsession, persist, RADIOLIB_LORAWAN_SESSION_BUF_SIZE);

// wait until next uplink - observing legal & TTN FUP constraints
// wait until next uplink - observing legal & TTN Fair Use Policy constraints
gotoSleep(sleepDuration());
}

Expand Down
22 changes: 10 additions & 12 deletions BresserWeatherSensorLWCfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
// 20240528 Added encoding of invalid values, modified default payload, fixes
// 20240603 Added definitions for sensor status flags
// Added appStatusUplinkInterval
// 20240607 Added ARDUINO_DFROBOT_FIREBEETLE_ESP32 variant selection
// Updated HELTEC_WIFI_LORA_32_V3 definition
// Modified STATUS_INTERVAL
//
// Note:
// Depending on board package file date, either
Expand All @@ -78,17 +81,12 @@
// #define NVS_LOG

//--- Select Board ---
#if !defined(ARDUINO_TTGO_LoRa32_V1) && !defined(ARDUINO_TTGO_LoRa32_V2) && \
!defined(ARDUINO_TTGO_LoRa32_v21new) && !defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2) && \
!defined(ARDUINO_FEATHER_ESP32) && !defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) && \
!defined(ARDUINO_heltec_wifi_32_lora_V3) && !defined(ARDUINO_heltec_wifi_lora_32_V3) && \
!defined(ARDUINO_M5STACK_Core2) && !defined(ARDUINO_M5STACK_CORE2) && \
!defined(ARDUINO_ESP32S3_POWERFEATHER)
#if defined(ARDUINO_DFROBOT_FIREBEETLE_ESP32)
// Use pinning for LoRaWAN Node
#define LORAWAN_NODE
//#define LORAWAN_NODE

// Use pinning for Firebeetle Cover LoRa
//#define FIREBEETLE_ESP32_COVER_LORA
#define FIREBEETLE_ESP32_COVER_LORA
#endif

#if defined(ARDUINO_FEATHER_ESP32)
Expand Down Expand Up @@ -125,8 +123,8 @@ const uint8_t PAYLOAD_SIZE = 51;
// RTC to network time sync interval (in minutes)
#define CLOCK_SYNC_INTERVAL 24 * 60

// TODO: Modify to set this value via downlink
const uint8_t appStatusUplinkInterval = 120;
// Status message uplink interval (in frames)
#define STATUS_INTERVAL 120

// Timeout for weather sensor data reception (seconds)
#define WEATHERSENSOR_TIMEOUT 180
Expand Down Expand Up @@ -197,7 +195,7 @@ const uint8_t appStatusUplinkInterval = 120;
#pragma message("No power-saving & deep-discharge protection implemented yet.")
// On-board VB
#define PIN_ADC_IN A0
#elif defined(ARDUINO_heltec_wifi_32_lora_V3) || defined(ARDUINO_heltec_wifi_lora_32_V3)
#elif defined(ARDUINO_HELTEC_WIFI_LORA_32_V3) || defined(ARDUINO_heltec_wifi_lora_32_V3)
// On-board VB
#define PIN_ADC_IN A0
#elif defined(ARDUINO_ESP32S3_POWERFEATHER)
Expand Down Expand Up @@ -286,7 +284,7 @@ const uint8_t ADC3_SAMPLES = 10;
// Voltage divider R1 / (R1 + R2) -> V_meas = V(R1 + R2); V_adc = V(R1)
#if defined(ARDUINO_THINGPULSE_EPULSE_FEATHER)
const float UBATT_DIV = 0.6812;
#elif defined(ARDUINO_heltec_wifi_32_lora_V3) || defined(ARDUINO_heltec_wifi_lora_32_V3)
#elif defined(ARDUINO_HELTEC_WIFI_LORA_32_V3) || defined(ARDUINO_heltec_wifi_lora_32_V3)
#define ADC_CTRL 37
// R17=100k, R14=390k => 100k / (100k + 390 k)
const float UBATT_DIV = 0.2041;
Expand Down
24 changes: 24 additions & 0 deletions BresserWeatherSensorLWCmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
// 20240507 Added CMD_GET_SENSORS_CFG/CMD_SET_SENSORS_CFG
// 20240508 Updated description of CMD_GET_SENSORS_CFG/CMD_SET_SENSORS_CFG
// 20240603 Added CMD_GET_SENSORS_STAT
// 20240606 Added CMD_GET_STATUS_INTERVAL/CMD_SET_STATUS_INTERVAL
//
// ToDo:
// -
Expand Down Expand Up @@ -166,6 +167,29 @@
// Port: CMD_GET_SENSORS_STAT
#define CMD_GET_SENSORS_STAT 0xD0

// CMD_GET_STATUS_INTERVAL
// ------------------------
// Note: Get status interval in frame counts
// Port: CMD_GET_STATUS_INTERVAL
#define CMD_GET_STATUS_INTERVAL 0xD2

// Downlink (command):
// byte0: 0x00

// Uplink (response):
// byte0: status_interval[7:0]

// CMD_SET_STATUS_INTERVAL
// ------------------------
// Note: Set status interval in frame counts
// Port: CMD_SET_STATUS_INTERVAL
#define CMD_SET_STATUS_INTERVAL 0xD3

// Downlink (command):
// byte0: status_interval[7:0]

// Uplink: n.a.

// CMD_GET_SENSORS_INC
// --------------------
// Note: Get sensors include list (0...12 IDs)
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ Many software parameters can be defined at compile time, i.e. in [BresserWeather
| \<analog\> | Bitmap for enabling analog input channels; each bit position corresponds to a channel |
| \<digital\> | Bitmap for enabling digital input channels in a broader sense &mdash; GPIO, SPI, I2C, UART, ... |
| <typeN_st> | Bitmap for Bresser sensor type \<N\> battery status; each bit position corresponds to a channel |
| <status_interval> | Sensor status message uplink interval in no. of uplink frames; 0...255; 0: disabled |
| <onewire_st> | Bitmap for 1-Wire sensor status; each bit position corresponds to an index |
| <analog_st> | Bitmap for analog input status; each bit position corresponds to a channel |
| <digital_st> | Bitmap for digital input channel status |
Expand Down Expand Up @@ -445,6 +446,8 @@ Many software parameters can be defined at compile time, i.e. in [BresserWeather
| CMD_GET_WS_TIMEOUT | 0xC0 (192) | 0x00 | ws_timeout[7:0] |
| CMD_SET_WS_TIMEOUT | 0xC1 (193) | ws_timeout[7:0] | n.a. |
| CMD_RESET_RAINGAUGE | 0xC3 (195) | flags[7:0] | n.a. |
| CMD_GET_STATUS_INTERVAL | 0xD2 (210) | 0x00 | status_interval[7:0] |
| CMD_SET_STATUS_INTERVAL | 0xD3 (211) | status_interval[7:0] | n.a. |
| CMD_GET_SENSORS_STAT | 0xD0 (208) | 0x00 | type00_st[7:0]<br>type01_st[7:0]<br>...<br>type15_st[7:0]<br>onewire_st[15:8]<br>onewire_st[7:0]<br>analog_st[15:8]<br>analog_st[7:0]<br>digital_st[31:24]<br>digital_st[23:16]<br>digital_st[15:8]<br>digital_st[7:0]<br>ble_st[15:8]<br>ble_st[7:0] |
| CMD_GET_SENSORS_INC | 0xC4 (196) | 0x00 | sensors_inc0[31:24]<br>sensors_inc0[23:15]<br>sensors_inc0[16:8]<br>sensors_inc0[7:0]<br>... |
| CMD_SET_SENSORS_INC | 0xC5 (197) | sensors_inc0[31:24]<br>sensors_inc0[23:15]<br>sensors_inc0[16:8]<br>sensors_inc0[7:0]<br>... | n.a. |
Expand Down Expand Up @@ -489,6 +492,8 @@ Many software parameters can be defined at compile time, i.e. in [BresserWeather
| CMD_GET_WS_TIMEOUT | {"cmd": "CMD_GET_WS_TIMEOUT"} | {"ws_timeout": <ws_timeout>} |
| CMD_SET_WS_TIMEOUT | {"ws_timeout": <ws_timeout>} | n.a. |
| CMD_RESET_RAINGAUGE | {"reset_flags": <reset_flags>} | n.a. |
| CMD_GET_STATUS_INTERVAL | {"cmd": "CMD_GET_STATUS_INTERVAL"} | {"status_interval": <status_interval>} |
| CMD_SET_STATUS_INTERVAL | {"status_interval": <status_interval>} | n.a. |
| CMD_GET_SENSORS_STAT | {"cmd": "CMD_GET_SENSORS_STAT"} | "sensor_status": {"ble": <ble_stat>, "bresser": [<bresser0_st>, ..., <bresser15_st>]} |
| CMD_GET_SENSORS_INC | {"cmd": "CMD_GET_SENSORS_INC"} | {"sensors_inc": [<sensors_inc0>, ..., <sensors_incN>]} |
| CMD_SET_SENSORS_INC | {"sensors_inc": [<sensors_inc0>, ..., <sensors_incN>]} | n.a. |
Expand Down
11 changes: 11 additions & 0 deletions extras/customization/AppLayerMinimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
// History:
//
// 20240513 Created
// 20240607 Added getAppStatusUplinkInterval() for compatibility
//
// ToDo:
// -
Expand Down Expand Up @@ -84,6 +85,16 @@ class AppLayer
{
};

/*!
* \brief Get sensor status message uplink interval
*
* \returns status uplink interval in frame counts (o: disabled)
*/
uint8_t getAppStatusUplinkInterval(void)
{
return 0;
};

/*!
* \brief Decode app layer specific downlink messages
*
Expand Down
30 changes: 30 additions & 0 deletions scripts/downlink_formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
// port = CMD_GET_LW_CONFIG, {"cmd": "CMD_GET_LW_CONFIG"} / payload = 0x00
// port = CMD_GET_WS_TIMEOUT, {"cmd": "CMD_GET_WS_TIMEOUT" / payload = 0x00
// port = CMD_SET_WS_TIMEOUT, {"ws_timeout": <ws_timeout>}
// port = CMD_GET_STATUS_INTERVAL, {"cmd": "CMD_GET_STATUS_INTERVAL"} / payload = 0x00
// port = CMD_SET_STATUS_INTERVAL, {"status_interval": <status_interval>}
// port = CMD_GET_SENSORS_STAT, {"cmd": "CMD_GET_SENSORS_STAT"} / payload = 0x00
// port = CMD_GET_SENSORS_INC, {"cmd": "CMD_GET_SENSORS_INC"} / payload = 0x00
// port = CMD_SET_SENSORS_INC, {"sensors_inc": [<sensors_inc0>, ..., <sensors_incN>]}
Expand All @@ -43,6 +45,8 @@
//
// CMD_GET_WS_TIMEOUT {"ws_timeout": <ws_timeout>}
//
// CMD_GET_STATUS_INTERVAL {"status_interval": <status_interval>}
//
// CMD_GET_SENSORS_STAT {"sensor_status": {bresser: [<bresser_stat0>, ..., <bresser_stat15>], "ble_stat": <ble_stat>}}
//
// CMD_GET_SENSORS_INC {"sensors_inc": [<sensors_inc0>, ...]}
Expand All @@ -63,6 +67,7 @@
// <epoch> : unix epoch time, see https://www.epochconverter.com/ (<integer> / "0x....")
// <reset_flags> : 0...15 (1: hourly / 2: daily / 4: weekly / 8: monthly) / "0x0"..."0xF"
// <rtc_source> : 0x00: GPS / 0x01: RTC / 0x02: LORA / 0x03: unsynched / 0x04: set (source unknown)
// <status_interval> : Sensor status message uplink interval in no. of frames (0...255, 0: disabled)
// <sensors_incN> : e.g. "0xDEADBEEF"
// <sensors_excN> : e.g. "0xDEADBEEF"
// <max_sensors> : max. number of Bresser sensors per receive cycle; 1...8
Expand Down Expand Up @@ -119,6 +124,7 @@
// 20240519 Added CMD_GET_APP_PAYLOAD_CFG/CMD_SET_APP_PAYLOAD_CFG
// 20240530 Added decoding of CMD_GET_APP_PAYLOAD_CFG/CMD_SET_APP_PAYLOAD_CFG
// 20240603 Added CMD_GET_SENSORS_STAT
// 20240607 Added CMD_GET_STATUS_INTERVAL/CMD_SET_STATUS_INTERVAL
//
// ToDo:
// -
Expand All @@ -134,6 +140,8 @@ const CMD_GET_LW_CONFIG = 0xB1;
const CMD_GET_WS_TIMEOUT = 0xC0;
const CMD_SET_WS_TIMEOUT = 0xC1;
const CMD_RESET_RAINGAUGE = 0xC3;
const CMD_GET_STATUS_INTERVAL = 0xD2;
const CMD_SET_STATUS_INTERVAL = 0xD3;
const CMD_GET_SENSORS_STAT = 0xD0;
const CMD_GET_SENSORS_INC = 0xC4;
const CMD_SET_SENSORS_INC = 0xC5;
Expand Down Expand Up @@ -270,6 +278,14 @@ function encodeDownlink(input) {
errors: []
};
}
else if (input.data.cmd == "CMD_GET_STATUS_INTERVAL") {
return {
bytes: [0],
fPort: CMD_GET_STATUS_INTERVAL,
warnings: [],
errors: []
};
}
else if (input.data.cmd == "CMD_GET_SENSORS_STAT") {
return {
bytes: [0],
Expand Down Expand Up @@ -401,6 +417,13 @@ function encodeDownlink(input) {
warnings: [],
errors: []
};
} else if (input.data.hasOwnProperty('status_interval')) {
return {
bytes: [input.data.status_interval],
fPort: CMD_SET_STATUS_INTERVAL,
warnings: [],
errors: []
};
} else if (input.data.hasOwnProperty('sensors_inc')) {
k = 0;
for (i = 0; i < input.data.sensors_inc.length; i++) {
Expand Down Expand Up @@ -537,6 +560,7 @@ function decodeDownlink(input) {
case CMD_GET_DATETIME:
case CMD_GET_LW_CONFIG:
case CMD_GET_WS_TIMEOUT:
case CMD_GET_STATUS_INTERVAL:
case CMD_GET_SENSORS_STAT:
case CMD_GET_SENSORS_INC:
case CMD_GET_SENSORS_EXC:
Expand Down Expand Up @@ -579,6 +603,12 @@ function decodeDownlink(input) {
reset_flags: "0x" + uint8(input.bytes).toString(16)
}
};
case CMD_SET_STATUS_INTERVAL:
return {
data: {
status_interval: uint8(input.bytes)
}
};
case CMD_SET_SENSORS_INC:
return {
data: {
Expand Down
Loading

0 comments on commit 1b6a635

Please sign in to comment.