LoRaWAN 06: Join ABP send unconfirmed
This example shows how to configure the module and send packets to a LoRaWAN gateway without ACK

Required Materials

1 x Waspmote 1 x Battery 1 x LoRaWAN EU or US or AU or IN or ASIA-PAC / LATAM module 1 x 868/900 MHz antenna

Notes

- It is strongly recommended to execute the necessary configuration example before using this code - This example can be executed in Waspmote v12 and Waspmote v15

Code

1
/*
2
* ------ LoRaWAN Code Example --------
3
*
4
* Explanation: This example shows how to configure the module
5
* and send packets to a LoRaWAN gateway without ACK after join a network
6
* using ABP
7
*
8
* Copyright (C) 2017 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.1
25
* Design: David Gascon
26
* Implementation: Luismi Marti
27
*/
28
29
#include <WaspLoRaWAN.h>
30
31
// socket to use
32
//////////////////////////////////////////////
33
uint8_t socket = SOCKET0;
34
//////////////////////////////////////////////
35
36
// Device parameters for Back-End registration
37
////////////////////////////////////////////////////////////
38
char DEVICE_EUI[] = "0102030405060708";
39
char DEVICE_ADDR[] = "05060708";
40
char NWK_SESSION_KEY[] = "01020304050607080910111213141516";
41
char APP_SESSION_KEY[] = "000102030405060708090A0B0C0D0E0F";
42
////////////////////////////////////////////////////////////
43
44
// Define port to use in Back-End: from 1 to 223
45
uint8_t PORT = 3;
46
47
// Define data payload to send (maximum is up to data rate)
48
char data[] = "0102030405060708090A0B0C0D0E0F";
49
50
// variable
51
uint8_t error;
52
53
54
55
void setup()
56
{
57
USB.ON();
58
USB.println(F("LoRaWAN example - Send Unconfirmed packets (no ACK)\n"));
59
60
61
USB.println(F("------------------------------------"));
62
USB.println(F("Module configuration"));
63
USB.println(F("------------------------------------\n"));
64
65
66
//////////////////////////////////////////////
67
// 1. Switch on
68
//////////////////////////////////////////////
69
70
error = LoRaWAN.ON(socket);
71
72
// Check status
73
if( error == 0 )
74
{
75
USB.println(F("1. Switch ON OK"));
76
}
77
else
78
{
79
USB.print(F("1. Switch ON error = "));
80
USB.println(error, DEC);
81
}
82
83
84
//////////////////////////////////////////////
85
// 2. Set Device EUI
86
//////////////////////////////////////////////
87
88
error = LoRaWAN.setDeviceEUI(DEVICE_EUI);
89
90
// Check status
91
if( error == 0 )
92
{
93
USB.println(F("2. Device EUI set OK"));
94
}
95
else
96
{
97
USB.print(F("2. Device EUI set error = "));
98
USB.println(error, DEC);
99
}
100
101
102
//////////////////////////////////////////////
103
// 3. Set Device Address
104
//////////////////////////////////////////////
105
106
error = LoRaWAN.setDeviceAddr(DEVICE_ADDR);
107
108
// Check status
109
if( error == 0 )
110
{
111
USB.println(F("3. Device address set OK"));
112
}
113
else
114
{
115
USB.print(F("3. Device address set error = "));
116
USB.println(error, DEC);
117
}
118
119
120
//////////////////////////////////////////////
121
// 4. Set Network Session Key
122
//////////////////////////////////////////////
123
124
error = LoRaWAN.setNwkSessionKey(NWK_SESSION_KEY);
125
126
// Check status
127
if( error == 0 )
128
{
129
USB.println(F("4. Network Session Key set OK"));
130
}
131
else
132
{
133
USB.print(F("4. Network Session Key set error = "));
134
USB.println(error, DEC);
135
}
136
137
138
//////////////////////////////////////////////
139
// 5. Set Application Session Key
140
//////////////////////////////////////////////
141
142
error = LoRaWAN.setAppSessionKey(APP_SESSION_KEY);
143
144
// Check status
145
if( error == 0 )
146
{
147
USB.println(F("5. Application Session Key set OK"));
148
}
149
else
150
{
151
USB.print(F("5. Application Session Key set error = "));
152
USB.println(error, DEC);
153
}
154
155
156
//////////////////////////////////////////////
157
// 6. Save configuration
158
//////////////////////////////////////////////
159
160
error = LoRaWAN.saveConfig();
161
162
// Check status
163
if( error == 0 )
164
{
165
USB.println(F("6. Save configuration OK"));
166
}
167
else
168
{
169
USB.print(F("6. Save configuration error = "));
170
USB.println(error, DEC);
171
}
172
173
174
USB.println(F("\n------------------------------------"));
175
USB.println(F("Module configured"));
176
USB.println(F("------------------------------------\n"));
177
178
LoRaWAN.getDeviceEUI();
179
USB.print(F("Device EUI: "));
180
USB.println(LoRaWAN._devEUI);
181
182
LoRaWAN.getDeviceAddr();
183
USB.print(F("Device Address: "));
184
USB.println(LoRaWAN._devAddr);
185
186
USB.println();
187
}
188
189
190
191
void loop()
192
{
193
194
//////////////////////////////////////////////
195
// 1. Switch on
196
//////////////////////////////////////////////
197
198
error = LoRaWAN.ON(socket);
199
200
// Check status
201
if( error == 0 )
202
{
203
USB.println(F("1. Switch ON OK"));
204
}
205
else
206
{
207
USB.print(F("1. Switch ON error = "));
208
USB.println(error, DEC);
209
}
210
211
212
//////////////////////////////////////////////
213
// 2. Join network
214
//////////////////////////////////////////////
215
216
error = LoRaWAN.joinABP();
217
218
// Check status
219
if( error == 0 )
220
{
221
USB.println(F("2. Join network OK"));
222
}
223
else
224
{
225
USB.print(F("2. Join network error = "));
226
USB.println(error, DEC);
227
}
228
229
230
//////////////////////////////////////////////
231
// 3. Send unconfirmed packet
232
//////////////////////////////////////////////
233
234
error = LoRaWAN.sendUnconfirmed( PORT, data);
235
236
// Error messages:
237
/*
238
* '6' : Module hasn't joined a network
239
* '5' : Sending error
240
* '4' : Error with data length
241
* '2' : Module didn't response
242
* '1' : Module communication error
243
*/
244
// Check status
245
if( error == 0 )
246
{
247
USB.println(F("3. Send Unconfirmed packet OK"));
248
if (LoRaWAN._dataReceived == true)
249
{
250
USB.print(F(" There's data on port number "));
251
USB.print(LoRaWAN._port,DEC);
252
USB.print(F(".\r\n Data: "));
253
USB.println(LoRaWAN._data);
254
}
255
}
256
else
257
{
258
USB.print(F("3. Send Unconfirmed packet error = "));
259
USB.println(error, DEC);
260
}
261
262
263
//////////////////////////////////////////////
264
// 4. Switch off
265
//////////////////////////////////////////////
266
267
error = LoRaWAN.OFF(socket);
268
269
// Check status
270
if( error == 0 )
271
{
272
USB.println(F("4. Switch OFF OK"));
273
}
274
else
275
{
276
USB.print(F("4. Switch OFF error = "));
277
USB.println(error, DEC);
278
}
279
280
281
USB.println();
282
delay(5000);
283
284
285
286
}
Copied!

Output

1
H#
2
LoRaWAN example - Send Unconfirmed packets (no ACK)
3
4
------------------------------------
5
Module configuration
6
------------------------------------
7
8
1. Switch ON OK
9
2. Device EUI set OK
10
3. Device address set OK
11
4. Network Session Key set OK
12
5. Application Session Key set OK
13
6. Save configuration OK
14
15
------------------------------------
16
Module configured
17
------------------------------------
18
19
Device EUI: 0102030405060708
20
Device Address: 05060708
21
22
1. Switch ON OK
23
2. Join network OK
24
3. Send Unconfirmed packet OK
25
4. Switch OFF OK
26
27
1. Switch ON OK
28
2. Join network OK
29
3. Send Unconfirmed packet OK
30
4. Switch OFF OK
31
32
...
Copied!
Last modified 1yr ago