Data sent to chirpstack gateway is wrong #1317
-
I am using the sample persistent ESP32 code modified to support my hardware main.ino /*
This demonstrates how to save the join information in to permanent memory
so that if the power fails, batteries run out or are changed, the rejoin
is more efficient & happens sooner due to the way that LoRaWAN secures
the join process - see the wiki for more details.
This is typically useful for devices that need more power than a battery
driven sensor - something like a air quality monitor or GPS based device that
is likely to use up it's power source resulting in loss of the session.
The relevant code is flagged with a ##### comment
Saving the entire session is possible but not demonstrated here - it has
implications for flash wearing and complications with which parts of the
session may have changed after an uplink. So it is assumed that the device
is going in to deep-sleep, as below, between normal uplinks.
Once you understand what happens, feel free to delete the comments and
Serial.prints - we promise the final result isn't that many lines.
*/
#if !defined(ESP32)
#pragma error ("This is not the example your device is looking for - ESP32 only")
#endif
// ##### load the ESP32 preferences facilites
#include <Preferences.h>
Preferences store;
// LoRaWAN config, credentials & pinmap
#include "config.h"
#include <RadioLib.h>
// utilities & vars to support ESP32 deep-sleep. The RTC_DATA_ATTR attribute
// puts these in to the RTC memory which is preserved during deep-sleep
RTC_DATA_ATTR uint16_t bootCount = 0;
RTC_DATA_ATTR uint16_t bootCountSinceUnsuccessfulJoin = 0;
RTC_DATA_ATTR uint8_t LWsession[RADIOLIB_LORAWAN_SESSION_BUF_SIZE];
// abbreviated version from the Arduino-ESP32 package, see
// https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/api/deepsleep.html
// for the complete set of options
void print_wakeup_reason() {
esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause();
if (wakeup_reason == ESP_SLEEP_WAKEUP_TIMER) {
Serial.println(F("Wake from sleep"));
} else {
Serial.print(F("Wake not caused by deep sleep: "));
Serial.println(wakeup_reason);
}
Serial.print(F("Boot count: "));
Serial.println(++bootCount); // increment before printing
}
// put device in to lowest power deep-sleep mode
void gotoSleep(uint32_t seconds) {
esp_sleep_enable_timer_wakeup(seconds * 1000UL * 1000UL); // function uses uS
Serial.println(F("Sleeping\n"));
Serial.flush();
esp_deep_sleep_start();
// if this appears in the serial debug, we didn't go to sleep!
// so take defensive action so we don't continually uplink
Serial.println(F("\n\n### Sleep failed, delay of 5 minutes & then restart ###\n"));
delay(5UL * 60UL * 1000UL);
ESP.restart();
}
int16_t lwActivate() {
int16_t state = RADIOLIB_ERR_UNKNOWN;
// setup the OTAA session information
node.beginOTAA(joinEUI, devEUI, nwkKey, appKey);
Serial.println(F("Recalling LoRaWAN nonces & session"));
// ##### setup the flash storage
store.begin("radiolib");
// ##### if we have previously saved nonces, restore them and try to restore session as well
if (store.isKey("nonces")) {
uint8_t buffer[RADIOLIB_LORAWAN_NONCES_BUF_SIZE]; // create somewhere to store nonces
store.getBytes("nonces", buffer, RADIOLIB_LORAWAN_NONCES_BUF_SIZE); // get them from the store
state = node.setBufferNonces(buffer); // send them to LoRaWAN
debug(state != RADIOLIB_ERR_NONE, F("Restoring nonces buffer failed"), state, false);
// recall session from RTC deep-sleep preserved variable
state = node.setBufferSession(LWsession); // send them to LoRaWAN stack
// if we have booted more than once we should have a session to restore, so report any failure
// otherwise no point saying there's been a failure when it was bound to fail with an empty LWsession var.
debug((state != RADIOLIB_ERR_NONE) && (bootCount > 1), F("Restoring session buffer failed"), state, false);
// if Nonces and Session restored successfully, activation is just a formality
// moreover, Nonces didn't change so no need to re-save them
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("Succesfully restored session - now activating"));
state = node.activateOTAA();
debug((state != RADIOLIB_LORAWAN_SESSION_RESTORED), F("Failed to activate restored session"), state, true);
// ##### close the store before returning
store.end();
return(state);
}
} else { // store has no key "nonces"
Serial.println(F("No Nonces saved - starting fresh."));
}
// if we got here, there was no session to restore, so start trying to join
state = RADIOLIB_ERR_NETWORK_NOT_JOINED;
while (state != RADIOLIB_LORAWAN_NEW_SESSION) {
Serial.println(F("Join ('login') to the LoRaWAN Network"));
state = node.activateOTAA();
// ##### save the join counters (nonces) to permanent store
Serial.println(F("Saving nonces to flash"));
uint8_t buffer[RADIOLIB_LORAWAN_NONCES_BUF_SIZE]; // create somewhere to store nonces
uint8_t *persist = node.getBufferNonces(); // get pointer to nonces
memcpy(buffer, persist, RADIOLIB_LORAWAN_NONCES_BUF_SIZE); // copy in to buffer
store.putBytes("nonces", buffer, RADIOLIB_LORAWAN_NONCES_BUF_SIZE); // send them to the store
// we'll save the session after an uplink
if (state != RADIOLIB_LORAWAN_NEW_SESSION) {
Serial.print(F("Join failed: "));
Serial.println(state);
// how long to wait before join attempts. This is an interim solution pending
// implementation of TS001 LoRaWAN Specification section #7 - this doc applies to v1.0.4 & v1.1
// it sleeps for longer & longer durations to give time for any gateway issues to resolve
// or whatever is interfering with the device <-> gateway airwaves.
uint32_t sleepForSeconds = min((bootCountSinceUnsuccessfulJoin++ + 1UL) * 60UL, 3UL * 60UL);
Serial.print(F("Boots since unsuccessful join: "));
Serial.println(bootCountSinceUnsuccessfulJoin);
Serial.print(F("Retrying join in "));
Serial.print(sleepForSeconds);
Serial.println(F(" seconds"));
gotoSleep(sleepForSeconds);
} // if activateOTAA state
} // while join
Serial.println(F("Joined"));
// reset the failed join count
bootCountSinceUnsuccessfulJoin = 0;
delay(1000); // hold off off hitting the airwaves again too soon - an issue in the US
// ##### close the store
store.end();
return(state);
}
// setup & execute all device functions ...
void setup() {
Serial.begin(115200);
while (!Serial); // wait for serial to be initalised
delay(2000); // give time to switch to the serial monitor
Serial.println(F("\nSetup"));
print_wakeup_reason();
int16_t state = 0; // return value for calls to RadioLib
// setup the radio based on the pinmap (connections) in config.h
Serial.println(F("Initalise the radio"));
state = radio.begin();
debug(state != RADIOLIB_ERR_NONE, F("Initalise radio failed"), state, true);
node.setADR(true);
// activate node by restoring session or otherwise joining the network
state = lwActivate();
// state is one of RADIOLIB_LORAWAN_NEW_SESSION or RADIOLIB_LORAWAN_SESSION_RESTORED
// ----- and now for the main event -----
Serial.println(F("Sending uplink"));
// build payload byte array
uint8_t uplinkPayload[3];
uplinkPayload[0] = 0xde;
uplinkPayload[1] = 0xad;
uplinkPayload[2] = 0xff;
// perform an uplink
state = node.sendReceive(uplinkPayload, sizeof(uplinkPayload));
debug((state < RADIOLIB_ERR_NONE), F("Error in sendReceive"), state, false);
Serial.print(F("FcntUp: "));
Serial.println(node.getFCntUp());
// now save session to RTC memory
uint8_t *persist = node.getBufferSession();
memcpy(LWsession, persist, RADIOLIB_LORAWAN_SESSION_BUF_SIZE);
// wait until next uplink - observing legal & TTN FUP constraints
gotoSleep(uplinkIntervalSeconds);
}
// The ESP32 wakes from deep-sleep and starts from the very beginning.
// It then goes back to sleep, so loop() is never called and which is
// why it is empty.
void loop() {} config.h: #ifndef _CONFIG_H
#define _CONFIG_H
#include <RadioLib.h>
#define RADIO_CS 8
#define RADIO_INT 14
#define RADIO_RST 12
#define RADIO_BSY 13
// How often to send an uplink - consider legal & FUP constraints - see notes
const uint32_t uplinkIntervalSeconds = 5UL * 60UL; // minutes x seconds
// 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 0x89589B10FABF935D
#define RADIOLIB_LORAWAN_DEV_EUI 0x5432ecf93b9e9ef0
#define RADIOLIB_LORAWAN_APP_KEY 0xDA, 0xAE, 0x29, 0xC8, 0x9F, 0x7F, 0xA3, 0xC6, 0x48, 0x93, 0xF8, 0x69, 0xAD, 0x83, 0x7A, 0xEE // daae29c89f7fa3c64894f869ad837aee
#define RADIOLIB_LORAWAN_NWK_KEY 0x23, 0xC9, 0x56, 0xC3, 0x36, 0xC1, 0xD1, 0x3C, 0x28, 0x3C, 0x91, 0xE6, 0xA7, 0x03, 0x19, 0x8B
// For the curious, the #ifndef blocks allow for automated testing &/or you can
// put your EUI & keys in to your platformio.ini - see wiki for more tips
// Regional choices: EU868, US915, AU915, AS923, IN865, KR920, CN780, CN500
const LoRaWANBand_t Region = US915;
const uint8_t subBand = 2; // For US915, change this to 2, otherwise leave on 0ate the LoRaWAN node
// ============================================================================
// Below is to support the sketch - only make changes if the notes say so ...
// Auto select MCU <-> radio connections
// If you get an error message when compiling, it may be that the
// pinmap could not be determined - see the notes for more info
SX1262 radio = new Module(RADIO_CS, RADIO_INT, RADIO_RST, RADIO_BSY);
// Copy over the EUI's & keys in to the something that will not compile if incorrectly formatted
uint64_t joinEUI = RADIOLIB_LORAWAN_JOIN_EUI;
uint64_t devEUI = RADIOLIB_LORAWAN_DEV_EUI;
uint8_t appKey[] = { RADIOLIB_LORAWAN_APP_KEY };
uint8_t nwkKey[] = { RADIOLIB_LORAWAN_NWK_KEY };
// Create the LoRaWAN node
LoRaWANNode node(&radio, &Region, subBand);
// Helper function to display any issues
void debug(bool isFail, const __FlashStringHelper* message, int state, bool Freeze) {
if (isFail) {
Serial.print(message);
Serial.print("(");
Serial.print(state);
Serial.println(")");
while (Freeze);
}
}
// Helper function to display a byte array
void arrayDump(uint8_t *buffer, uint16_t len) {
for (uint16_t c = 0; c < len; c++) {
Serial.printf("%02X", buffer[c]);
}
Serial.println();
}
#endif The chirpstack logs show the uplink data as Chirpstack is configured for forward this to an http endpoint, and it shows the same thing (which is good, I think)? {
"deduplicationId": "9190485b-1bc9-4dee-8275-fe35c50e5775",
"time": "2024-11-15T13:30:07.944779943+00:00",
"deviceInfo": {
"tenantId": "592d5555-aa9a-4448-abe0-2bd6764fd2b6",
"tenantName": "ChirpStack",
"applicationId": "xxxxxxx2",
"applicationName": "Button",
"deviceProfileId": "xxxxxx",
"deviceProfileName": "Panic Button",
"deviceName": "Test Device #1",
"devEui": "xxxxxxx",
"deviceClassEnabled": "CLASS_A",
"tags": {}
},
"devAddr": "00262a1e",
"adr": true,
"dr": 3,
"fCnt": 1,
"fPort": 1,
"confirmed": false,
"data": "aErq",
"rxInfo": [
{
"gatewayId": "0016c001f160f3c5",
"uplinkId": 1879065181,
"nsTime": "2024-11-15T13:30:07.712276479+00:00",
"rssi": -44,
"snr": 14.25,
"location": {},
"context": "aEVWOg==",
"metadata": {
"region_config_id": "us915_1",
"region_common_name": "US915"
},
"crcStatus": "CRC_OK"
}
],
"txInfo": {
"frequency": 903900000,
"modulation": {
"lora": {
"bandwidth": 125000,
"spreadingFactor": 7,
"codeRate": "CR_4_5"
}
}
}
} And in fact, if I just leave this to send the same data again and again, chirpstack is reporting different data. I was pretty sure I had this working, but now I am stuck and have no clue. Is there something I can do to make sure the gateway receives 0xdeadff per my code, or at least drop if their is a CRC error? Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I think it might have been one of my encryption keys that was wrong. The weird thing is that I would have thought that it wouldn't have joined and the CRCs would have been wrong. |
Beta Was this translation helpful? Give feedback.
Welcome to LoRaWAN v1.1 where the
NwkKey
handles all the network stuff, and theAppKey
handles just the application payload. So you can have the NwkKey right and the AppKey wrong and still have it function!