Skip to content

Commit

Permalink
[LoRaWAN] Improve examples, add getter for DevAddr (#974)
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenCellist authored Feb 15, 2024
1 parent bb23946 commit 0ea00fa
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 184 deletions.
25 changes: 16 additions & 9 deletions examples/LoRaWAN/LoRaWAN_End_Device/LoRaWAN_End_Device.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
After your device is registered, you can run this example.
The device will join the network and start uploading data.
LoRaWAN v1.1 requires the use of EEPROM (persistent storage).
Please refer to the 'persistent' example once you are familiar
with LoRaWAN.
Running this examples REQUIRES you to check "Resets DevNonces"
on your LoRaWAN dashboard. Refer to the network's
documentation on how to do this.
NOTE: LoRaWAN v1.1 requires storing parameters persistently!
RadioLib does this by using EEPROM (persistent storage),
by default starting at address 0 and using 448 bytes.
If you already use EEPROM in your application,
you will have to either avoid this range, or change it
by setting a different start address by changing the value of
RADIOLIB_HAL_PERSISTENT_STORAGE_BASE macro, either
during build or in src/BuildOpt.h.
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration
Expand Down Expand Up @@ -49,8 +51,8 @@ LoRaWANNode node(&radio, &EU868);
void setup() {
Serial.begin(9600);

// initialize SX1278 with default settings
Serial.print(F("[SX1278] Initializing ... "));
// initialize radio (SX1262 / SX1278 / ... ) with default settings
Serial.print(F("[Radio] Initializing ... "));
int state = radio.begin();
if(state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
Expand Down Expand Up @@ -104,6 +106,7 @@ void setup() {

if(state >= RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
delay(2000); // small delay between joining and uplink
} else {
Serial.print(F("failed, code "));
Serial.println(state);
Expand All @@ -118,7 +121,7 @@ int count = 0;
void loop() {
// send uplink to port 10
Serial.print(F("[LoRaWAN] Sending uplink packet ... "));
String strUp = "Hello World! #" + String(count++);
String strUp = "Hello!" + String(count++);
String strDown;
int state = node.sendReceive(strUp, 10, strDown);
if(state == RADIOLIB_ERR_NONE) {
Expand Down Expand Up @@ -154,6 +157,10 @@ void loop() {
Serial.print(F("failed, code "));
Serial.println(state);
}

// on EEPROM enabled boards, you can save the current session
// by calling "saveSession" which allows retrieving the session after reboot or deepsleep
node.saveSession();

// wait before sending another packet
uint32_t minimumDelay = 60000; // try to send once every minute
Expand Down
24 changes: 15 additions & 9 deletions examples/LoRaWAN/LoRaWAN_End_Device_ABP/LoRaWAN_End_Device_ABP.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
The device will start uploading data directly,
without having to join the network.
LoRaWAN v1.1 requires the use of EEPROM (persistent storage).
Please refer to the 'persistent' example once you are familiar
with LoRaWAN.
Running this examples REQUIRES you to check "Resets DevNonces"
on your LoRaWAN dashboard. Refer to the network's
documentation on how to do this.
NOTE: LoRaWAN v1.1 requires storing parameters persistently!
RadioLib does this by using EEPROM (persistent storage),
by default starting at address 0 and using 448 bytes.
If you already use EEPROM in your application,
you will have to either avoid this range, or change it
by setting a different start address by changing the value of
RADIOLIB_HAL_PERSISTENT_STORAGE_BASE macro, either
during build or in src/BuildOpt.h.
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration
Expand Down Expand Up @@ -50,8 +52,8 @@ LoRaWANNode node(&radio, &EU868);
void setup() {
Serial.begin(9600);

// initialize SX1278 with default settings
Serial.print(F("[SX1278] Initializing ... "));
// initialize radio (SX1262 / SX1278 / ... ) with default settings
Serial.print(F("[Radio] Initializing ... "));
int state = radio.begin();
if(state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
Expand Down Expand Up @@ -126,7 +128,7 @@ int count = 0;
void loop() {
// send uplink to port 10
Serial.print(F("[LoRaWAN] Sending uplink packet ... "));
String strUp = "Hello World! #" + String(count++);
String strUp = "Hello!" + String(count++);
String strDown;
int state = node.sendReceive(strUp, 10, strDown);
if(state == RADIOLIB_ERR_NONE) {
Expand Down Expand Up @@ -163,6 +165,10 @@ void loop() {
Serial.println(state);
}

// on EEPROM enabled boards, you can save the current session
// by calling "saveSession" which allows retrieving the session after reboot or deepsleep
node.saveSession();

// wait before sending another packet
uint32_t minimumDelay = 60000; // try to send once every minute
uint32_t interval = node.timeUntilUplink(); // calculate minimum duty cycle delay (per law!)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ LoRaWANNode node(&radio, &EU868);
void setup() {
Serial.begin(9600);

// initialize SX1278 with default settings
Serial.print(F("[SX1278] Initializing ... "));
// initialize radio (SX1262 / SX1278 / ... ) with default settings
Serial.print(F("[Radio] Initializing ... "));
int state = radio.begin();
if(state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
Expand Down Expand Up @@ -102,12 +102,16 @@ void setup() {

if(state >= RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
delay(2000); // small delay between joining and uplink
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while(true);
}

Serial.print("[LoRaWAN] DevAddr: ");
Serial.println(node.getDevAddr(), HEX);

// on EEPROM-enabled boards, after the device has been activated,
// the session can be restored without rejoining after device power cycle
// this is intrinsically done when calling `beginOTAA()` with the same keys
Expand All @@ -116,7 +120,7 @@ void setup() {
/*
Serial.print(F("[LoRaWAN] Resuming previous session ... "));
state = node.restore();
if(state == RADIOLIB_ERR_NONE) {
if(state >= RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Expand All @@ -130,7 +134,7 @@ void setup() {

// set a fixed datarate
node.setDatarate(5);
// in order to save the datarate persistent across reboot/deepsleep, use the following:
// in order to set the datarate persistent across reboot/deepsleep, use the following:
/*
node.setDatarate(5, true);
*/
Expand All @@ -148,10 +152,10 @@ void setup() {
node.setDutyCycle(true, 1250);

// enable or disable the dwell time limits
// the second argument specific allowed airtime per uplink in milliseconds
// if not called, this corresponds to setDwellTime(true, 0)
// the second argument specifies the allowed airtime per uplink in milliseconds
// unless specified, this argument is set to 0
// setting this to 0 corresponds to the band's maximum allowed dwell time by law
node.setDwellTime(true, 1000);
node.setDwellTime(true, 400);
}

void loop() {
Expand All @@ -170,14 +174,14 @@ void loop() {
uint32_t fcntUp = node.getFcntUp();

Serial.print(F("[LoRaWAN] Sending uplink packet ... "));
String strUp = "Hello World! #" + String(fcntUp);
String strUp = "Hello!" + String(fcntUp);

// send a confirmed uplink to port 10 every 64th frame
// and also request the LinkCheck and DeviceTime MAC commands
if(fcntUp % 64 == 0) {
state = node.uplink(strUp, 10, true);
node.sendMacCommandReq(RADIOLIB_LORAWAN_MAC_LINK_CHECK);
node.sendMacCommandReq(RADIOLIB_LORAWAN_MAC_DEVICE_TIME);
state = node.uplink(strUp, 10, true);
} else {
state = node.uplink(strUp, 10);
}
Expand Down Expand Up @@ -239,7 +243,7 @@ void loop() {
Serial.print(F("[LoRaWAN] Confirming:\t"));
Serial.println(event.confirming);
Serial.print(F("[LoRaWAN] Datarate:\t"));
Serial.print(event.datarate);
Serial.println(event.datarate);
Serial.print(F("[LoRaWAN] Frequency:\t"));
Serial.print(event.freq, 3);
Serial.println(F(" MHz"));
Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ setTxPower KEYWORD2
setCSMA KEYWORD2
getMacLinkCheckAns KEYWORD2
getMacDeviceTimeAns KEYWORD2
getDevAddr KEYWORD2

#######################################
# Constants (LITERAL1)
Expand Down
3 changes: 3 additions & 0 deletions src/protocols/LoRaWAN/LoRaWAN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2873,6 +2873,9 @@ int16_t LoRaWANNode::getMacDeviceTimeAns(uint32_t* gpsEpoch, uint8_t* fraction,
return(RADIOLIB_ERR_NONE);
}

uint64_t LoRaWANNode::getDevAddr() {
return(this->devAddr);
}

// The following function enables LMAC, a CSMA scheme for LoRa as specified
// in the LoRa Alliance Technical Recommendation #13.
Expand Down
6 changes: 6 additions & 0 deletions src/protocols/LoRaWAN/LoRaWAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,12 @@ class LoRaWANNode {
*/
int16_t getMacDeviceTimeAns(uint32_t* gpsEpoch, uint8_t* fraction, bool returnUnix = true);

/*!
\brief Returns the DevAddr of the device, regardless of OTAA or ABP mode
\returns 8-byte DevAddr
*/
uint64_t getDevAddr();

#if !RADIOLIB_GODMODE
private:
#endif
Expand Down

0 comments on commit 0ea00fa

Please sign in to comment.