ZB 09a: Complete example TX
This is a complete example for XBee-ZigBee. This example shows how to send a packet using unicast 64-bit destination address. After sending the packet, Waspmote waits for the response of the receiver and prints all available information

Required Materials

1 x Waspmote 1 x Battery 1 x MiniUSB wire 1 x XBee-ZigBee module

Notes

- This example belongs to a two-code example. This is the sending part. - Both XBee modules must be configured with the same operating network parameters. - The battery has to be connected. - This example can be executed in Waspmote v12

Code

1
/*
2
* ------ [ZB_09a] - complete example send ------
3
*
4
* Explanation: This is a complete example for XBee-ZigBee
5
* This example shows how to send a packet using unicast 64-bit
6
* destination address. After sending the packet, Waspmote waits
7
* for the response of the receiver and prints all available
8
* information
9
*
10
* Copyright (C) 2015 Libelium Comunicaciones Distribuidas S.L.
11
* http://www.libelium.com
12
*
13
* This program is free software: you can redistribute it and/or modify
14
* it under the terms of the GNU General Public License as published by
15
* the Free Software Foundation, either version 3 of the License, or
16
* (at your option) any later version.
17
*
18
* This program is distributed in the hope that it will be useful,
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
* GNU General Public License for more details.
22
*
23
* You should have received a copy of the GNU General Public License
24
* along with this program. If not, see .
25
*
26
* Version: 0.2
27
* Design: David Gascón
28
* Implementation: Yuri Carmona
29
*/
30
31
#include <WaspXBeeZB.h>
32
#include <WaspFrame.h>
33
34
// Destination MAC address
35
//////////////////////////////////////////
36
char RX_ADDRESS[] = "0013A200403A2A49";
37
//////////////////////////////////////////
38
39
// define variable
40
uint8_t error;
41
42
43
44
void setup()
45
{
46
// init USB port
47
USB.ON();
48
USB.println(F("Complete example (TX node)"));
49
50
// set Waspmote identifier
51
frame.setID("node_TX");
52
53
//////////////////////////
54
// 1. init XBee
55
//////////////////////////
56
xbeeZB.ON();
57
58
delay(3000);
59
60
//////////////////////////
61
// 2. check XBee's network parameters
62
//////////////////////////
63
checkNetworkParams();
64
}
65
66
67
68
void loop()
69
{
70
//////////////////////////
71
// 1. create frame
72
//////////////////////////
73
74
// 1.1. create new frame
75
frame.createFrame(ASCII);
76
77
// 1.2. add frame fields
78
frame.addSensor(SENSOR_STR, "Complete example message");
79
frame.addSensor(SENSOR_BAT, PWR.getBatteryLevel() );
80
81
USB.println(F("\n1. Created frame to be sent"));
82
frame.showFrame();
83
84
//////////////////////////
85
// 2. send packet
86
//////////////////////////
87
88
// send XBee packet
89
error = xbeeZB.send( RX_ADDRESS, frame.buffer, frame.length );
90
91
USB.println(F("\n2. Send a packet to the RX node: "));
92
93
// check TX flag
94
if( error == 0 )
95
{
96
USB.println(F("send ok"));
97
98
// blink green LED
99
Utils.blinkGreenLED();
100
}
101
else
102
{
103
USB.println(F("send error"));
104
105
// blink red LED
106
Utils.blinkRedLED();
107
}
108
109
110
//////////////////////////
111
// 3. receive answer
112
//////////////////////////
113
114
USB.println(F("\n3. Wait for an incoming message"));
115
116
// receive XBee packet
117
error = xbeeZB.receivePacketTimeout( 10000 );
118
119
// check answer
120
if( error == 0 )
121
{
122
// Show data stored in '_payload' buffer indicated by '_length'
123
USB.print(F("--> Data: "));
124
USB.println( xbeeZB._payload, xbeeZB._length);
125
126
// Show data stored in '_payload' buffer indicated by '_length'
127
USB.print(F("--> Length: "));
128
USB.println( xbeeZB._length,DEC);
129
130
// Show data stored in '_payload' buffer indicated by '_length'
131
USB.print(F("--> Source MAC address: "));
132
USB.printHex( xbeeZB._srcMAC[0] );
133
USB.printHex( xbeeZB._srcMAC[1] );
134
USB.printHex( xbeeZB._srcMAC[2] );
135
USB.printHex( xbeeZB._srcMAC[3] );
136
USB.printHex( xbeeZB._srcMAC[4] );
137
USB.printHex( xbeeZB._srcMAC[5] );
138
USB.printHex( xbeeZB._srcMAC[6] );
139
USB.printHex( xbeeZB._srcMAC[7] );
140
USB.println();
141
}
142
else
143
{
144
// Print error message:
145
/*
146
* '7' : Buffer full. Not enough memory space
147
* '6' : Error escaping character within payload bytes
148
* '5' : Error escaping character in checksum byte
149
* '4' : Checksum is not correct
150
* '3' : Checksum byte is not available
151
* '2' : Frame Type is not valid
152
* '1' : Timeout when receiving answer
153
*/
154
USB.print(F("Error receiving a packet:"));
155
USB.println(error,DEC);
156
}
157
158
// wait for 5 seconds
159
USB.println(F("\n----------------------------------"));
160
delay(5000);
161
162
}
163
164
165
/*******************************************
166
*
167
* checkNetworkParams - Check operating
168
* network parameters in the XBee module
169
*
170
*******************************************/
171
void checkNetworkParams()
172
{
173
// 1. get operating 64-b PAN ID
174
xbeeZB.getOperating64PAN();
175
176
// 2. wait for association indication
177
xbeeZB.getAssociationIndication();
178
179
while( xbeeZB.associationIndication != 0 )
180
{
181
printAssociationState();
182
183
delay(2000);
184
185
// get operating 64-b PAN ID
186
xbeeZB.getOperating64PAN();
187
188
USB.print(F("operating 64-b PAN ID: "));
189
USB.printHex(xbeeZB.operating64PAN[0]);
190
USB.printHex(xbeeZB.operating64PAN[1]);
191
USB.printHex(xbeeZB.operating64PAN[2]);
192
USB.printHex(xbeeZB.operating64PAN[3]);
193
USB.printHex(xbeeZB.operating64PAN[4]);
194
USB.printHex(xbeeZB.operating64PAN[5]);
195
USB.printHex(xbeeZB.operating64PAN[6]);
196
USB.printHex(xbeeZB.operating64PAN[7]);
197
USB.println();
198
199
xbeeZB.getAssociationIndication();
200
}
201
202
USB.println(F("\nJoined a network!"));
203
204
// 3. get network parameters
205
xbeeZB.getOperating16PAN();
206
xbeeZB.getOperating64PAN();
207
xbeeZB.getChannel();
208
209
USB.print(F("operating 16-b PAN ID: "));
210
USB.printHex(xbeeZB.operating16PAN[0]);
211
USB.printHex(xbeeZB.operating16PAN[1]);
212
USB.println();
213
214
USB.print(F("operating 64-b PAN ID: "));
215
USB.printHex(xbeeZB.operating64PAN[0]);
216
USB.printHex(xbeeZB.operating64PAN[1]);
217
USB.printHex(xbeeZB.operating64PAN[2]);
218
USB.printHex(xbeeZB.operating64PAN[3]);
219
USB.printHex(xbeeZB.operating64PAN[4]);
220
USB.printHex(xbeeZB.operating64PAN[5]);
221
USB.printHex(xbeeZB.operating64PAN[6]);
222
USB.printHex(xbeeZB.operating64PAN[7]);
223
USB.println();
224
225
USB.print(F("channel: "));
226
USB.printHex(xbeeZB.channel);
227
USB.println();
228
229
}
230
231
232
233
234
235
/*******************************************
236
*
237
* printAssociationState - Print the state
238
* of the association flag
239
*
240
*******************************************/
241
void printAssociationState()
242
{
243
switch(xbeeZB.associationIndication)
244
{
245
case 0x00 :
246
USB.println(F("Successfully formed or joined a network"));
247
break;
248
case 0x21 :
249
USB.println(F("Scan found no PANs"));
250
break;
251
case 0x22 :
252
USB.println(F("Scan found no valid PANs based on current SC and ID settings"));
253
break;
254
case 0x23 :
255
USB.println(F("Valid Coordinator or Routers found, but they are not allowing joining (NJ expired)"));
256
break;
257
case 0x24 :
258
USB.println(F("No joinable beacons were found"));
259
break;
260
case 0x25 :
261
USB.println(F("Unexpected state, node should not be attempting to join at this time"));
262
break;
263
case 0x27 :
264
USB.println(F("Node Joining attempt failed"));
265
break;
266
case 0x2A :
267
USB.println(F("Coordinator Start attempt failed"));
268
break;
269
case 0x2B :
270
USB.println(F("Checking for an existing coordinator"));
271
break;
272
case 0x2C :
273
USB.println(F("Attempt to leave the network failed"));
274
break;
275
case 0xAB :
276
USB.println(F("Attempted to join a device that did not respond."));
277
break;
278
case 0xAC :
279
USB.println(F("Secure join error: network security key received unsecured"));
280
break;
281
case 0xAD :
282
USB.println(F("Secure join error: network security key not received"));
283
break;
284
case 0xAF :
285
USB.println(F("Secure join error: joining device does not have the right preconfigured link key"));
286
break;
287
case 0xFF :
288
USB.println(F("Scanning for a ZigBee network (routers and end devices)"));
289
break;
290
default :
291
USB.println(F("Unkown associationIndication"));
292
break;
293
}
294
}
295
296
Copied!

Output

1
E#
2
Complete example (TX node)
3
4
Joined a network!
5
operating 16-b PAN ID: D27C
6
operating 64-b PAN ID: 84D87D34C4AE4CB1
7
channel: 0B
8
9
1. Created frame to be sent
10
===============================
11
Current ASCII Frame:
12
Length: 61
13
Frame Type: 128
14
frame (HEX): 3C3D3E800223333837323435323635236E6F64655F54582330235354523A436F6D706C657465206578616D706C65206D657373616765234241543A3623
15
frame (STR): <=>€#387245265#node_TX#0#STR:Complete example message#BAT:6#
16
===============================
17
18
2. Send a packet to the RX node:
19
send ok
20
21
3. Wait for an incoming message
22
--> Data: Message_from_RX_node
23
--> Length: 20
24
--> Source MAC address: 0013A200407ACB92
25
26
----------------------------------
27
...
Copied!
Last modified 2yr ago