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

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 frames to a LoRaWAN gateway with 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
#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[] = "0102030405060708";
40
char DEVICE_ADDR[] = "05060708";
41
char NWK_SESSION_KEY[] = "01020304050607080910111213141516";
42
char APP_SESSION_KEY[] = "000102030405060708090A0B0C0D0E0F";
43
////////////////////////////////////////////////////////////
44
45
// Define port to use in Back-End: from 1 to 223
46
uint8_t PORT = 3;
47
48
// variable
49
uint8_t error;
50
51
// define the Waspmote ID
52
char moteID[] = "node_01";
53
54
55
void setup()
56
{
57
USB.ON();
58
USB.println(F("LoRaWAN example - Send Confirmed packets (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
frame.setID(moteID);
189
190
}
191
192
193
194
void loop()
195
{
196
197
//////////////////////////////////////////////
198
// 1. Creating a new frame
199
//////////////////////////////////////////////
200
USB.println(F("Creating an ASCII frame"));
201
202
// Create new frame (ASCII)
203
frame.createFrame(ASCII);
204
205
// set frame fields (String - char*)
206
frame.addSensor(SENSOR_STR, (char*) "this_is_a_string");
207
// set frame fields (Battery sensor - uint8_t)
208
frame.addSensor(SENSOR_BAT, PWR.getBatteryLevel());
209
210
// Prints frame
211
frame.showFrame();
212
213
214
//////////////////////////////////////////////
215
// 2. Switch on LoRaWAN
216
//////////////////////////////////////////////
217
218
error = LoRaWAN.ON(socket);
219
220
// Check status
221
if( error == 0 )
222
{
223
USB.println(F("1. Switch ON OK"));
224
}
225
else
226
{
227
USB.print(F("1. Switch ON error = "));
228
USB.println(error, DEC);
229
}
230
231
232
//////////////////////////////////////////////
233
// 3. Join network
234
//////////////////////////////////////////////
235
236
error = LoRaWAN.joinABP();
237
238
// Check status
239
if( error == 0 )
240
{
241
USB.println(F("2. Join network OK"));
242
243
//////////////////////////////////////////////
244
// 4. Send confirmed packet
245
//////////////////////////////////////////////
246
247
error = LoRaWAN.sendConfirmed( PORT, frame.buffer,frame.length);
248
249
// Error messages:
250
/*
251
* '6' : Module hasn't joined a network
252
* '5' : Sending error
253
* '4' : Error with data length
254
* '2' : Module didn't response
255
* '1' : Module communication error
256
*/
257
// Check status
258
if( error == 0 )
259
{
260
USB.println(F("3. Send Confirmed packet OK"));
261
if (LoRaWAN._dataReceived == true)
262
{
263
USB.print(F(" There's data on port number "));
264
USB.print(LoRaWAN._port,DEC);
265
USB.print(F(".\r\n Data: "));
266
USB.println(LoRaWAN._data);
267
}
268
}
269
else
270
{
271
USB.print(F("3. Send Confirmed packet error = "));
272
USB.println(error, DEC);
273
}
274
}
275
else
276
{
277
USB.print(F("2. Join network error = "));
278
USB.println(error, DEC);
279
}
280
281
282
283
//////////////////////////////////////////////
284
// 5. Switch off
285
//////////////////////////////////////////////
286
287
error = LoRaWAN.OFF(socket);
288
289
// Check status
290
if( error == 0 )
291
{
292
USB.println(F("4. Switch OFF OK"));
293
}
294
else
295
{
296
USB.print(F("4. Switch OFF error = "));
297
USB.println(error, DEC);
298
}
299
300
301
USB.println();
302
delay(5000);
303
304
305
306
}
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. 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
Creating an ASCII frame
23
===============================
24
Current ASCII Frame:
25
Length: 61
26
Frame Type: 134
27
frame (HEX): 3C3D3E86022334443343373036334439333734324634236E6F64655F30312330235354523A746869735F69735F615F737472696E67234241543A393823
28
frame (STR): <=>�#4D3C7063D93742F4#node_01#0#STR:this_is_a_string#BAT:98#
29
===============================
30
1. Switch ON OK
31
2. Join network OK
32
3. Send Confirmed packet OK
33
4. Switch OFF OK
34
35
Creating an ASCII frame
36
===============================
37
Current ASCII Frame:
38
Length: 61
39
Frame Type: 134
40
frame (HEX): 3C3D3E86022334443343373036334439333734324634236E6F64655F30312331235354523A746869735F69735F615F737472696E67234241543A393823
41
frame (STR): <=>�#4D3C7063D93742F4#node_01#1#STR:this_is_a_string#BAT:98#
42
===============================
43
1. Switch ON OK
44
2. Join network OK
45
3. Send Confirmed packet OK
46
4. Switch OFF OK
Copied!
Last modified 1yr ago