ZB 09b: Complete example RX
This is a complete example for XBee-ZigBee. This example shows how to receive a packet. When the source address is the expected one, an answer is sent back to the sending Waspmote

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

Output

1
E#
2
Complete example (RX node)
3
Wait for association
4
5
Joined a network!
6
operating 16-b PAN ID: D27C
7
operating 64-b PAN ID: 84D87D34C4AE4CB1
8
channel: 0B
9
10
1. New packet received
11
--> Data: <=>€#387245265#node_TX#0#STR:Complete example message#BAT:6#
12
--> Length: 61
13
--> Source MAC address: 0013A200403A2A49
14
15
2. Send a response to the TX node:
16
send ok
17
18
----------------------------------
19
...
Copied!
Last modified 2yr ago