Skip to content

Commit

Permalink
Fix compatibility mode (#77)
Browse files Browse the repository at this point in the history
* Fixed/improved compatibility mode

* Moved MAX_DOWNLINK_SIZE from config.h to BresserWeatherSensorLWCfg.h, changed to 51
  • Loading branch information
matthias-bs authored Jul 4, 2024
1 parent c6a952f commit 580f75a
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 53 deletions.
11 changes: 8 additions & 3 deletions BresserWeatherSensorLWCfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@
// Modified STATUS_INTERVAL
// 20240608 Added MAX_NUM_868MHZ_SENSORS
// 20240613 Removed workaround for ARDUINO_THINGPULSE_EPULSE_FEATHER
// 20240704 Moved MAX_DOWNLINK_SIZE from config.h, changed to 51
//
// Note:
// Depending on board package file date, either
// ARDUINO_M5STACK_Core2 or ARDUINO_M5STACK_CORE2
// is used - see https://github.com/espressif/arduino-esp32/issues/9423!
// Depending on board package file date, some defines are written either
// in mixed-case or upper-case letters, for example,
// ARDUINO_M5STACK_Core2 or ARDUINO_M5STACK_CORE2 -
// see https://github.com/espressif/arduino-esp32/issues/9423!
// Consequently both variants have to be checked!!!
//
// ToDo:
Expand Down Expand Up @@ -95,6 +97,9 @@
// The maximum allowed for all data rates is 51 bytes.
const uint8_t PAYLOAD_SIZE = 51;

// Maximum downlink payload size (bytes)
const uint8_t MAX_DOWNLINK_SIZE = 51;

// Battery voltage thresholds for energy saving & deep-discharge prevention

// If battery voltage <= BATTERY_WEAK [mV], MCU will sleep for SLEEP_INTERVAL_LONG
Expand Down
4 changes: 1 addition & 3 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
// 20240530 Added stateDecode(), updated debug() from RadioLib v6.6.0
// (examples/LoRaWAN/LoRaWAN_Reference/config.h)
// 20240613 Added LORAWAN_NODE (DFRobot FireBeetle ESP32 wiring variant)
// 20240704 Moved MAX_DOWNLINK_SIZE to BresserWeatherSensorLWCfg.h
//
// ToDo:
// -
Expand All @@ -55,9 +56,6 @@
// How often to send an uplink - consider legal & FUP constraints - see notes
const uint32_t uplinkIntervalSeconds = 5UL * 60UL; // minutes x seconds

// Maximum downlink payload size (bytes)
#define MAX_DOWNLINK_SIZE 5

// JoinEUI - previous versions of LoRaWAN called this AppEUI
// for development purposes you can use all zeros - see wiki for details
#define RADIOLIB_LORAWAN_JOIN_EUI 0x0000000000000000
Expand Down
143 changes: 96 additions & 47 deletions scripts/uplink_formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@
// 20240609 Refactored command encoding
// 20240610 Fixed CMD_GET_SENSORS_CFG and CMD_GET_APP_PAYLOAD_CFG,
// decode function 'bits8'
// 20240612 Disabled BLE temperature/humidity and compatibility mode due to
// issues #65 & #68
// 20240704 Fixed/improved compatibility mode
//
// ToDo:
// -
Expand Down Expand Up @@ -211,7 +210,11 @@ function decoder(bytes, port) {
if (SKIP_INVALID_SIGNALS && (timestamp == -1) || (timestamp == 0)) {
return NaN;
}
return { time: time, timestamp: timestamp };
if (!COMPATIBILITY_MODE) {
return { time: time, timestamp: timestamp };
} else {
return timestamp;
}
};
unixtime.BYTES = 4;

Expand Down Expand Up @@ -467,7 +470,6 @@ function decoder(bytes, port) {
};
sensor_status.BYTES = 26;

//sensorStatus = {"status": {"ws_dec_ok": true}};
/**
* Decodes the given bytes using the provided mask and names.
*
Expand Down Expand Up @@ -499,9 +501,9 @@ function decoder(bytes, port) {
.map(function (decodeFn, idx) {
var current = bytes.slice(offset, offset += decodeFn.BYTES);
var decodedValue = decodeFn(current);
// Check if the decoded value is NaN
if (isNaN(decodedValue) && decodedValue.constructor === Number) {
if (COMPATIBILITY_MODE) {
// Check if the decoded value is NaN
var name = names[idx] || idx;
if ((name == "ws_temp_c") || (name == "ws_humidity") || (name == "ws_rain_mm") || (name.startsWith('ws_wind_'))) {
ws_dec_ok = false;
Expand Down Expand Up @@ -531,6 +533,9 @@ function decoder(bytes, port) {
decodedValues.status.ws_dec_ok = ws_dec_ok;
decodedValues.status.ls_dec_ok = ls_dec_ok;
decodedValues.status.s1_dec_ok = s1_dec_ok;
decodedValues.status.ws_batt_ok = decodedValues.status.ws_batt_ok || !ws_dec_ok;
decodedValues.status.ls_batt_ok = decodedValues.status.ls_batt_ok || !ls_dec_ok;
decodedValues.status.s1_batt_ok = decodedValues.status.s1_batt_ok || !s1_dec_ok;
decodedValues.status.ble_ok = ble_ok;
}
return decodedValues;
Expand Down Expand Up @@ -563,48 +568,92 @@ function decoder(bytes, port) {


if (port === 1) {
return decode(
port,
bytes,
[
temperature,
uint8,
rawfloat,
uint16fp1, uint16fp1, uint16fp1,
uint8fp1,
rawfloat,
rawfloat, rawfloat, rawfloat,
temperature, uint8,
temperature, uint8,
unixtime,
uint16,
uint8,
temperature,
uint16,
//temperature,
//uint8,
//bitmap_sensors
],
[
'ws_temp_c',
'ws_humidity',
'ws_rain_mm',
'ws_wind_gust_ms', 'ws_wind_avg_ms', 'ws_wind_dir_deg',
'ws_uv',
'ws_rain_hourly_mm',
'ws_rain_daily_mm', 'ws_rain_weekly_mm', 'ws_rain_monthly_mm',
'th1_temp_c', 'th1_humidity',
'soil1_temp_c', 'soil1_moisture',
'lgt_ev_time',
'lgt_ev_events',
'lgt_ev_dist_km',
'ow0_temp_c',
'a0_voltage_mv',
//'ble0_temp_c',
//'ble0_humidity',
//'status'
]
);
if (!COMPATIBILITY_MODE) {
return decode(
port,
bytes,
[
temperature,
uint8,
rawfloat,
uint16fp1, uint16fp1, uint16fp1,
uint8fp1,
rawfloat,
rawfloat, rawfloat, rawfloat,
temperature, uint8,
temperature, uint8,
unixtime,
uint16,
uint8,
temperature,
uint16,
temperature,
uint8
],
[
'ws_temp_c',
'ws_humidity',
'ws_rain_mm',
'ws_wind_gust_ms', 'ws_wind_avg_ms', 'ws_wind_dir_deg',
'ws_uv',
'ws_rain_hourly_mm',
'ws_rain_daily_mm', 'ws_rain_weekly_mm', 'ws_rain_monthly_mm',
'th1_temp_c', 'th1_humidity',
'soil1_temp_c', 'soil1_moisture',
'lgt_ev_time',
'lgt_ev_events',
'lgt_ev_dist_km',
'ow0_temp_c',
'a0_voltage_mv',
'ble0_temp_c',
'ble0_humidity'
]
);
} else {
// COMPATIBILITY_MODE - not recommended for new designs!
return decode(
port,
bytes,
[
temperature,
uint8,
rawfloat,
uint16fp1, uint16fp1, uint16fp1,
uint8fp1,
rawfloat,
rawfloat, rawfloat, rawfloat,
temperature, uint8,
temperature, uint8,
unixtime,
uint16,
uint8,
temperature,
uint16,
temperature,
uint8,
bitmap_sensors
],
[
'air_temp_c',
'humidity',
'rain_mm',
'wind_gust_meter_sec', 'wind_avg_meter_sec', 'wind_direction_deg',
'ws_uv', // new
'rain_hr',
'rain_day', 'rain_week', 'rain_month',
'th1_temp_c', 'th1_humidity', //new
'soil_temp_c', 'soil_moisture',
'lightning_time',
'lightning_events',
'lightning_distance_km',
'water_temp_c',
'supply_v',
'indoor_temp_c',
'indoor_humidity',
'status'
]
);
}
//return {...res, ...sensorStatus};

} else if (port === CMD_GET_DATETIME) {
Expand Down

0 comments on commit 580f75a

Please sign in to comment.