LoRaWAN 01c: Configure module JP/KR
This example shows how to configure the module and all general settings related to back-end registration process.

Required Materials

1 x Waspmote 1 x Battery 1 x LoRaWAN JP/KR module 1 x 900 MHz Antenna

Code

1
/*
2
------ LoRaWAN Code Example --------
3
4
Explanation: This example shows how to configure the module
5
and all general settings related to back-end registration
6
process.
7
8
Copyright (C) 2019 Libelium Comunicaciones Distribuidas S.L.
9
http://www.libelium.com
10
11
This program is free software: you can redistribute it and/or modify
12
it under the terms of the GNU General Public License as published by
13
the Free Software Foundation, either version 3 of the License, or
14
(at your option) any later version.
15
16
This program is distributed in the hope that it will be useful,
17
but WITHOUT ANY WARRANTY; without even the implied warranty of
18
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
GNU General Public License for more details.
20
21
You should have received a copy of the GNU General Public License
22
along with this program. If not, see .
23
24
Version: 3.0
25
Design: David Gascon
26
Implementation: Luismi Marti
27
*/
28
29
#include <WaspLoRaWAN.h>
30
31
//////////////////////////////////////////////
32
uint8_t socket = SOCKET0;
33
//////////////////////////////////////////////
34
35
// Device parameters for Back-End registration
36
////////////////////////////////////////////////////////////
37
char DEVICE_ADDR[] = "05060708";
38
char NWK_SESSION_KEY[] = "01020304050607080910111213141516";
39
char APP_SESSION_KEY[] = "000102030405060708090A0B0C0D0E0F";
40
char APP_KEY[] = "000102030405060708090A0B0C0D0E0F";
41
////////////////////////////////////////////////////////////
42
43
// variable
44
uint8_t error;
45
46
47
48
void setup()
49
{
50
USB.ON();
51
USB.println(F("LoRaWAN example - Module configuration"));
52
53
54
USB.println(F(" _____________________________________________________"));
55
USB.println(F("| |"));
56
USB.println(F("| It is not mandatory to configure channel parameters.|"));
57
USB.println(F("| Server should configure the module during the |"));
58
USB.println(F("| Over The Air Activation process. |"));
59
USB.println(F("|_____________________________________________________|"));
60
USB.println();
61
62
//////////////////////////////////////////////
63
// 1. switch on
64
//////////////////////////////////////////////
65
66
error = LoRaWAN.ON(socket);
67
68
// Check status
69
if ( error == 0 )
70
{
71
USB.println(F("1. Switch ON OK"));
72
}
73
else
74
{
75
USB.print(F("1. Switch ON error = "));
76
USB.println(error, DEC);
77
}
78
79
if (LoRaWAN._version != ABZ_MODULE)
80
{
81
USB.println(F("\n---------------------------------------------------------------"));
82
USB.println(F("Module version error"));
83
USB.println(F("This is not a JP/KR LoRaWAN module"));
84
USB.println(F("Please open the example 01a or 01b related to your module"));
85
USB.println(F("\n---------------------------------------------------------------"));
86
while (1);
87
}
88
89
90
//////////////////////////////////////////////
91
// 2. Reset to factory default values
92
//////////////////////////////////////////////
93
94
error = LoRaWAN.factoryReset();
95
96
// Check status
97
if ( error == 0 )
98
{
99
USB.println(F("2. Reset to factory default values OK"));
100
}
101
else
102
{
103
USB.print(F("2. Reset to factory error = "));
104
USB.println(error, DEC);
105
}
106
107
108
//////////////////////////////////////////////
109
// 3. Get Device EUI
110
// Device EUI is hard coded in production line. End customer
111
// should NOT modify device EUI. Device EUI will be not be
112
// restored in factoryReset() function
113
//////////////////////////////////////////////
114
115
// Get Device EUI
116
error = LoRaWAN.getDeviceEUI();
117
118
// Check status
119
if ( error == 0 )
120
{
121
USB.print(F("3.1. Get Device EUI OK. "));
122
USB.print(F("Device EUI: "));
123
USB.println(LoRaWAN._devEUI);
124
}
125
else
126
{
127
USB.print(F("3.1. Get Device EUI error = "));
128
USB.println(error, DEC);
129
}
130
131
132
//////////////////////////////////////////////
133
// 4. Set/Get Device Address
134
//////////////////////////////////////////////
135
136
// Set Device Address
137
error = LoRaWAN.setDeviceAddr(DEVICE_ADDR);
138
139
// Check status
140
if ( error == 0 )
141
{
142
USB.println(F("4.1. Set Device address OK"));
143
}
144
else
145
{
146
USB.print(F("4.1. Set Device address error = "));
147
USB.println(error, DEC);
148
}
149
150
// Get Device Address
151
error = LoRaWAN.getDeviceAddr();
152
153
// Check status
154
if ( error == 0 )
155
{
156
USB.print(F("4.2. Get Device address OK. "));
157
USB.print(F("Device address: "));
158
USB.println(LoRaWAN._devAddr);
159
}
160
else
161
{
162
USB.print(F("4.2. Get Device address error = "));
163
USB.println(error, DEC);
164
}
165
166
167
//////////////////////////////////////////////
168
// 5. Set Network Session Key
169
//////////////////////////////////////////////
170
171
error = LoRaWAN.setNwkSessionKey(NWK_SESSION_KEY);
172
173
// Check status
174
if ( error == 0 )
175
{
176
USB.println(F("5. Set Network Session Key OK"));
177
}
178
else
179
{
180
USB.print(F("5. Set Network Session Key error = "));
181
USB.println(error, DEC);
182
}
183
184
185
//////////////////////////////////////////////
186
// 6. Set Application Session Key
187
//////////////////////////////////////////////
188
189
error = LoRaWAN.setAppSessionKey(APP_SESSION_KEY);
190
191
// Check status
192
if ( error == 0 )
193
{
194
USB.println(F("6. Set Application Session Key OK"));
195
}
196
else
197
{
198
USB.print(F("6. Set Application Session Key error = "));
199
USB.println(error, DEC);
200
}
201
202
203
//////////////////////////////////////////////
204
// 7. Set retransmissions for uplink confirmed packet
205
//////////////////////////////////////////////
206
207
// set retries
208
error = LoRaWAN.setRetries(7);
209
210
// Check status
211
if ( error == 0 )
212
{
213
USB.println(F("7.1. Set Retransmissions for uplink confirmed packet OK"));
214
}
215
else
216
{
217
USB.print(F("7.1. Set Retransmissions for uplink confirmed packet error = "));
218
USB.println(error, DEC);
219
}
220
221
// Get retries
222
error = LoRaWAN.getRetries();
223
224
// Check status
225
if ( error == 0 )
226
{
227
USB.print(F("7.2. Get Retransmissions for uplink confirmed packet OK. "));
228
USB.print(F("TX retries: "));
229
USB.println(LoRaWAN._retries, DEC);
230
}
231
else
232
{
233
USB.print(F("7.2. Get Retransmissions for uplink confirmed packet error = "));
234
USB.println(error, DEC);
235
}
236
237
238
//////////////////////////////////////////////
239
// 8. Set application key
240
//////////////////////////////////////////////
241
242
error = LoRaWAN.setAppKey(APP_KEY);
243
244
// Check status
245
if ( error == 0 )
246
{
247
USB.println(F("8. Application key set OK"));
248
}
249
else
250
{
251
USB.print(F("8. Application key set error = "));
252
USB.println(error, DEC);
253
}
254
255
256
////////////////////////////////////////////////////////
257
// ___________________________________________________________
258
// | |
259
// | It is mandatory to configure minimum channel parameters. |
260
// | Server should configure the rest of the channel after |
261
// | the Over The Air Activation process. |
262
// |___________________________________________________________|
263
//
264
////////////////////////////////////////////////////////
265
266
//////////////////////////////////////////////
267
// 9. Configure region and minimum channels
268
//////////////////////////////////////////////
269
270
// JAPAN = BAND_JP923 / KOREA = BAND_KR920
271
uint8_t band = BAND_JP923;
272
273
// Set band
274
error = LoRaWAN.setBand(band);
275
276
// Check status
277
if ( error == 0 )
278
{
279
USB.println(F("9.1. Set band OK"));
280
}
281
else
282
{
283
USB.print(F("9.1. Set band error = "));
284
USB.println(error, DEC);
285
}
286
287
// Get band
288
error = LoRaWAN.getBand();
289
290
// Check status
291
if ( error == 0 )
292
{
293
USB.print(F("9.2. Get band OK. "));
294
USB.print(F("Band: "));
295
USB.print(LoRaWAN._bandABZ, DEC);
296
}
297
else
298
{
299
USB.print(F("9.2. Get band error = "));
300
USB.println(error, DEC);
301
}
302
303
switch (LoRaWAN._bandABZ)
304
{
305
case BAND_JP923:
306
USB.println(F(" - Region is AS923 JAPAN"));
307
308
error = LoRaWAN.setChannelFreq(0, 923200000);
309
if ( error == 0 )
310
{
311
USB.print(F("9.3. Set channel frequency OK. "));
312
USB.print(F("Frequency in channel 0: "));
313
USB.println(LoRaWAN._freq[0]);
314
}
315
else
316
{
317
USB.print(F("9.3. Set channel frequency error = "));
318
USB.println(error, DEC);
319
}
320
321
error = LoRaWAN.setChannelFreq(1, 923400000);
322
if ( error == 0 )
323
{
324
USB.print(F("9.3. Set channel frequency OK. "));
325
USB.print(F("Frequency in channel 1: "));
326
USB.println(LoRaWAN._freq[1]);
327
}
328
else
329
{
330
USB.print(F("9.3. Set channel frequency error = "));
331
USB.println(error, DEC);
332
}
333
334
break;
335
336
case BAND_KR920:
337
USB.println(F(" - Region is KR920"));
338
339
error = LoRaWAN.setChannelFreq(0, 922100000);
340
if ( error == 0 )
341
{
342
USB.print(F("9.3. Set channel frequency OK. "));
343
USB.print(F("Frequency in channel 0: "));
344
USB.println(LoRaWAN._freq[0]);
345
}
346
else
347
{
348
USB.print(F("9.3. Set channel frequency error = "));
349
USB.println(error, DEC);
350
}
351
352
error = LoRaWAN.setChannelFreq(1, 922300000);
353
if ( error == 0 )
354
{
355
USB.print(F("9.3. Set channel frequency OK. "));
356
USB.print(F("Frequency in channel 1: "));
357
USB.println(LoRaWAN._freq[1]);
358
}
359
else
360
{
361
USB.print(F("9.3. Set channel frequency error = "));
362
USB.println(error, DEC);
363
}
364
365
error = LoRaWAN.setChannelFreq(2, 922500000);
366
if ( error == 0 )
367
{
368
USB.print(F("9.3. Set channel frequency OK. "));
369
USB.print(F("Frequency in channel 2: "));
370
USB.println(LoRaWAN._freq[2]);
371
}
372
else
373
{
374
USB.print(F("9.3. Set channel frequency error = "));
375
USB.println(error, DEC);
376
}
377
break;
378
379
}
380
381
382
//////////////////////////////////////////////
383
// 10. Set Adaptive Data Rate (recommended)
384
//////////////////////////////////////////////
385
386
// set ADR
387
error = LoRaWAN.setADR("on");
388
389
// Check status
390
if ( error == 0 )
391
{
392
USB.println(F("10.1. Set Adaptive data rate status to on OK"));
393
}
394
else
395
{
396
USB.print(F("10.1. Set Adaptive data rate status to on error = "));
397
USB.println(error, DEC);
398
}
399
400
// Get ADR
401
error = LoRaWAN.getADR();
402
403
// Check status
404
if ( error == 0 )
405
{
406
USB.print(F("10.2. Get Adaptive data rate status OK. "));
407
USB.print(F("Adaptive data rate status: "));
408
if (LoRaWAN._adr == true)
409
{
410
USB.println("on");
411
}
412
else
413
{
414
USB.println("off");
415
}
416
}
417
else
418
{
419
USB.print(F("10.2. Get Adaptive data rate status error = "));
420
USB.println(error, DEC);
421
}
422
423
//////////////////////////////////////////////
424
// 11. Set data format of messages
425
//////////////////////////////////////////////
426
427
// set data format
428
// 0 = text
429
// 1 = hex
430
error = LoRaWAN.setDataFormat(1);
431
432
// Check status
433
if ( error == 0 )
434
{
435
USB.println(F("11.1. Set data format OK"));
436
}
437
else
438
{
439
USB.print(F("11.1. Set data format error = "));
440
USB.println(error, DEC);
441
}
442
443
// Get data format
444
error = LoRaWAN.getDataFormat();
445
446
// Check status
447
if ( error == 0 )
448
{
449
USB.print(F("11.2. Get data format OK. "));
450
USB.print(F("Data format: "));
451
if (LoRaWAN._dFormat == 0)
452
{
453
USB.println("Text");
454
}
455
else
456
{
457
USB.println("Hex");
458
}
459
}
460
else
461
{
462
USB.print(F("11.2. Get data format error = "));
463
USB.println(error, DEC);
464
}
465
466
467
USB.println(F("------------------------------------"));
468
USB.println(F("Now the LoRaWAN module is ready for"));
469
USB.println(F("joining networks and send messages."));
470
USB.println(F("Please check the next examples..."));
471
USB.println(F("------------------------------------\n"));
472
473
}
474
475
476
void loop()
477
{
478
// do nothing
479
}
Copied!

Output

1
J#
2
LoRaWAN example - Module configuration
3
_____________________________________________________
4
| |
5
| It is not mandatory to configure channel parameters.|
6
| Server should configure the module during the |
7
| Over The Air Activation process. |
8
|_____________________________________________________|
9
10
1. Switch ON OK
11
2. Reset to factory default values OK
12
3.1. Get Device EUI OK. Device EUI: 303931356F387B02
13
4.1. Set Device address OK
14
4.2. Get Device address OK. Device address: 05060708
15
5. Set Network Session Key OK
16
6. Set Application Session Key OK
17
7.1. Set Retransmissions for uplink confirmed packet OK
18
7.2. Get Retransmissions for uplink confirmed packet OK. TX retries: 7
19
8. Application key set OK
20
9.1. Set band OK
21
9.2. Get band OK. Band: 0 - Region is AS923 JAPAN
22
9.3. Set channel frequency OK. Frequency in channel 0: 923200000
23
9.3. Set channel frequency OK. Frequency in channel 1: 923400000
24
10.1. Set Adaptive data rate status to on OK
25
10.2. Get Adaptive data rate status OK. Adaptive data rate status: on
26
11.1. Set data format OK
27
11.2. Get data format OK. Data format: Hex
28
------------------------------------
29
Now the LoRaWAN module is ready for
30
joining networks and send messages.
31
Please check the next examples...
32
------------------------------------
Copied!
Last modified 1yr ago