Skip to content

Commit

Permalink
Added sensor battery status flags for compatibility to BresserWeather…
Browse files Browse the repository at this point in the history
…SensorTTN (#50)
  • Loading branch information
matthias-bs committed Jun 3, 2024
1 parent d82e819 commit 09cbdbf
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 23 deletions.
13 changes: 12 additions & 1 deletion BresserWeatherSensorLWCfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
// 20240524 Added sensor feature flags
// Moved PAYLOAD_SIZE from BresserWeatherSensorLW.ino
// 20240528 Added encoding of invalid values, modified default payload, fixes
// 20240603 Added definitions for sensor status flags
//
// Note:
// Depending on board package file date, either
Expand Down Expand Up @@ -300,6 +301,9 @@ const uint8_t UBATT_SAMPLES = 10;
// BLE scan mode (0: passive / 1: active)
#define BLE_SCAN_MODE 1

// BLE battery o.k. threshold in percent
#define BLE_BATT_OK 5

// List of known sensors' BLE addresses
#define KNOWN_BLE_ADDRESSES \
{ \
Expand All @@ -310,6 +314,8 @@ const uint8_t UBATT_SAMPLES = 10;
/// AppLayer payload configuration size in bytes
#define APP_PAYLOAD_CFG_SIZE 24

#define APP_STATUS_SIZE 26

// --- Default AppLayer payload configuration ---

// For each sensor/interface type, there is a set of flags.
Expand All @@ -335,7 +341,9 @@ const uint8_t UBATT_SAMPLES = 10;
// -- 868 MHz Sensor Types --
// 0 - Weather Station; 1 Ch
// Note: Included in APP_PAYLOAD_CFG_TYPE01
#define APP_PAYLOAD_CFG_TYPE00 0x00

// Flag: Enable battery_ok flags (to be removed)
#define APP_PAYLOAD_CFG_TYPE00 0x01

// 1 - Weather Station; 1 Ch
// - Professional Wind Gauge (with T and H); 1 Ch
Expand Down Expand Up @@ -432,6 +440,9 @@ const uint8_t UBATT_SAMPLES = 10;
#define APP_PAYLOAD_OFFS_DIGITAL 20
#define APP_PAYLOAD_BYTES_DIGITAL 4

#define APP_PAYLOAD_OFFS_BLE 24
#define APP_PAYLOAD_BYTES_BLE 2

// Encoding of invalid values
// for floating point, see
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN
Expand Down
11 changes: 5 additions & 6 deletions scripts/uplink_formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
// 20240531 Fixed handling of arrays in decoder()
// 20240601 Change lightning event to provide timestamp and time
// Added compatibility mode: "status" as in BresserweatherSensorTTN
// 20240603 Added sensor battery status flags (compatibility mode)
//
// ToDo:
// -
Expand Down Expand Up @@ -441,9 +442,7 @@ function decoder(bytes, port) {
var offset = 0;
if (COMPATIBILITY_MODE) {
var ws_dec_ok = true;
var ws_batt_ok = true;
var s1_dec_ok = true;
var s1_batt_ok = true;
var ble_ok = true;
}
var decodedValues = mask
Expand Down Expand Up @@ -476,11 +475,9 @@ function decoder(bytes, port) {
return prev;
}, {});
if (COMPATIBILITY_MODE) {
decodedValues.status = {}; // Create a status object in the decoded values
//decodedValues.status = {}; // Create a status object in the decoded values
decodedValues.status.ws_dec_ok = ws_dec_ok;
decodedValues.status.ws_batt_ok = ws_batt_ok;
decodedValues.status.s1_dec_ok = s1_dec_ok;
decodedValues.status.s1_batt_ok = s1_batt_ok;
decodedValues.status.ble_ok = ble_ok;
}
return decodedValues;
Expand Down Expand Up @@ -529,7 +526,8 @@ function decoder(bytes, port) {
temperature,
uint16,
temperature,
uint8
uint8,
bitmap_sensors
],
[
'ws_temp_c',
Expand All @@ -548,6 +546,7 @@ function decoder(bytes, port) {
'a0_voltage_mv',
'ble0_temp_c',
'ble0_humidity',
'status'
]
);
//return {...res, ...sensorStatus};
Expand Down
20 changes: 18 additions & 2 deletions src/AppLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
// 20240529 Changed encoding of INV_TEMP for BLE sensors
// 20240530 Fixed CMD_SET_APP_PAYLOAD_CFG handling
// 20240531 Moved BLE specific code to PayloadBLE.cpp
// 20240603 Added encoding of sensor battery status flags
//
// ToDo:
// -
Expand Down Expand Up @@ -94,7 +95,7 @@ void AppLayer::getPayloadStage1(uint8_t port, LoraEncoder &encoder)
// (ws > -1) ? weatherSensor.sensor[ws].valid : false,
// (ws > -1) ? weatherSensor.sensor[ws].battery_ok : false);

encodeBresser(appPayloadCfg, encoder);
encodeBresser(appPayloadCfg, appStatus, encoder);

#ifdef ONEWIRE_EN
encodeOneWire(appPayloadCfg, encoder);
Expand All @@ -108,9 +109,24 @@ void AppLayer::getPayloadStage1(uint8_t port, LoraEncoder &encoder)

#if defined(MITHERMOMETER_EN) || defined(THEENGSDECODER_EN)
// BLE Temperature/Humidity Sensors
encodeBLE(appPayloadCfg, encoder);
encodeBLE(appPayloadCfg, appStatus, encoder);
#endif

// FIXME: To be removed later
// Battery status flags for compatibility with BresserWeatherSensorTTN
if ((appPayloadCfg[0] & 1) && (encoder.getLength() <= PAYLOAD_SIZE - 1)) {
log_i("Battery status flags: ws=%u, soil=%u, lgt=%u", appStatus[SENSOR_TYPE_WEATHER1] & 1,
(appStatus[SENSOR_TYPE_SOIL] & 2) >> 1, appStatus[SENSOR_TYPE_LIGHTNING] & 1);
encoder.writeBitmap(0,
0,
0,
(appStatus[SENSOR_TYPE_LIGHTNING] & 1) ? true : false,
0,
(appStatus[SENSOR_TYPE_SOIL] & 2) ? true : false,
0,
(appStatus[SENSOR_TYPE_WEATHER1] & 1) ? true : false
);
}
}

void AppLayer::getPayloadStage2(uint8_t port, LoraEncoder &encoder)
Expand Down
9 changes: 8 additions & 1 deletion src/AppLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
// Moved code to PayloadBresser, PayloadAnalog & PayloadDigital
// 20240530 Removed BleSensors as base class & from initializers
// 20240531 Moved BLE specific code to PayloadBLE.h
// 20240603 Added appStatus[]
//
// ToDo:
// -
Expand All @@ -66,7 +67,6 @@
#include "PayloadBLE.h"
#include <LoraMessage.h>


/// Default AppLayer payload configuration
const uint8_t appPayloadCfgDef[APP_PAYLOAD_CFG_SIZE] = {
APP_PAYLOAD_CFG_TYPE00,
Expand Down Expand Up @@ -120,6 +120,9 @@ class AppLayer : public PayloadBresser, PayloadAnalog, PayloadDigital
/// AppLayer payload configuration
uint8_t appPayloadCfg[APP_PAYLOAD_CFG_SIZE];

/// AppLayer status bits
uint8_t appStatus[APP_STATUS_SIZE];

public:
/*!
* \brief Constructor with BLE sensors
Expand All @@ -139,6 +142,10 @@ class AppLayer : public PayloadBresser, PayloadAnalog, PayloadDigital
{
_rtc = rtc;
_rtcLastClockSync = clocksync;
for (int i = 0; i < APP_STATUS_SIZE; i++)
{
appStatus[i] = 0;
}
};

/*!
Expand Down
10 changes: 7 additions & 3 deletions src/PayloadBLE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
// History:
//
// 20240531 Moved from AppLayer.cpp
// 20240603 Added BLE sensor battery status
//
// ToDo:
// -
Expand Down Expand Up @@ -97,7 +98,6 @@ std::vector<std::string> PayloadBLE::getBleAddr(void)
return bleAddr;
}


/*
* Initialize list of known BLE addresses from defaults or Preferences
*/
Expand Down Expand Up @@ -127,9 +127,9 @@ void PayloadBLE::bleAddrInit(void)
/*
* Encode BLE temperature/humidity sensor values for LoRaWAN transmission
*/
void PayloadBLE::encodeBLE(uint8_t *appPayloadCfg, LoraEncoder &encoder)
void PayloadBLE::encodeBLE(uint8_t *appPayloadCfg, uint8_t * appStatus, LoraEncoder &encoder)
{
#if defined(MITHERMOMETER_EN) || defined(THEENGSDECODER_EN)
#if defined(MITHERMOMETER_EN) || defined(THEENGSDECODER_EN)
float indoor_temp_c;
float indoor_humidity;

Expand Down Expand Up @@ -163,6 +163,10 @@ void PayloadBLE::encodeBLE(uint8_t *appPayloadCfg, LoraEncoder &encoder)
log_i("Indoor Humidity: %3.1f %%", bleSensors.data[0].humidity / div);
encoder.writeTemperature(indoor_temp_c);
encoder.writeUint8(static_cast<uint8_t>(indoor_humidity + 0.5));
if (bleSensors.data[0].batt_level > BLE_BATT_OK)
{
appStatus[APP_PAYLOAD_OFFS_BLE + APP_PAYLOAD_BYTES_BLE - 1] |= 1;
}
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion src/PayloadBLE.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
// History:
//
// 20240531 Moved from AppLayer.h
// 20240603 encodeBLE(): added appStatus parameter
//
// ToDo:
// -
Expand Down Expand Up @@ -134,7 +135,7 @@ class PayloadBLE
* \param appPayloadCfg LoRaWAN payload configuration bitmaps
* \param encoder LoRaWAN payload encoder object
*/
void encodeBLE(uint8_t *appPayloadCfg, LoraEncoder &encoder);
void encodeBLE(uint8_t *appPayloadCfg, uint8_t * appStatus, LoraEncoder &encoder);
};
#endif // defined(MITHERMOMETER_EN) || defined(THEENGSDECODER_EN)
#endif //_PAYLOAD_BLE
22 changes: 14 additions & 8 deletions src/PayloadBresser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
// 20240529 Changed encoding of INV_TEMP
// 20240530 Weather sensor: Fixed encoding of invalid temperature
// 20240601 Added mapping of invalid RainGauge values to INV_FLOAT
// 20240603 Added encoding of sensor battery status
//
// ToDo:
// - Add handling of Professional Rain Gauge
Expand All @@ -64,13 +65,11 @@ void PayloadBresser::begin(void)
log_i("Receiving Weather Sensor Data %s", decode_ok ? "o.k." : "failed");
}

void PayloadBresser::encodeBresser(uint8_t *appPayloadCfg, LoraEncoder &encoder)
void PayloadBresser::encodeBresser(uint8_t *appPayloadCfg, uint8_t *appStatus, LoraEncoder &encoder)
{
// Handle weather sensors first
// SENSOR_TYPE_WEATHER0 and WEATHER_TYPE_WEATHER1 are treated as one.
// Those sensors only have one channel (0).

uint8_t flags = appPayloadCfg[0] | appPayloadCfg[1];
// Handle weather sensors - which only have one channel (0) - first.
// Configuration for SENSOR_TYPE_WEATHER0 is integrated into SENSOR_TYPE_WEATHER1.
uint8_t flags = appPayloadCfg[1];
if (flags & 1)
{
// Try to find SENSOR_TYPE_WEATHER1
Expand Down Expand Up @@ -102,6 +101,10 @@ void PayloadBresser::encodeBresser(uint8_t *appPayloadCfg, LoraEncoder &encoder)
}
}
#endif
if ((idx > -1) && weatherSensor.sensor[idx].battery_ok)
{
appStatus[1] |= 1;
}
encodeWeatherSensor(idx, flags, encoder);
}

Expand All @@ -116,7 +119,9 @@ void PayloadBresser::encodeBresser(uint8_t *appPayloadCfg, LoraEncoder &encoder)
if (type == SENSOR_TYPE_LIGHTNING)
{
int idx = weatherSensor.findType(type, 0);
#ifdef LIGHTNINGSENSOR_EN
if ((idx > -1) && weatherSensor.sensor[idx].battery_ok) {
appStatus[type] |= 1;
}

// Check if time is valid
if (*_rtcLastClockSync > 0)
Expand All @@ -134,7 +139,6 @@ void PayloadBresser::encodeBresser(uint8_t *appPayloadCfg, LoraEncoder &encoder)
weatherSensor.sensor[idx].startup);
}
}
#endif

encodeLightningSensor(idx, appPayloadCfg[type], encoder);
continue;
Expand All @@ -155,6 +159,8 @@ void PayloadBresser::encodeBresser(uint8_t *appPayloadCfg, LoraEncoder &encoder)
int idx = weatherSensor.findType(type, ch);
if (idx == -1) {
log_i("-- Failure");
} else if (weatherSensor.sensor[idx].battery_ok){
appStatus[type] |= (1 << ch);
}

if (type == SENSOR_TYPE_THERMO_HYGRO)
Expand Down
3 changes: 2 additions & 1 deletion src/PayloadBresser.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
// Added isSpaceLeft(), payloadSize[] & sensorTypes[]
// 20240528 Moved encoding of invalid values to BresserWeatherSensorLWCmd.h
// 20240530 Added missing entries in sensorTypes[]
// 20240603 encodeBresser(): added appStatus parameter
//
// ToDo:
// -
Expand Down Expand Up @@ -153,7 +154,7 @@ class PayloadBresser
* \param appPayloadCfg LoRaWAN payload configuration bitmaps
* \param encoder LoRaWAN payload encoder object
*/
void encodeBresser(uint8_t *appPayloadCfg, LoraEncoder &encoder);
void encodeBresser(uint8_t *appPayloadCfg, uint8_t *appStatus, LoraEncoder &encoder);

private:
void encodeWeatherSensor(int idx, uint8_t flags, LoraEncoder &encoder);
Expand Down

0 comments on commit 09cbdbf

Please sign in to comment.