Skip to content

Commit

Permalink
Added uplink of sensor status flags via downlink command CMD_GET_SENS…
Browse files Browse the repository at this point in the history
…ORS_STAT and at fixed interval (#51)
  • Loading branch information
matthias-bs authored Jun 3, 2024
1 parent d70e3d2 commit 471937f
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 2 deletions.
19 changes: 19 additions & 0 deletions BresserWeatherSensorLW.ino
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
// 20240528 Disabled uplink transmission of LoRaWAN node status flags
// 20242529 Fixed payload size calculation
// 20240530 Updated to RadioLib v6.6.0
// 20240603 Added AppLayer status uplink
//
// ToDo:
// - Fix restoring nonces/session buffers after examples in
Expand Down Expand Up @@ -204,6 +205,7 @@ RTC_DATA_ATTR time_t rtcLastClockSync = 0; //!< timestamp of last RTC synchoniza
RTC_DATA_ATTR uint16_t bootCount = 1;
RTC_DATA_ATTR uint16_t bootCountSinceUnsuccessfulJoin = 0;
RTC_DATA_ATTR E_TIME_SOURCE rtcTimeSource;
RTC_DATA_ATTR bool appStatusUplinkPending = false;
RTC_DATA_ATTR uint8_t LWsession[RADIOLIB_LORAWAN_SESSION_BUF_SIZE];

#else
Expand All @@ -221,6 +223,9 @@ uint8_t LWsession[RADIOLIB_LORAWAN_SESSION_BUF_SIZE] __attribute__((section(".un

/// RTC time source
E_TIME_SOURCE rtcTimeSource __attribute__((section(".uninitialized_data")));

/// AppLayer status uplink pending
bool appStatusUplinkPending __attribute__((section(".uninitialized_data")));
#endif

/// Real time clock
Expand Down Expand Up @@ -723,6 +728,7 @@ void setup()
if (bootCount == 1)
{
rtcTimeSource = E_TIME_SOURCE::E_UNSYNCHED;
appStatusUplinkPending = 0;
}

// Set time zone
Expand Down Expand Up @@ -886,6 +892,12 @@ void setup()
node.sendMacCommandReq(RADIOLIB_LORAWAN_MAC_LINK_CHECK);
}

// Set appStatusUplink flag if required
if (appStatusUplinkInterval && (fCntUp % appStatusUplinkInterval == 0))
{
appStatusUplinkPending = true;
}

// ----- and now for the main event -----
log_i("Sending uplink");

Expand Down Expand Up @@ -1001,9 +1013,16 @@ void setup()
log_d("[LoRaWAN] LinkCheck count:\t%u", gwCnt);
}

if (appStatusUplinkPending) {
log_i("AppLayer status uplink pending");
}

if (uplinkReq)
{
sendCfgUplink(uplinkReq);
} else if (appStatusUplinkPending) {
sendCfgUplink(CMD_GET_SENSORS_STAT);
appStatusUplinkPending = false;
}

log_d("FcntUp: %u", node.getFCntUp());
Expand Down
4 changes: 4 additions & 0 deletions BresserWeatherSensorLWCfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
// Moved PAYLOAD_SIZE from BresserWeatherSensorLW.ino
// 20240528 Added encoding of invalid values, modified default payload, fixes
// 20240603 Added definitions for sensor status flags
// Added appStatusUplinkInterval
//
// Note:
// Depending on board package file date, either
Expand Down Expand Up @@ -124,6 +125,9 @@ 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;

// Timeout for weather sensor data reception (seconds)
#define WEATHERSENSOR_TIMEOUT 180

Expand Down
5 changes: 5 additions & 0 deletions BresserWeatherSensorLWCmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
// 20240504 Extracted from BresserWeatherSensorLWCfg.h
// 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
//
// ToDo:
// -
Expand Down Expand Up @@ -160,6 +161,10 @@
// Reset Lightning???
// -------------------

// CMD_GET_SENSORS_STAT
// ---------------------
// Port: CMD_GET_SENSORS_STAT
#define CMD_GET_SENSORS_STAT 0xD0

// CMD_GET_SENSORS_INC
// --------------------
Expand Down
14 changes: 14 additions & 0 deletions scripts/downlink_formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// 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_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>]}
// port = CMD_GET_SENSORS_EXC, {"cmd": "CMD_GET_SENSORS_EXC"} / payload = 0x00
Expand All @@ -42,6 +43,8 @@
//
// CMD_GET_WS_TIMEOUT {"ws_timeout": <ws_timeout>}
//
// CMD_GET_SENSORS_STAT {"sensor_status": {bresser: [<bresser_stat0>, ..., <bresser_stat15>], "ble_stat": <ble_stat>}}
//
// CMD_GET_SENSORS_INC {"sensors_inc": [<sensors_inc0>, ...]}
//
// CMD_GET_SENSORS_EXC {"sensors_exc"}: [<sensors_exc0>, ...]}
Expand Down Expand Up @@ -115,6 +118,7 @@
// Added en_decoders to CMD_GET_SENSORS_CFG/CMD_SET_SENSORS_CFG
// 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
//
// ToDo:
// -
Expand All @@ -130,6 +134,7 @@ 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_SENSORS_STAT = 0xD0;
const CMD_GET_SENSORS_INC = 0xC4;
const CMD_SET_SENSORS_INC = 0xC5;
const CMD_GET_SENSORS_EXC = 0xC6;
Expand Down Expand Up @@ -265,6 +270,14 @@ function encodeDownlink(input) {
errors: []
};
}
else if (input.data.cmd == "CMD_GET_SENSORS_STAT") {
return {
bytes: [0],
fPort: CMD_GET_SENSORS_STAT,
warnings: [],
errors: []
};
}
else if (input.data.cmd == "CMD_GET_SENSORS_INC") {
return {
bytes: [0],
Expand Down Expand Up @@ -524,6 +537,7 @@ function decodeDownlink(input) {
case CMD_GET_DATETIME:
case CMD_GET_LW_CONFIG:
case CMD_GET_WS_TIMEOUT:
case CMD_GET_SENSORS_STAT:
case CMD_GET_SENSORS_INC:
case CMD_GET_SENSORS_EXC:
case CMD_GET_SENSORS_CFG:
Expand Down
43 changes: 41 additions & 2 deletions scripts/uplink_formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// 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_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>]}
// port = CMD_GET_SENSORS_EXC, {"cmd": "CMD_GET_SENSORS_EXC"} / payload = 0x00
Expand All @@ -41,6 +42,8 @@
//
// CMD_GET_WS_TIMEOUT {"ws_timeout": <ws_timeout>}
//
// CMD_GET_SENSORS_STAT {"sensor_status": {bresser: [<bresser_stat0>, ..., <bresser_stat15>], "ble_stat": <ble_stat>}}
//
// CMD_GET_SENSORS_INC {"sensors_inc": [<sensors_inc0>, ...]}
//
// CMD_GET_SENSORS_EXC {"sensors_exc"}: [<sensors_exc0>, ...]}
Expand Down Expand Up @@ -121,6 +124,7 @@
// 20240601 Change lightning event to provide timestamp and time
// Added compatibility mode: "status" as in BresserweatherSensorTTN
// 20240603 Added sensor battery status flags (compatibility mode)
// Added command Added CMD_GET_SENSORS_STAT and sensor status decoder
//
// ToDo:
// -
Expand All @@ -145,6 +149,7 @@ function decoder(bytes, port) {
const CMD_GET_BLE_ADDR = 0xC8;
const CMD_GET_BLE_CONFIG = 0xCA;
const CMD_GET_APP_PAYLOAD_CFG = 0xCE;
const CMD_GET_SENSORS_STAT = 0xD0;

const rtc_source_code = {
0x00: "GPS",
Expand Down Expand Up @@ -418,6 +423,20 @@ function decoder(bytes, port) {
};
bitmap_sensors.BYTES = 1;

var sensor_status = function (bytes) {
if (bytes.length !== sensor_status.BYTES) {
throw new Error('Sensor status must have exactly 26 bytes');
}
let res = {};
res.bresser = [];
for (var i = 0; i < 16; i++) {
res.bresser[i] = "0x" + byte2hex(bytes[i]);
}
res.ble = "0x" + byte2hex(bytes[24]) + byte2hex(bytes[25]);
return res;
};
sensor_status.BYTES = 26;

//sensorStatus = {"status": {"ws_dec_ok": true}};
/**
* Decodes the given bytes using the provided mask and names.
Expand All @@ -428,7 +447,7 @@ function decoder(bytes, port) {
* @returns {Object} - The decoded values as an object.
* @throws {Error} - If the length of the bytes is less than the mask length.
*/
var decode = function (bytes, mask, names) {
var decode = function (port, bytes, mask, names) {

// Sum of all mask bytes
var maskLength = mask.reduce(function (prev, cur) {
Expand Down Expand Up @@ -474,7 +493,7 @@ function decoder(bytes, port) {
}
return prev;
}, {});
if (COMPATIBILITY_MODE) {
if ((port == 1) && COMPATIBILITY_MODE) {
//decodedValues.status = {}; // Create a status object in the decoded values
decodedValues.status.ws_dec_ok = ws_dec_ok;
decodedValues.status.s1_dec_ok = s1_dec_ok;
Expand All @@ -498,6 +517,7 @@ function decoder(bytes, port) {
latLng: latLng,
bitmap_node: bitmap_node,
bitmap_sensors: bitmap_sensors,
sensor_status: sensor_status,
rawfloat: rawfloat,
uint8fp1: uint8fp1,
uint16fp1: uint16fp1,
Expand All @@ -509,6 +529,7 @@ function decoder(bytes, port) {

if (port === 1) {
return decode(
port,
bytes,
[
temperature,
Expand Down Expand Up @@ -553,6 +574,7 @@ function decoder(bytes, port) {

} else if (port === CMD_GET_DATETIME) {
return decode(
port,
bytes,
[uint32BE, rtc_source
],
Expand All @@ -561,6 +583,7 @@ function decoder(bytes, port) {
);
} else if (port === CMD_GET_LW_CONFIG) {
return decode(
port,
bytes,
[uint16BE, uint16BE
],
Expand All @@ -569,6 +592,7 @@ function decoder(bytes, port) {
);
} else if (port === CMD_GET_WS_TIMEOUT) {
return decode(
port,
bytes,
[uint8
],
Expand All @@ -577,6 +601,7 @@ function decoder(bytes, port) {
);
} else if (port === CMD_GET_SENSORS_INC) {
return decode(
port,
bytes,
[id32
],
Expand All @@ -585,6 +610,7 @@ function decoder(bytes, port) {
);
} else if (port === CMD_GET_SENSORS_EXC) {
return decode(
port,
bytes,
[id32
],
Expand All @@ -594,13 +620,15 @@ function decoder(bytes, port) {
} else if (port === CMD_GET_SENSORS_CFG) {
return decode(
bytes,
port,
[uint8, uint8, uint8
],
['max_sensors', 'rx_flags', 'en_decoders'
]
);
} else if (port === CMD_GET_BLE_ADDR) {
return decode(
port,
bytes,
[mac48
],
Expand All @@ -609,18 +637,29 @@ function decoder(bytes, port) {
);
} else if (port === CMD_GET_BLE_CONFIG) {
return decode(
port,
bytes,
[uint8, uint8
],
['ble_active', 'ble_scantime']
);
} else if (port === CMD_GET_APP_PAYLOAD_CFG) {
return decode(
port,
bytes,
[bresser_bitmaps, hex16, hex16, hex32
],
['bresser', 'onewire', 'analog', 'digital']
);
} else if (port === CMD_GET_SENSORS_STAT) {
return decode(
port,
bytes,
[sensor_status
],
['sensor_status'
]
);
}

}
Expand Down
16 changes: 16 additions & 0 deletions src/AppLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
// 20240530 Fixed CMD_SET_APP_PAYLOAD_CFG handling
// 20240531 Moved BLE specific code to PayloadBLE.cpp
// 20240603 Added encoding of sensor battery status flags
// Added CMD_GET_SENSORS_STAT
//
// ToDo:
// -
Expand Down Expand Up @@ -165,6 +166,12 @@ AppLayer::decodeDownlink(uint8_t port, uint8_t *payload, size_t size)
return 0;
}

if ((port == CMD_GET_SENSORS_STAT) && (payload[0] == 0x00) && (size == 1))
{
log_d("Get sensors' status");
return CMD_GET_SENSORS_STAT;
}

if ((port == CMD_GET_SENSORS_INC) && (payload[0] == 0x00) && (size == 1))
{
log_d("Get sensors include list");
Expand Down Expand Up @@ -310,6 +317,15 @@ void AppLayer::getConfigPayload(uint8_t cmd, uint8_t &port, LoraEncoder &encoder
port = CMD_GET_BLE_CONFIG;
}
#endif
else if (cmd == CMD_GET_SENSORS_STAT)
{
for (size_t i = 0; i < APP_STATUS_SIZE; i++)
{
encoder.writeUint8(appStatus[i]);
}
port = CMD_GET_SENSORS_STAT;
}

else if (cmd == CMD_GET_SENSORS_INC)
{
uint8_t payload[48];
Expand Down

0 comments on commit 471937f

Please sign in to comment.