Skip to content

Commit

Permalink
Fixed handling of arrays in decoder()
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-bs committed May 31, 2024
1 parent 5c78bfd commit fc920e4
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 47 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "BresserWeatherSensorLW",
"version": "0.5.0",
"version": "0.6.0",
"description": "Bresser 868 MHz Weather Sensor Radio Receiver; provides data via LoRaWAN",
"main": "BresserWeatherSensorLW.ino",
"frameworks": "arduino",
Expand All @@ -23,7 +23,7 @@
"homepage": "https://github.com/matthias-bs/BresserWeatherSensorLW#README",
"dependencies": {
"BresserWeatherSensorReceiver": "matthias-bs/BresserWeatherSensorReceiver#semver:^0.28.2",
"RadioLib": "jgromes/RadioLib#semver:^6.5.0",
"RadioLib": "jgromes/RadioLib#semver:^6.6.0",
"lora-serialization": "thesolarnomad/lora-serialization#semver:^3.2.1",
"ESP32Time": "fbiego/ESP32Time#semver:^2.0.6",
"OneWireNg": "https://github.com/pstolarz/OneWireNg#semver:^0.13.3",
Expand Down
64 changes: 32 additions & 32 deletions scripts/uplink_formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
// Added supression of NaN results in decoder
// 20240530 Added SKIP_INVALID_SIGNALS
// Added BLE signals to decoder
// 20240531 Fixed handling of arrays in decoder()
//
// ToDo:
// -
Expand All @@ -139,7 +140,7 @@ function decoder(bytes, port) {
const CMD_GET_BLE_CONFIG = 0xCA;
const CMD_GET_APP_PAYLOAD_CFG = 0xCE;

var rtc_source_code = {
const rtc_source_code = {
0x00: "GPS",
0x01: "RTC",
0x02: "LORA",
Expand All @@ -156,7 +157,7 @@ function decoder(bytes, port) {
rtc_source.BYTES = 1;

var bytesToInt = function (bytes) {
var i = 0;
let i = 0;
for (var x = 0; x < bytes.length; x++) {
i |= +(bytes[x] << (x * 8));
}
Expand Down Expand Up @@ -184,7 +185,7 @@ function decoder(bytes, port) {
if (bytes.length !== uint8.BYTES) {
throw new Error('int must have exactly 1 byte');
}
var res = bytesToInt(bytes);
let res = bytesToInt(bytes);
if (SKIP_INVALID_SIGNALS && res === 0xFF) {
return NaN;
}
Expand All @@ -196,7 +197,7 @@ function decoder(bytes, port) {
if (bytes.length !== uint8fp1.BYTES) {
throw new Error('int must have exactly 1 byte');
}
var res = bytesToInt(bytes);
let res = bytesToInt(bytes);
if (SKIP_INVALID_SIGNALS && res === 0xFF) {
return NaN;
}
Expand All @@ -209,7 +210,7 @@ function decoder(bytes, port) {
if (bytes.length !== uint16.BYTES) {
throw new Error('int must have exactly 2 bytes');
}
var res = bytesToInt(bytes);
let res = bytesToInt(bytes);
if (SKIP_INVALID_SIGNALS && res === 0xFFFF) {
return NaN;
}
Expand All @@ -221,7 +222,7 @@ function decoder(bytes, port) {
if (bytes.length !== uint16fp1.BYTES) {
throw new Error('int must have exactly 2 bytes');
}
var res = bytesToInt(bytes);
let res = bytesToInt(bytes);
if (SKIP_INVALID_SIGNALS && res === 0xFFFF) {
return NaN;
}
Expand Down Expand Up @@ -259,59 +260,57 @@ function decoder(bytes, port) {
}

var mac48 = function (bytes) {
var res = [];
var size = bytes.length;
var j = 0;
let res = [];
let j = 0;
for (var i = 0; i < bytes.length; i += 6) {
res[j++] = byte2hex(bytes[i]) + ":" + byte2hex(bytes[i + 1]) + ":" + byte2hex(bytes[i + 2]) + ":" +
byte2hex(bytes[i + 3]) + ":" + byte2hex(bytes[i + 4]) + ":" + byte2hex(bytes[i + 5]);
}
return res;
}
};
mac48.BYTES = bytes.length;

var bresser_bitmaps = function (bytes) {
var res = [];
let res = [];
res[0] = "0x" + byte2hex(bytes[0]);
for (var i = 1; i < 16; i++) {
res[i] = "0x" + byte2hex(bytes[i]);
}
return res;
}
};
bresser_bitmaps.BYTES = 16;

var hex16 = function (bytes) {
var res;
let res;
res = "0x" + byte2hex(bytes[0]) + byte2hex(bytes[1]);
return res;
}
};
hex16.BYTES = 2;

var hex32 = function (bytes) {
var res;
let res;
res = "0x" + byte2hex(bytes[0]) + byte2hex(bytes[1]) + byte2hex(bytes[2]) + byte2hex(bytes[3]);
return res;
}
};
hex32.BYTES = 4;

var id32 = function (bytes) {
var res = [];
var size = bytes.length;
var j = 0;
let res = [];
let j = 0;
for (var i = 0; i < bytes.length; i += 4) {
res[j++] = "0x" + byte2hex(bytes[i]) + byte2hex(bytes[i + 1]) + byte2hex(bytes[i + 2]) + byte2hex(bytes[i + 3]);
}
return res;
}
};
id32.BYTES = bytes.length;

var latLng = function (bytes) {
if (bytes.length !== latLng.BYTES) {
throw new Error('Lat/Long must have exactly 8 bytes');
}

var lat = bytesToInt(bytes.slice(0, latLng.BYTES / 2));
var lng = bytesToInt(bytes.slice(latLng.BYTES / 2, latLng.BYTES));
let lat = bytesToInt(bytes.slice(0, latLng.BYTES / 2));
let lng = bytesToInt(bytes.slice(latLng.BYTES / 2, latLng.BYTES));

return [lat / 1e6, lng / 1e6];
};
Expand All @@ -321,11 +320,11 @@ function decoder(bytes, port) {
if (bytes.length !== temperature.BYTES) {
throw new Error('Temperature must have exactly 2 bytes');
}
var isNegative = bytes[0] & 0x80;
var b = ('00000000' + Number(bytes[0]).toString(2)).slice(-8)
let isNegative = bytes[0] & 0x80;
let b = ('00000000' + Number(bytes[0]).toString(2)).slice(-8)
+ ('00000000' + Number(bytes[1]).toString(2)).slice(-8);
if (isNegative) {
var arr = b.split('').map(function (x) { return !Number(x); });
let arr = b.split('').map(function (x) { return !Number(x); });
for (var i = arr.length - 1; i > 0; i--) {
arr[i] = !arr[i];
if (arr[i]) {
Expand All @@ -334,7 +333,7 @@ function decoder(bytes, port) {
}
b = arr.map(Number).join('');
}
var t = parseInt(b, 2);
let t = parseInt(b, 2);
if (isNegative) {
t = -t;
}
Expand All @@ -351,7 +350,7 @@ function decoder(bytes, port) {
throw new Error('Humidity must have exactly 2 bytes');
}

var h = bytesToInt(bytes);
let h = bytesToInt(bytes);
if (SKIP_INVALID_SIGNALS && h === 0xFFFF) {
return NaN;
}
Expand Down Expand Up @@ -383,8 +382,8 @@ function decoder(bytes, port) {
if (byte.length !== bitmap_node.BYTES) {
throw new Error('Bitmap must have exactly 1 byte');
}
var i = bytesToInt(byte);
var bm = ('00000000' + Number(i).toString(2)).slice(-8).split('').map(Number).map(Boolean);
let i = bytesToInt(byte);
let bm = ('00000000' + Number(i).toString(2)).slice(-8).split('').map(Number).map(Boolean);

return ['res7', 'res6', 'res5', 'res4', 'res3', 'res2', 'res1', 'res0']
.reduce(function (obj, pos, index) {
Expand All @@ -398,8 +397,8 @@ function decoder(bytes, port) {
if (byte.length !== bitmap_sensors.BYTES) {
throw new Error('Bitmap must have exactly 1 byte');
}
var i = bytesToInt(byte);
var bm = ('00000000' + Number(i).toString(2)).slice(-8).split('').map(Number).map(Boolean);
let i = bytesToInt(byte);
let bm = ('00000000' + Number(i).toString(2)).slice(-8).split('').map(Number).map(Boolean);
// Only Weather Sensor
//return ['res5', 'res4', 'res3', 'res2', 'res1', 'res0', 'dec_ok', 'batt_ok']
// Weather Sensor + MiThermo (BLE) Sensor
Expand Down Expand Up @@ -438,7 +437,7 @@ function decoder(bytes, port) {
.map(function (decodeFn) {
var current = bytes.slice(offset, offset += decodeFn.BYTES);
var decodedValue = decodeFn(current);
if (isNaN(decodedValue)) {
if (isNaN(decodedValue) && decodedValue.constructor === Number) {
return null;
}
return decodedValue;
Expand All @@ -460,6 +459,7 @@ function decoder(bytes, port) {
uint16BE: uint16BE,
uint32BE: uint32BE,
mac48: mac48,
bresser_bitmaps: bresser_bitmaps,
temperature: temperature,
humidity: humidity,
latLng: latLng,
Expand Down
128 changes: 115 additions & 13 deletions secrets.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,117 @@
// 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

// The Device EUI & two keys can be generated on the TTN console (or any other LoRaWAN Network Service Provider's console)
#pragma message("Replace the dummy values for RADIOLIB_LORAWAN_DEV_EUI, RADIOLIB_LORAWAN_APP_KEY and RADIOLIB_LORAWAN_NWK_KEY by your own credentials.")
#ifndef RADIOLIB_LORAWAN_DEV_EUI // Replace with your Device EUI
#define RADIOLIB_LORAWAN_DEV_EUI 0x0000000000000000
#endif
#ifndef RADIOLIB_LORAWAN_APP_KEY // Replace with your App Key
#define RADIOLIB_LORAWAN_APP_KEY 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
#define SECRETS

//-------------------------------
// --> REPLACE BY YOUR VALUES <--
//-------------------------------

#if 0
// application identifier - pre-LoRaWAN 1.1.0, this was called appEUI
// when adding new end device in TTN, you will have to enter this number
// you can pick any number you want, but it has to be unique
uint64_t joinEUI = 0x12AD1011B0C0FFEE;

// device identifier - this number can be anything
// when adding new end device in TTN, you can generate this number,
// or you can set any value you want, provided it is also unique
uint64_t devEUI = 0x70B3D57ED005E120;

// select some encryption keys which will be used to secure the communication
// there are two of them - network key and application key
// because LoRaWAN uses AES-128, the key MUST be 16 bytes (or characters) long

// network key is the ASCII string "topSecretKey1234"
uint8_t nwkKey[] = { 0x74, 0x6F, 0x70, 0x53, 0x65, 0x63, 0x72, 0x65,
0x74, 0x4B, 0x65, 0x79, 0x31, 0x32, 0x33, 0x34 };

// application key is the ASCII string "aDifferentKeyABC"
uint8_t appKey[] = { 0x61, 0x44, 0x69, 0x66, 0x66, 0x65, 0x72, 0x65,
0x6E, 0x74, 0x4B, 0x65, 0x79, 0x41, 0x42, 0x43 };

// prior to LoRaWAN 1.1.0, only a single "nwkKey" is used
// when connecting to LoRaWAN 1.0 network, "appKey" will be disregarded
// and can be set to NULL
#endif
#ifndef RADIOLIB_LORAWAN_NWK_KEY // Put your Nwk Key here
#define RADIOLIB_LORAWAN_NWK_KEY 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00


// Adafruit RFM9x LoRa Radio #1
//#define RADIO1
//#define RADIO4
#define RADIO5
//#define RADIO7
//#define RADIO9

#ifdef RADIO1
static const uint64_t devEUI = 0x9876B6000011C941;

static const uint64_t joinEUI = 0x0000000000000000;

static const std::uint8_t nwkKey[] = { 0x81, 0x83, 0xF4, 0xDD, 0x1B, 0x1F, 0xCF, 0x36, 0xC3, 0x73, 0x2A, 0xBE, 0x7C, 0xA2, 0x2F, 0x92 };

static const std::uint8_t appKey[] = { 0x24, 0xD4, 0x43, 0xCD, 0xB4, 0xD5, 0xDE, 0x3F, 0x9E, 0x17, 0xB7, 0x19, 0xE1, 0xD7, 0x16, 0xA2 };

#elif defined(RADIO4)
// Adafruit RFM9x LoRa Radio #4

static const uint64_t devEUI = 0x9876B6000011F87A;

static const uint64_t joinEUI = 0x0000000000000000;

static const std::uint8_t nwkKey[] = { 0x81, 0x83, 0xF4, 0xDD, 0x1B, 0x1F, 0xCF, 0x36, 0xC3, 0x73, 0x2A, 0xBE, 0x7C, 0xA2, 0x2F, 0x92 };

static const std::uint8_t appKey[] = { 0x39, 0x15, 0x2C, 0xBB, 0xDA, 0xEC, 0x25, 0xC6, 0x53, 0x4F, 0xA2, 0x20, 0x40, 0x04, 0xFA, 0xC8 };

#elif defined(RADIO5)

#define RADIOLIB_LORAWAN_DEV_EUI 0x0C8B95AAB05CFEFF

//static const uint64_t joinEUI = 0x0000000000000000;

#define RADIOLIB_LORAWAN_NWK_KEY 0x81, 0x83, 0xF4, 0xDD, 0x1B, 0x1F, 0xCF, 0x36, 0xC3, 0x73, 0x2A, 0xBE, 0x7C, 0xA2, 0x2F, 0x92

#define RADIOLIB_LORAWAN_APP_KEY 0x73, 0xF9, 0x96, 0x32, 0x29, 0x2A, 0xF9, 0x23, 0xC0, 0x7D, 0x26, 0x43, 0x1D, 0x0C, 0x9A, 0xBC

#elif defined(RADIO6)
static const uint64_t devEUI = 0x0C8B95AAAE6CFEFF;

static const uint64_t joinEUI = 0x0000000000000000;

static const std::uint8_t nwkKey[] = { 0x81, 0x83, 0xF4, 0xDD, 0x1B, 0x1F, 0xCF, 0x36, 0xC3, 0x73, 0x2A, 0xBE, 0x7C, 0xA2, 0x2F, 0x92 };

static const std::uint8_t appKey[] = { 0x59, 0xDC, 0x28, 0x58, 0xF6, 0x0C, 0x54, 0x70, 0x4E, 0x2E, 0xCB, 0x5E, 0x7B, 0x10, 0x79, 0x70 };

#elif defined(RADIO7)
// FireBeetleCover LoRa Radio #7
// pv-inverter-0

#define RADIOLIB_LORAWAN_DEV_EUI 0x70B3D57ED005CF56

// appeui, little-endian (lsb first)
///static const uint64_t joinEUI = 0x0000000000000000;

#define RADIOLIB_LORAWAN_NWK_KEY 0x2B, 0xA8, 0x2A, 0x2D, 0xCE, 0x8F, 0xC5, 0x01, 0x4A, 0xBB, 0xFA, 0x7A, 0xF7, 0x97, 0x1D, 0x3B

#define RADIOLIB_LORAWAN_APP_KEY 0xD6, 0xB3, 0x3F, 0xBB, 0x7B, 0x3B, 0x01, 0xC8, 0x04, 0x8A, 0xCF, 0x9E, 0x24, 0x12, 0x45, 0xC1

#elif defined(RADIO8)
// FireBeetleCover LoRa Radio #8
// pv-inverter-1

static const uint64_t devEUI[] = 0x70B3D57ED005D3B9;

static const uint64_t joinEUI = 0x0000000000000000;

static const std::uint8_t nwkKey[] = { 0x81, 0x83, 0xF4, 0xDD, 0x1B, 0x1F, 0xCF, 0x36, 0xC3, 0x73, 0x2A, 0xBE, 0x7C, 0xA2, 0x2F, 0x92 };

static const std::uint8_t appKey[] = { 0xB2, 0xA3, 0xD7, 0x16, 0xA6, 0x77, 0x8E, 0xEF, 0xBA, 0x57, 0xB7, 0xBE, 0x4F, 0xD7, 0xC4, 0x7E };

#elif defined(RADIO9)
// 98:76:B6:12:87:a1
static const uint64_t devEUI[] = 0x9876B600001287A1;

static const uint64_t joinEUI = 0x0000000000000000;

static const std::uint8_t nwkKey[] = { 0x81, 0x83, 0xF4, 0xDD, 0x1B, 0x1F, 0xCF, 0x36, 0xC3, 0x73, 0x2A, 0xBE, 0x7C, 0xA2, 0x2F, 0x92 };

static const std::uint8_t appKey[] = { 0x8F, 0xC5, 0x12, 0xE1, 0x22, 0xC8, 0xD0, 0xFC, 0xB7, 0x9F, 0x04, 0xC1, 0xAE, 0xC9, 0x51, 0x4C };

#endif
6 changes: 6 additions & 0 deletions secrets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"joinEUI": "0x0000000000000000",
"devEUI": "0x0C8B95AAB05CFEFF",
"nwkKey": ["0x81", "0x83", "0xF4", "0xDD", "0x1B", "0x1F", "0xCF", "0x36", "0xC3", "0x73", "0x2A", "0xBE", "0x7C", "0xA2", "0x2F", "0x92"],
"appKey": ["0x73", "0xF9", "0x96", "0x32", "0x29", "0x2A", "0xF9", "0x23", "0xC0", "0x7D", "0x26", "0x43", "0x1D", "0x0C", "0x9A", "0xBC"]
}

0 comments on commit fc920e4

Please sign in to comment.