@@ -36,27 +36,27 @@ const uint32_t uplinkIntervalSeconds = 5UL * 60UL; // minutes x seconds
36
36
// device address - either a development address or one assigned
37
37
// to the LoRaWAN Service Provider - TTN will generate one for you
38
38
#ifndef RADIOLIB_LORAWAN_DEV_ADDR // Replace with your DevAddr
39
- #define RADIOLIB_LORAWAN_DEV_ADDR 0x12345678
39
+ #define RADIOLIB_LORAWAN_DEV_ADDR 0x260D55DD
40
40
#endif
41
41
42
42
#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
44
44
#endif
45
45
#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
47
47
#endif
48
48
#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
50
50
#endif
51
51
#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
53
53
#endif
54
54
55
55
// for the curious, the #ifndef blocks allow for automated testing &/or you can
56
56
// put your EUI & keys in to your platformio.ini - see wiki for more tips
57
57
58
58
// regional choices: EU868, US915, AU915, AS923, IN865, KR920, CN780, CN500
59
- const LoRaWANBand_t Region = EU868 ;
59
+ const LoRaWANBand_t Region = CN500 ;
60
60
const uint8_t subBand = 0 ; // For US915, change this to 2, otherwise leave on 0
61
61
62
62
@@ -83,22 +83,13 @@ void setup()
83
83
84
84
watch.begin ();
85
85
86
- // initialize SX1262 with default settings
87
- Serial.print (F (" [SX1262] Initializing ... " ));
86
+ Serial.println (F (" Initialise the radio" ));
88
87
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 );
96
89
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" ));
99
91
node.beginABP (devAddr, fNwkSIntKey , sNwkSIntKey , nwkSEncKey, appSKey);
100
92
101
-
102
93
node.activateABP ();
103
94
debug (state != RADIOLIB_ERR_NONE, F (" Activate ABP failed" ), state, true );
104
95
@@ -125,30 +116,28 @@ void loop()
125
116
126
117
// Perform an uplink
127
118
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 );
133
120
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
+ }
134
128
129
+ Serial.print (F (" Next uplink in " ));
130
+ Serial.print (uplinkIntervalSeconds);
131
+ Serial.println (F (" seconds\n " ));
135
132
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
148
135
}
149
136
150
137
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
152
141
String stateDecode (const int16_t result)
153
142
{
154
143
switch (result) {
@@ -174,7 +163,6 @@ String stateDecode(const int16_t result)
174
163
return " ERR_INVALID_OUTPUT_POWER" ;
175
164
case RADIOLIB_ERR_NETWORK_NOT_JOINED:
176
165
return " RADIOLIB_ERR_NETWORK_NOT_JOINED" ;
177
-
178
166
case RADIOLIB_ERR_DOWNLINK_MALFORMED:
179
167
return " RADIOLIB_ERR_DOWNLINK_MALFORMED" ;
180
168
case RADIOLIB_ERR_INVALID_REVISION:
@@ -201,16 +189,45 @@ String stateDecode(const int16_t result)
201
189
return " RADIOLIB_ERR_DWELL_TIME_EXCEEDED" ;
202
190
case RADIOLIB_ERR_CHECKSUM_MISMATCH:
203
191
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 " ;
206
194
case RADIOLIB_LORAWAN_SESSION_RESTORED:
207
195
return " RADIOLIB_LORAWAN_SESSION_RESTORED" ;
208
196
case RADIOLIB_LORAWAN_NEW_SESSION:
209
197
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
+ }
214
219
}
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
+ }
0 commit comments