LoRaWAN 09: Join OTAA send unconfirmed
This example shows how to configure the module and send packets to a LoRaWAN gateway without acknowledgement.

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

Output

1
H#
2
LoRaWAN example - Send Unconfirmed packets (ACK)
3
4
------------------------------------
5
Module configuration
6
------------------------------------
7
8
1. Switch ON OK
9
2. Device EUI set OK
10
3. Application EUI set OK
11
4. Application Key set OK
12
5. Join network OK
13
6. Save configuration OK
14
7. Switch OFF OK
15
16
---------------------------------------------------------------
17
Module configured
18
After joining through OTAA, the module and the network exchanged
19
the Network Session Key and the Application Session Key which
20
are needed to perform communications. After that, 'ABP mode' is used
21
to join the network and send messages after powering on the module
22
---------------------------------------------------------------
23
24
25
1. Switch ON OK
26
2. Join network OK
27
3. Send unconfirmed packet OK
28
4. Switch OFF OK
29
1. Switch ON OK
30
2. Join network OK
31
3. Send unconfirmed packet OK
32
4. Switch OFF OK
33
1. Switch ON OK
34
2. Join network OK
35
3. Send unconfirmed packet OK
36
4. Switch OFF OK
37
1. Switch ON OK
38
2. Join network OK
39
3. Send unconfirmed packet OK
40
4. Switch OFF OK
41
1. Switch ON OK
42
2. Join network OK
43
3. Send unconfirmed packet OK
44
4. Switch OFF OK
45
1. Switch ON OK
46
2. Join network OK
47
3. Send unconfirmed packet OK
48
4. Switch OFF OK
Copied!
Last modified 1yr ago