Skip to content

Commit 85b786e

Browse files
committed
Update RadioLib example
1 parent b097561 commit 85b786e

File tree

5 files changed

+221
-189
lines changed

5 files changed

+221
-189
lines changed

examples/radio/SX1262/LoRaWAN/LoRaWAN_ABP/LoRaWAN_ABP.ino

Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,27 @@ const uint32_t uplinkIntervalSeconds = 5UL * 60UL; // minutes x seconds
3636
// device address - either a development address or one assigned
3737
// to the LoRaWAN Service Provider - TTN will generate one for you
3838
#ifndef RADIOLIB_LORAWAN_DEV_ADDR // Replace with your DevAddr
39-
#define RADIOLIB_LORAWAN_DEV_ADDR 0x12345678
39+
#define RADIOLIB_LORAWAN_DEV_ADDR 0x260D55DD
4040
#endif
4141

4242
#ifndef RADIOLIB_LORAWAN_FNWKSINT_KEY // Replace with your FNwkSInt Key
43-
#define RADIOLIB_LORAWAN_FNWKSINT_KEY 0x74, 0x6F, 0x70, 0x53, 0x65, 0x63, 0x72, 0x65,0x74, 0x4B, 0x65, 0x79, 0x31, 0x32, 0x33, 0x34
43+
#define RADIOLIB_LORAWAN_FNWKSINT_KEY 0x24, 0x83, 0x65, 0x51, 0x23, 0x32, 0x82, 0x47, 0xEF, 0x16, 0x4D, 0x7B, 0x0C, 0xB2, 0xC5, 0x96
4444
#endif
4545
#ifndef RADIOLIB_LORAWAN_SNWKSINT_KEY // Replace with your SNwkSInt Key
46-
#define RADIOLIB_LORAWAN_SNWKSINT_KEY 0x74, 0x6F, 0x70, 0x53, 0x65, 0x63, 0x72, 0x65,0x74, 0x4B, 0x65, 0x79, 0x31, 0x32, 0x33, 0x34
46+
#define RADIOLIB_LORAWAN_SNWKSINT_KEY 0xAD, 0xD1, 0x50, 0x62, 0x28, 0x8F, 0xEE, 0xA4, 0xD5, 0x6B, 0x6B, 0xF3, 0x56, 0x3D, 0x5A, 0x90
4747
#endif
4848
#ifndef RADIOLIB_LORAWAN_NWKSENC_KEY // Replace with your NwkSEnc Key
49-
#define RADIOLIB_LORAWAN_NWKSENC_KEY 0x74, 0x6F, 0x70, 0x53, 0x65, 0x63, 0x72, 0x65,0x74, 0x4B, 0x65, 0x79, 0x31, 0x32, 0x33, 0x34
49+
#define RADIOLIB_LORAWAN_NWKSENC_KEY 0xFE, 0x9D, 0x84, 0xD9, 0x4A, 0x66, 0x69, 0x08, 0x01, 0x7B, 0x17, 0xE5, 0x38, 0x53, 0x7F, 0xD9
5050
#endif
5151
#ifndef RADIOLIB_LORAWAN_APPS_KEY // Replace with your AppS Key
52-
#define RADIOLIB_LORAWAN_APPS_KEY 0x74, 0x6F, 0x70, 0x53, 0x65, 0x63, 0x72, 0x65,0x74, 0x4B, 0x65, 0x79, 0x31, 0x32, 0x33, 0x34
52+
#define RADIOLIB_LORAWAN_APPS_KEY 0xF4, 0x29, 0xA2, 0x5E, 0xE5, 0x49, 0x97, 0x50, 0x96, 0xB3, 0x31, 0x22, 0x34, 0xFD, 0x08, 0x5B
5353
#endif
5454

5555
// for the curious, the #ifndef blocks allow for automated testing &/or you can
5656
// put your EUI & keys in to your platformio.ini - see wiki for more tips
5757

5858
// regional choices: EU868, US915, AU915, AS923, IN865, KR920, CN780, CN500
59-
const LoRaWANBand_t Region = EU868;
59+
const LoRaWANBand_t Region = CN500;
6060
const uint8_t subBand = 0; // For US915, change this to 2, otherwise leave on 0
6161

6262

@@ -83,22 +83,13 @@ void setup()
8383

8484
watch.begin();
8585

86-
// initialize SX1262 with default settings
87-
Serial.print(F("[SX1262] Initializing ... "));
86+
Serial.println(F("Initialise the radio"));
8887
int state = radio.begin();
89-
if (state == RADIOLIB_ERR_NONE) {
90-
Serial.println(F("success!"));
91-
} else {
92-
Serial.print(F("failed, code "));
93-
Serial.println(state);
94-
while (true);
95-
}
88+
debug(state != RADIOLIB_ERR_NONE, F("Initialise radio failed"), state, true);
9689

97-
// start the device by directly providing the encryption keys and device address
98-
Serial.print(F("[LoRaWAN] Attempting over-the-air activation ... "));
90+
Serial.println(F("Initialise LoRaWAN Network credentials"));
9991
node.beginABP(devAddr, fNwkSIntKey, sNwkSIntKey, nwkSEncKey, appSKey);
10092

101-
10293
node.activateABP();
10394
debug(state != RADIOLIB_ERR_NONE, F("Activate ABP failed"), state, true);
10495

@@ -125,30 +116,28 @@ void loop()
125116

126117
// Perform an uplink
127118
int state = node.sendReceive(uplinkPayload, sizeof(uplinkPayload));
128-
debug((state != RADIOLIB_LORAWAN_NO_DOWNLINK) && (state != RADIOLIB_ERR_NONE), F("Error in sendReceive"), state, false);
129-
130-
// Wait until next uplink - observing legal & TTN FUP constraints
131-
delay(uplinkIntervalSeconds * 1000UL);
132-
}
119+
debug(state < RADIOLIB_ERR_NONE, F("Error in sendReceive"), state, false);
133120

121+
// Check if a downlink was received
122+
// (state 0 = no downlink, state 1/2 = downlink in window Rx1/Rx2)
123+
if (state > 0) {
124+
Serial.println(F("Received a downlink"));
125+
} else {
126+
Serial.println(F("No downlink received"));
127+
}
134128

129+
Serial.print(F("Next uplink in "));
130+
Serial.print(uplinkIntervalSeconds);
131+
Serial.println(F(" seconds\n"));
135132

136-
// helper function to display any issues
137-
void debug(bool isFail, const __FlashStringHelper *message, int state, bool Freeze)
138-
{
139-
if (isFail) {
140-
Serial.print(message);
141-
Serial.print(" - ");
142-
Serial.print(stateDecode(state));
143-
Serial.print(" (");
144-
Serial.print(state);
145-
Serial.println(")");
146-
while (Freeze);
147-
}
133+
// Wait until next uplink - observing legal & TTN FUP constraints
134+
delay(uplinkIntervalSeconds * 1000UL); // delay needs milli-seconds
148135
}
149136

150137

151-
// result code to text ...
138+
139+
// result code to text - these are error codes that can be raised when using LoRaWAN
140+
// however, RadioLib has many more - see https://jgromes.github.io/RadioLib/group__status__codes.html for a complete list
152141
String stateDecode(const int16_t result)
153142
{
154143
switch (result) {
@@ -174,7 +163,6 @@ String stateDecode(const int16_t result)
174163
return "ERR_INVALID_OUTPUT_POWER";
175164
case RADIOLIB_ERR_NETWORK_NOT_JOINED:
176165
return "RADIOLIB_ERR_NETWORK_NOT_JOINED";
177-
178166
case RADIOLIB_ERR_DOWNLINK_MALFORMED:
179167
return "RADIOLIB_ERR_DOWNLINK_MALFORMED";
180168
case RADIOLIB_ERR_INVALID_REVISION:
@@ -201,16 +189,45 @@ String stateDecode(const int16_t result)
201189
return "RADIOLIB_ERR_DWELL_TIME_EXCEEDED";
202190
case RADIOLIB_ERR_CHECKSUM_MISMATCH:
203191
return "RADIOLIB_ERR_CHECKSUM_MISMATCH";
204-
case RADIOLIB_LORAWAN_NO_DOWNLINK:
205-
return "RADIOLIB_LORAWAN_NO_DOWNLINK";
192+
case RADIOLIB_ERR_NO_JOIN_ACCEPT:
193+
return "RADIOLIB_ERR_NO_JOIN_ACCEPT";
206194
case RADIOLIB_LORAWAN_SESSION_RESTORED:
207195
return "RADIOLIB_LORAWAN_SESSION_RESTORED";
208196
case RADIOLIB_LORAWAN_NEW_SESSION:
209197
return "RADIOLIB_LORAWAN_NEW_SESSION";
210-
case RADIOLIB_LORAWAN_NONCES_DISCARDED:
211-
return "RADIOLIB_LORAWAN_NONCES_DISCARDED";
212-
case RADIOLIB_LORAWAN_SESSION_DISCARDED:
213-
return "RADIOLIB_LORAWAN_SESSION_DISCARDED";
198+
case RADIOLIB_ERR_NONCES_DISCARDED:
199+
return "RADIOLIB_ERR_NONCES_DISCARDED";
200+
case RADIOLIB_ERR_SESSION_DISCARDED:
201+
return "RADIOLIB_ERR_SESSION_DISCARDED";
202+
}
203+
return "See https://jgromes.github.io/RadioLib/group__status__codes.html";
204+
}
205+
206+
// helper function to display any issues
207+
void debug(bool failed, const __FlashStringHelper *message, int state, bool halt)
208+
{
209+
if (failed) {
210+
Serial.print(message);
211+
Serial.print(" - ");
212+
Serial.print(stateDecode(state));
213+
Serial.print(" (");
214+
Serial.print(state);
215+
Serial.println(")");
216+
while (halt) {
217+
delay(1);
218+
}
214219
}
215-
return "See TypeDef.h";
216-
}
220+
}
221+
222+
// helper function to display a byte array
223+
void arrayDump(uint8_t *buffer, uint16_t len)
224+
{
225+
for (uint16_t c = 0; c < len; c++) {
226+
char b = buffer[c];
227+
if (b < 0x10) {
228+
Serial.print('0');
229+
}
230+
Serial.print(b, HEX);
231+
}
232+
Serial.println();
233+
}
Lines changed: 20 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/*
22
RadioLib LoRaWAN Starter Example
33
4+
! Please refer to the included notes to get started !
5+
46
This example joins a LoRaWAN network and will send
57
uplink packets. Before you start, you will have to
68
register your device at https://www.thethingsnetwork.org/
@@ -21,69 +23,29 @@
2123
https://github.com/jgromes/RadioLib/wiki/LoRaWAN
2224
2325
*/
24-
// include the library
25-
#include <LilyGoLib.h>
26-
#include <LV_Helper.h>
27-
28-
// how often to send an uplink - consider legal & FUP constraints - see notes
29-
const uint32_t uplinkIntervalSeconds = 5UL * 60UL; // minutes x seconds
30-
31-
// joinEUI - previous versions of LoRaWAN called this AppEUI
32-
// for development purposes you can use all zeros - see wiki for details
33-
#define RADIOLIB_LORAWAN_JOIN_EUI 0x0000000000000000
34-
35-
// the Device EUI & two keys can be generated on the TTN console
36-
#ifndef RADIOLIB_LORAWAN_DEV_EUI // Replace with your Device EUI
37-
#define RADIOLIB_LORAWAN_DEV_EUI 0x12345678
38-
#endif
39-
#ifndef RADIOLIB_LORAWAN_APP_KEY // Replace with your App Key
40-
#define RADIOLIB_LORAWAN_APP_KEY 0x74, 0x6F, 0x70, 0x53, 0x65, 0x63, 0x72, 0x65,0x74, 0x4B, 0x65, 0x79, 0x31, 0x32, 0x33, 0x34
41-
#endif
42-
#ifndef RADIOLIB_LORAWAN_NWK_KEY // Put your Nwk Key here
43-
#define RADIOLIB_LORAWAN_NWK_KEY 0x74, 0x6F, 0x70, 0x53, 0x65, 0x63, 0x72, 0x65,0x74, 0x4B, 0x65, 0x79, 0x31, 0x32, 0x33, 0x34
44-
#endif
45-
46-
// for the curious, the #ifndef blocks allow for automated testing &/or you can
47-
// put your EUI & keys in to your platformio.ini - see wiki for more tips
48-
49-
// regional choices: EU868, US915, AU915, AS923, IN865, KR920, CN780, CN500
50-
const LoRaWANBand_t Region = EU868;
51-
const uint8_t subBand = 0; // For US915, change this to 2, otherwise leave on 0
52-
5326

54-
// copy over the EUI's & keys in to the something that will not compile if incorrectly formatted
55-
uint64_t joinEUI = RADIOLIB_LORAWAN_JOIN_EUI;
56-
uint64_t devEUI = RADIOLIB_LORAWAN_DEV_EUI;
57-
uint8_t appKey[] = { RADIOLIB_LORAWAN_APP_KEY };
58-
uint8_t nwkKey[] = { RADIOLIB_LORAWAN_NWK_KEY };
5927

60-
61-
SX1262 radio = newModule();
62-
63-
// create the LoRaWAN node
64-
LoRaWANNode node(&radio, &Region, subBand);
65-
66-
67-
String stateDecode(const int16_t result);
68-
void debug(bool isFail, const __FlashStringHelper *message, int state, bool Freeze);
28+
#include "config.h"
6929

7030

7131
void setup()
7232
{
7333
Serial.begin(115200);
34+
35+
watch.begin();
36+
7437
while (!Serial);
38+
7539
delay(5000); // Give time to switch to the serial monitor
7640
Serial.println(F("\nSetup ... "));
7741

78-
79-
watch.begin();
80-
8142
Serial.println(F("Initialise the radio"));
8243
int16_t state = radio.begin();
8344
debug(state != RADIOLIB_ERR_NONE, F("Initialise radio failed"), state, true);
8445

8546
// Setup the OTAA session information
86-
node.beginOTAA(joinEUI, devEUI, nwkKey, appKey);
47+
state = node.beginOTAA(joinEUI, devEUI, nwkKey, appKey);
48+
debug(state != RADIOLIB_ERR_NONE, F("Initialise node failed"), state, true);
8749

8850
Serial.println(F("Join ('login') the LoRaWAN Network"));
8951
state = node.activateOTAA();
@@ -109,104 +71,20 @@ void loop()
10971

11072
// Perform an uplink
11173
int16_t state = node.sendReceive(uplinkPayload, sizeof(uplinkPayload));
112-
debug((state != RADIOLIB_LORAWAN_NO_DOWNLINK) && (state != RADIOLIB_ERR_NONE), F("Error in sendReceive"), state, false);
74+
debug(state < RADIOLIB_ERR_NONE, F("Error in sendReceive"), state, false);
11375

114-
Serial.print(F("Uplink complete, next in "));
76+
// Check if a downlink was received
77+
// (state 0 = no downlink, state 1/2 = downlink in window Rx1/Rx2)
78+
if (state > 0) {
79+
Serial.println(F("Received a downlink"));
80+
} else {
81+
Serial.println(F("No downlink received"));
82+
}
83+
84+
Serial.print(F("Next uplink in "));
11585
Serial.print(uplinkIntervalSeconds);
116-
Serial.println(F(" seconds"));
86+
Serial.println(F(" seconds\n"));
11787

11888
// Wait until next uplink - observing legal & TTN FUP constraints
11989
delay(uplinkIntervalSeconds * 1000UL); // delay needs milli-seconds
12090
}
121-
122-
123-
124-
// result code to text ...
125-
String stateDecode(const int16_t result) {
126-
switch (result) {
127-
case RADIOLIB_ERR_NONE:
128-
return "ERR_NONE";
129-
case RADIOLIB_ERR_CHIP_NOT_FOUND:
130-
return "ERR_CHIP_NOT_FOUND";
131-
case RADIOLIB_ERR_PACKET_TOO_LONG:
132-
return "ERR_PACKET_TOO_LONG";
133-
case RADIOLIB_ERR_RX_TIMEOUT:
134-
return "ERR_RX_TIMEOUT";
135-
case RADIOLIB_ERR_CRC_MISMATCH:
136-
return "ERR_CRC_MISMATCH";
137-
case RADIOLIB_ERR_INVALID_BANDWIDTH:
138-
return "ERR_INVALID_BANDWIDTH";
139-
case RADIOLIB_ERR_INVALID_SPREADING_FACTOR:
140-
return "ERR_INVALID_SPREADING_FACTOR";
141-
case RADIOLIB_ERR_INVALID_CODING_RATE:
142-
return "ERR_INVALID_CODING_RATE";
143-
case RADIOLIB_ERR_INVALID_FREQUENCY:
144-
return "ERR_INVALID_FREQUENCY";
145-
case RADIOLIB_ERR_INVALID_OUTPUT_POWER:
146-
return "ERR_INVALID_OUTPUT_POWER";
147-
case RADIOLIB_ERR_NETWORK_NOT_JOINED:
148-
return "RADIOLIB_ERR_NETWORK_NOT_JOINED";
149-
150-
case RADIOLIB_ERR_DOWNLINK_MALFORMED:
151-
return "RADIOLIB_ERR_DOWNLINK_MALFORMED";
152-
case RADIOLIB_ERR_INVALID_REVISION:
153-
return "RADIOLIB_ERR_INVALID_REVISION";
154-
case RADIOLIB_ERR_INVALID_PORT:
155-
return "RADIOLIB_ERR_INVALID_PORT";
156-
case RADIOLIB_ERR_NO_RX_WINDOW:
157-
return "RADIOLIB_ERR_NO_RX_WINDOW";
158-
case RADIOLIB_ERR_INVALID_CID:
159-
return "RADIOLIB_ERR_INVALID_CID";
160-
case RADIOLIB_ERR_UPLINK_UNAVAILABLE:
161-
return "RADIOLIB_ERR_UPLINK_UNAVAILABLE";
162-
case RADIOLIB_ERR_COMMAND_QUEUE_FULL:
163-
return "RADIOLIB_ERR_COMMAND_QUEUE_FULL";
164-
case RADIOLIB_ERR_COMMAND_QUEUE_ITEM_NOT_FOUND:
165-
return "RADIOLIB_ERR_COMMAND_QUEUE_ITEM_NOT_FOUND";
166-
case RADIOLIB_ERR_JOIN_NONCE_INVALID:
167-
return "RADIOLIB_ERR_JOIN_NONCE_INVALID";
168-
case RADIOLIB_ERR_N_FCNT_DOWN_INVALID:
169-
return "RADIOLIB_ERR_N_FCNT_DOWN_INVALID";
170-
case RADIOLIB_ERR_A_FCNT_DOWN_INVALID:
171-
return "RADIOLIB_ERR_A_FCNT_DOWN_INVALID";
172-
case RADIOLIB_ERR_DWELL_TIME_EXCEEDED:
173-
return "RADIOLIB_ERR_DWELL_TIME_EXCEEDED";
174-
case RADIOLIB_ERR_CHECKSUM_MISMATCH:
175-
return "RADIOLIB_ERR_CHECKSUM_MISMATCH";
176-
case RADIOLIB_LORAWAN_NO_DOWNLINK:
177-
return "RADIOLIB_LORAWAN_NO_DOWNLINK";
178-
case RADIOLIB_LORAWAN_SESSION_RESTORED:
179-
return "RADIOLIB_LORAWAN_SESSION_RESTORED";
180-
case RADIOLIB_LORAWAN_NEW_SESSION:
181-
return "RADIOLIB_LORAWAN_NEW_SESSION";
182-
case RADIOLIB_LORAWAN_NONCES_DISCARDED:
183-
return "RADIOLIB_LORAWAN_NONCES_DISCARDED";
184-
case RADIOLIB_LORAWAN_SESSION_DISCARDED:
185-
return "RADIOLIB_LORAWAN_SESSION_DISCARDED";
186-
}
187-
return "See TypeDef.h";
188-
}
189-
190-
// helper function to display any issues
191-
void debug(bool isFail, const __FlashStringHelper* message, int state, bool Freeze) {
192-
if (isFail) {
193-
Serial.print(message);
194-
Serial.print(" - ");
195-
Serial.print(stateDecode(state));
196-
Serial.print(" (");
197-
Serial.print(state);
198-
Serial.println(")");
199-
while (Freeze);
200-
}
201-
}
202-
203-
204-
// helper function to display a byte array
205-
void arrayDump(uint8_t *buffer, uint16_t len) {
206-
for(uint16_t c = 0; c < len; c++) {
207-
char b = buffer[c];
208-
if(b < 0x10) { Serial.print('0'); }
209-
Serial.print(b, HEX);
210-
}
211-
Serial.println();
212-
}

0 commit comments

Comments
 (0)