Code examples and extended information
In the Waspmote Development section you can find complete examples:
Waspmote development Wiki
Waspmote
Example:
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) 2016 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 <http://www.gnu.org/licenses/>.
23
*
24
* Version: 3.1
25
* Design: David Gascon
26
* Implementation: Luis Miguel Marti
27
*/
28
#include <WaspLoRaWAN.h>
29
//////////////////////////////////////////////
30
uint8_t socket = SOCKET0;
31
//////////////////////////////////////////////
32
// Device parameters for Back-End registration
33
////////////////////////////////////////////////////////////
34
char DEVICE_EUI[] =0102030405060708;
35
char DEVICE_ADDR[] =05060708;
36
char NWK_SESSION_KEY[] =01020304050607080910111213141516;
37
char APP_SESSION_KEY[] =000102030405060708090A0B0C0D0E0F”;
38
char APP_KEY[] =000102030405060708090A0B0C0D0E0F”;
39
////////////////////////////////////////////////////////////
40
// variable
41
uint8_t error;
42
void setup()
43
{
44
USB.ON();
45
USB.println(F(“LoRaWAN example - Module configuration”));
46
USB.println(F(“ _____________________________________________________”));
47
USB.println(F(| |));
48
USB.println(F(| It is not mandatory to configure channel parameters.|));
49
USB.println(F(| Server should configure the module during the |));
50
USB.println(F(| Over The Air Activation process. |));
51
USB.println(F(|_____________________________________________________|));
52
USB.println();
53
//////////////////////////////////////////////
54
// 1. switch on
55
//////////////////////////////////////////////
56
error = LoRaWAN.ON(socket);
57
// Check status
58
if( error == 0 )
59
{
60
USB.println(F(1. Switch ON OK”));
61
}
62
else
63
{
64
USB.print(F(1. Switch ON error =));
65
USB.println(error, DEC);
66
}
67
//////////////////////////////////////////////
68
// 2. Reset to factory default values
69
//////////////////////////////////////////////
70
error = LoRaWAN.factoryReset();
71
// Check status
72
if( error == 0 )
73
{
74
USB.println(F(2. Reset to factory default values OK”));
75
}
76
else
77
{
78
USB.print(F(2. Reset to factory error =));
79
USB.println(error, DEC);
80
}
81
//////////////////////////////////////////////
82
// 3. Set/Get Device EUI
83
//////////////////////////////////////////////
84
// Set Device EUI
85
error = LoRaWAN.setDeviceEUI(DEVICE_EUI);
86
// Check status
87
if( error == 0 )
88
{
89
USB.println(F(3.1. Set Device EUI OK”));
90
}
91
else
92
{
93
USB.print(F(3.1. Set Device EUI error =));
94
USB.println(error, DEC);
95
}
96
// Get Device EUI
97
error = LoRaWAN.getDeviceEUI();
98
// Check status
99
if( error == 0 )
100
{
101
USB.print(F(3.2. Get Device EUI OK.));
102
USB.print(F(“Device EUI:));
103
USB.println(LoRaWAN._devEUI);
104
}
105
else
106
{
107
USB.print(F(3.2. Get Device EUI error =));
108
USB.println(error, DEC);
109
}
110
//////////////////////////////////////////////
111
// 4. Set/Get Device Address
112
//////////////////////////////////////////////
113
// Set Device Address
114
error = LoRaWAN.setDeviceAddr(DEVICE_ADDR);
115
// Check status
116
if( error == 0 )
117
{
118
USB.println(F(4.1. Set Device address OK”));
119
}
120
else
121
{
122
USB.print(F(4.1. Set Device address error =));
123
USB.println(error, DEC);
124
}
125
// Get Device Address
126
error = LoRaWAN.getDeviceAddr();
127
// Check status
128
if( error == 0 )
129
{
130
USB.print(F(4.2. Get Device address OK.));
131
USB.print(F(“Device address:));
132
USB.println(LoRaWAN._devAddr);
133
}
134
else
135
{
136
USB.print(F(4.2. Get Device address error =));
137
USB.println(error, DEC);
138
}
139
//////////////////////////////////////////////
140
// 5. Set Network Session Key
141
//////////////////////////////////////////////
142
error = LoRaWAN.setNwkSessionKey(NWK_SESSION_KEY);
143
// Check status
144
if( error == 0 )
145
{
146
USB.println(F(5. Set Network Session Key OK”));
147
}
148
else
149
{
150
USB.print(F(5. Set Network Session Key error =));
151
USB.println(error, DEC);
152
}
153
//////////////////////////////////////////////
154
// 6. Set Application Session Key
155
//////////////////////////////////////////////
156
error = LoRaWAN.setAppSessionKey(APP_SESSION_KEY);
157
// Check status
158
if( error == 0 )
159
{
160
USB.println(F(6. Set Application Session Key OK”));
161
}
162
else
163
{
164
USB.print(F(6. Set Application Session Key error =));
165
USB.println(error, DEC);
166
}
167
//////////////////////////////////////////////
168
// 7. Set retransmissions for uplink confirmed packet
169
//////////////////////////////////////////////
170
// set retries
171
error = LoRaWAN.setRetries(7);
172
// Check status
173
if( error == 0 )
174
{
175
USB.println(F(7.1. Set Retransmissions for uplink confirmed packet OK”));
176
}
177
else
178
{
179
USB.print(F(7.1. Set Retransmissions for uplink confirmed packet error =));
180
USB.println(error, DEC);
181
}
182
// Get retries
183
error = LoRaWAN.getRetries();
184
// Check status
185
if( error == 0 )
186
{
187
USB.print(F(7.2. Get Retransmissions for uplink confirmed packet OK.));
188
USB.print(F(“TX retries:));
189
USB.println(LoRaWAN._retries, DEC);
190
}
191
else
192
{
193
USB.print(F(7.2. Get Retransmissions for uplink confirmed packet error =));
194
USB.println(error, DEC);
195
}
196
//////////////////////////////////////////////
197
// 8. Set application key
198
//////////////////////////////////////////////
199
error = LoRaWAN.setAppKey(APP_KEY);
200
// Check status
201
if( error == 0 )
202
{
203
USB.println(F(8. Application key set OK”));
204
}
205
else
206
{
207
USB.print(F(8. Application key set error =));
208
USB.println(error, DEC);
209
}
210
////////////////////////////////////////////////////////
211
// ______________________________________________________
212
// | |
213
// | It is not mandatory to configure channel parameters.|
214
// | Server should configure the module during the |
215
// | Over The Air Activation process. If channels aren’t |
216
// | configured, please uncomment channel configuration |
217
// | functions below these lines. |
218
// |______________________________________________________|
219
//
220
////////////////////////////////////////////////////////
221
//////////////////////////////////////////////
222
// 9. Channel configuration. (Recommended)
223
// Consult your Network Operator and Backend Provider
224
//////////////////////////////////////////////
225
// Set channel 3 -> 867.1 MHz
226
// Set channel 4 -> 867.3 MHz
227
// Set channel 5 -> 867.5 MHz
228
// Set channel 6 -> 867.7 MHz
229
// Set channel 7 -> 867.9 MHz
230
// uint32_t freq = 867100000;
231
//
232
// for (uint8_t ch = 3; ch <= 7; ch++)
233
// {
234
// error = LoRaWAN.setChannelFreq(ch, freq);
235
// freq += 200000;
236
//
237
// // Check status
238
// if( error == 0 )
239
// {
240
// USB.println(F(“9. Frequency channel set OK”));
241
// }
242
// else
243
// {
244
// USB.print(F(“9. Frequency channel set error = “));
245
// USB.println(error, DEC);
246
// }
247
//
248
//
249
// }
250
//////////////////////////////////////////////
251
// 10. Set Duty Cycle for specific channel. (Recommended)
252
// Consult your Network Operator and Backend Provider
253
//////////////////////////////////////////////
254
// for (uint8_t ch = 0; ch <= 2; ch++)
255
// {
256
// error = LoRaWAN.setChannelDutyCycle(ch, 33333);
257
//
258
// // Check status
259
// if( error == 0 )
260
// {
261
// USB.println(F(“10. Duty cycle channel set OK”));
262
// }
263
// else
264
// {
265
// USB.print(F(“10. Duty cycle channel set error = “));
266
// USB.println(error, DEC);
267
// }
268
// }
269
//
270
// for (uint8_t ch = 3; ch <= 7; ch++)
271
// {
272
// error = LoRaWAN.setChannelDutyCycle(ch, 40000);
273
//
274
// // Check status
275
// if( error == 0 )
276
// {
277
// USB.println(F(“10. Duty cycle channel set OK”));
278
// }
279
// else
280
// {
281
// USB.print(F(“10. Duty cycle channel set error = “));
282
// USB.println(error, DEC);
283
// }
284
// }
285
//////////////////////////////////////////////
286
// 11. Set Data Range for specific channel. (Recommended)
287
// Consult your Network Operator and Backend Provider
288
//////////////////////////////////////////////
289
// for (int ch = 0; ch <= 7; ch++)
290
// {
291
// error = LoRaWAN.setChannelDRRange(ch, 0, 5);
292
//
293
// // Check status
294
// if( error == 0 )
295
// {
296
// USB.println(F(“11. Data rate range channel set OK”));
297
// }
298
// else
299
// {
300
// USB.print(F(“11. Data rate range channel set error = “));
301
// USB.println(error, DEC);
302
// }
303
// }
304
//////////////////////////////////////////////
305
// 12. Set Data rate range for specific channel. (Recommended)
306
// Consult your Network Operator and Backend Provider
307
//////////////////////////////////////////////
308
// for (int ch = 0; ch <= 7; ch++)
309
// {
310
// error = LoRaWAN.setChannelStatus(ch, “on”);
311
//
312
// // Check status
313
// if( error == 0 )
314
// {
315
// USB.println(F(“12. Channel status set OK”));
316
// }
317
// else
318
// {
319
// USB.print(F(“12. Channel status set error = “));
320
// USB.println(error, DEC);
321
// }
322
// }
323
//////////////////////////////////////////////
324
// 13. Set Adaptive Data Rate (recommended)
325
//////////////////////////////////////////////
326
// set ADR
327
error = LoRaWAN.setADR(“on”);
328
// Check status
329
if( error == 0 )
330
{
331
USB.println(F(13.1. Set Adaptive data rate status to on OK”));
332
}
333
else
334
{
335
USB.print(F(13.1. Set Adaptive data rate status to on error =));
336
USB.println(error, DEC);
337
}
338
// Get ADR
339
error = LoRaWAN.getADR();
340
// Check status
341
if( error == 0 )
342
{
343
USB.print(F(13.2. Get Adaptive data rate status OK.));
344
USB.print(F(“Adaptive data rate status:));
345
if (LoRaWAN._adr == true)
346
{
347
USB.println(“on”);
348
}
349
else
350
{
351
USB.println(“off”);
352
}
353
}
354
else
355
{
356
USB.print(F(13.2. Get Adaptive data rate status error =));
357
USB.println(error, DEC);
358
}
359
//////////////////////////////////////////////
360
// 14. Set Automatic Reply
361
//////////////////////////////////////////////
362
// set AR
363
error = LoRaWAN.setAR(“on”);
364
// Check status
365
if( error == 0 )
366
{
367
USB.println(F(14.1. Set automatic reply status to on OK”));
368
}
369
else
370
{
371
USB.print(F(14.1. Set automatic reply status to on error =));
372
USB.println(error, DEC);
373
}
374
// Get AR
375
error = LoRaWAN.getAR();
376
// Check status
377
if( error == 0 )
378
{
379
USB.print(F(14.2. Get automatic reply status OK.));
380
USB.print(F(“Automatic reply status:));
381
if (LoRaWAN._ar == true)
382
{
383
USB.println(“on”);
384
}
385
else
386
{
387
USB.println(“off”);
388
}
389
}
390
else
391
{
392
USB.print(F(14.2. Get automatic reply status error =));
393
USB.println(error, DEC);
394
}
395
//////////////////////////////////////////////
396
// 15. Save configuration
397
//////////////////////////////////////////////
398
error = LoRaWAN.saveConfig();
399
// Check status
400
if( error == 0 )
401
{
402
USB.println(F(15. Save configuration OK”));
403
}
404
else
405
{
406
USB.print(F(15. Save configuration error =));
407
USB.println(error, DEC);
408
}
409
USB.println(F(------------------------------------));
410
USB.println(F(“Now the LoRaWAN module is ready for));
411
USB.println(F(“joining networks and send messages.));
412
USB.println(F(“Please check the next examples...));
413
USB.println(F(------------------------------------\n”));
414
}
415
void loop()
416
{
417
// do nothing
418
}
Copied!
Last modified 1yr ago
Copy link