LoRaWAN 11: Join OTAA send frame
This example shows how to configure the module and send frames to a LoRaWAN gateway with ACK after join a network using OTAA

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 frames to a LoRaWAN gateway with 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
#include <WaspFrame.h>
31
32
// socket to use
33
//////////////////////////////////////////////
34
uint8_t socket = SOCKET0;
35
//////////////////////////////////////////////
36
37
// Device parameters for Back-End registration
38
////////////////////////////////////////////////////////////
39
char DEVICE_EUI[] = "BE7A000000000F97";
40
char APP_EUI[] = "BE7A00000000062B";
41
char APP_KEY[] = "4498379737936910D288C35346A2B4A9";
42
////////////////////////////////////////////////////////////
43
44
// Define port to use in Back-End: from 1 to 223
45
uint8_t PORT = 3;
46
47
// variable
48
uint8_t error;
49
50
// define the Waspmote ID
51
char moteID[] = "node_01";
52
53
54
void setup()
55
{
56
USB.ON();
57
USB.println(F("LoRaWAN example - Send Confirmed 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
frame.setID(moteID);
197
198
}
199
200
201
202
void loop()
203
{
204
205
//////////////////////////////////////////////
206
// 1. Creating a new frame
207
//////////////////////////////////////////////
208
USB.println(F("Creating an ASCII frame"));
209
210
// Create new frame (ASCII)
211
frame.createFrame(ASCII);
212
213
// set frame fields (Battery sensor - uint8_t)
214
frame.addSensor(SENSOR_BAT, PWR.getBatteryLevel());
215
216
// Prints frame
217
frame.showFrame();
218
219
//////////////////////////////////////////////
220
// 2. Switch on
221
//////////////////////////////////////////////
222
223
error = LoRaWAN.ON(socket);
224
225
// Check status
226
if( error == 0 )
227
{
228
USB.println(F("1. Switch ON OK"));
229
}
230
else
231
{
232
USB.print(F("1. Switch ON error = "));
233
USB.println(error, DEC);
234
}
235
236
237
//////////////////////////////////////////////
238
// 3. Join network
239
//////////////////////////////////////////////
240
241
error = LoRaWAN.joinABP();
242
243
// Check status
244
if( error == 0 )
245
{
246
USB.println(F("2. Join network OK"));
247
248
//////////////////////////////////////////////
249
// 4. Send confirmed packet
250
//////////////////////////////////////////////
251
252
error = LoRaWAN.sendConfirmed( PORT, frame.buffer, frame.length);
253
254
// Error messages:
255
/*
256
* '6' : Module hasn't joined a network
257
* '5' : Sending error
258
* '4' : Error with data length
259
* '2' : Module didn't response
260
* '1' : Module communication error
261
*/
262
// Check status
263
if( error == 0 )
264
{
265
USB.println(F("3. Send confirmed packet OK"));
266
if (LoRaWAN._dataReceived == true)
267
{
268
USB.print(F(" There's data on port number "));
269
USB.print(LoRaWAN._port,DEC);
270
USB.print(F(".\r\n Data: "));
271
USB.println(LoRaWAN._data);
272
}
273
}
274
else
275
{
276
USB.print(F("3. Send confirmed packet error = "));
277
USB.println(error, DEC);
278
}
279
}
280
else
281
{
282
USB.print(F("2. Join network error = "));
283
USB.println(error, DEC);
284
}
285
286
287
//////////////////////////////////////////////
288
// 5. Switch off
289
//////////////////////////////////////////////
290
291
error = LoRaWAN.OFF(socket);
292
293
// Check status
294
if( error == 0 )
295
{
296
USB.println(F("4. Switch OFF OK"));
297
}
298
else
299
{
300
USB.print(F("4. Switch OFF error = "));
301
USB.println(error, DEC);
302
}
303
304
305
USB.println();
306
delay(10000);
307
308
309
310
}
Copied!

Output

1
H#
2
LoRaWAN example - Send Confirmed 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
Creating an ASCII frame
26
===============================
27
Current ASCII Frame:
28
Length: 40
29
Frame Type: 134
30
frame (HEX): 3C3D3E86012332453443373036334439333734324135236E6F64655F30312330234241543A393723
31
frame (STR): <=>�#2E4C7063D93742A5#node_01#0#BAT:97#
32
===============================
33
1. Switch ON OK
34
2. Join network OK
35
3. Send confirmed packet OK
36
4. Switch OFF OK
37
38
...
Copied!
Last modified 1yr ago