ZB 05b: Receive broadcast
This program shows how to receive packets with XBee-ZigBee modules. The packets received were broadcast.

Required Materials

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

Notes

- The coordinator must be turned on - The battery has to be connected. - This example can be executed in Waspmote v12

Code

1
/*
2
* ------ [ZB_05b] - receive broadcast packets --------
3
*
4
* Explanation: This program shows how to receive packets with
5
* XBee-ZigBee modules. The packets received were broadcast
6
*
7
* Copyright (C) 2015 Libelium Comunicaciones Distribuidas S.L.
8
* http://www.libelium.com
9
*
10
* This program is free software: you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation, either version 3 of the License, or
13
* (at your option) any later version.
14
*
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
19
*
20
* You should have received a copy of the GNU General Public License
21
* along with this program. If not, see .
22
*
23
* Version: 0.2
24
* Design: David Gascón
25
* Implementation: Yuri Carmona
26
*/
27
28
#include <WaspXBeeZB.h>
29
30
// define variable
31
uint8_t error;
32
33
34
void setup()
35
{
36
// init USB port
37
USB.ON();
38
USB.println(F("Receiving BROADCAST packets example"));
39
40
//////////////////////////
41
// 1. init XBee
42
//////////////////////////
43
xbeeZB.ON();
44
45
delay(3000);
46
47
//////////////////////////
48
// 2. check XBee's network parameters
49
//////////////////////////
50
checkNetworkParams();
51
}
52
53
54
55
void loop()
56
{
57
// receive XBee packet (wait for 10 seconds)
58
error = xbeeZB.receivePacketTimeout( 10000 );
59
60
// check answer
61
if( error == 0 )
62
{
63
// Show data stored in '_payload' buffer indicated by '_length'
64
USB.print(F("Data: "));
65
USB.println( xbeeZB._payload, xbeeZB._length);
66
67
// Show data stored in '_payload' buffer indicated by '_length'
68
USB.print(F("Length: "));
69
USB.println( xbeeZB._length,DEC);
70
71
// Show data stored in '_payload' buffer indicated by '_length'
72
USB.print(F("Source MAC Address: "));
73
USB.printHex( xbeeZB._srcMAC[0] );
74
USB.printHex( xbeeZB._srcMAC[1] );
75
USB.printHex( xbeeZB._srcMAC[2] );
76
USB.printHex( xbeeZB._srcMAC[3] );
77
USB.printHex( xbeeZB._srcMAC[4] );
78
USB.printHex( xbeeZB._srcMAC[5] );
79
USB.printHex( xbeeZB._srcMAC[6] );
80
USB.printHex( xbeeZB._srcMAC[7] );
81
USB.println();
82
USB.println(F("--------------------------------"));
83
}
84
else
85
{
86
// Print error message:
87
/*
88
* '7' : Buffer full. Not enough memory space
89
* '6' : Error escaping character within payload bytes
90
* '5' : Error escaping character in checksum byte
91
* '4' : Checksum is not correct
92
* '3' : Checksum byte is not available
93
* '2' : Frame Type is not valid
94
* '1' : Timeout when receiving answer
95
*/
96
USB.print(F("Error receiving a packet:"));
97
USB.println(error,DEC);
98
USB.println(F("--------------------------------"));
99
}
100
}
101
102
103
/*******************************************
104
*
105
* checkNetworkParams - Check operating
106
* network parameters in the XBee module
107
*
108
*******************************************/
109
void checkNetworkParams()
110
{
111
// 1. get operating 64-b PAN ID
112
xbeeZB.getOperating64PAN();
113
114
// 2. wait for association indication
115
xbeeZB.getAssociationIndication();
116
117
while( xbeeZB.associationIndication != 0 )
118
{
119
delay(2000);
120
121
// get operating 64-b PAN ID
122
xbeeZB.getOperating64PAN();
123
124
USB.print(F("operating 64-b PAN ID: "));
125
USB.printHex(xbeeZB.operating64PAN[0]);
126
USB.printHex(xbeeZB.operating64PAN[1]);
127
USB.printHex(xbeeZB.operating64PAN[2]);
128
USB.printHex(xbeeZB.operating64PAN[3]);
129
USB.printHex(xbeeZB.operating64PAN[4]);
130
USB.printHex(xbeeZB.operating64PAN[5]);
131
USB.printHex(xbeeZB.operating64PAN[6]);
132
USB.printHex(xbeeZB.operating64PAN[7]);
133
USB.println();
134
135
xbeeZB.getAssociationIndication();
136
}
137
138
USB.println(F("\nJoined a network!"));
139
140
// 3. get network parameters
141
xbeeZB.getOperating16PAN();
142
xbeeZB.getOperating64PAN();
143
xbeeZB.getChannel();
144
145
USB.print(F("operating 16-b PAN ID: "));
146
USB.printHex(xbeeZB.operating16PAN[0]);
147
USB.printHex(xbeeZB.operating16PAN[1]);
148
USB.println();
149
150
USB.print(F("operating 64-b PAN ID: "));
151
USB.printHex(xbeeZB.operating64PAN[0]);
152
USB.printHex(xbeeZB.operating64PAN[1]);
153
USB.printHex(xbeeZB.operating64PAN[2]);
154
USB.printHex(xbeeZB.operating64PAN[3]);
155
USB.printHex(xbeeZB.operating64PAN[4]);
156
USB.printHex(xbeeZB.operating64PAN[5]);
157
USB.printHex(xbeeZB.operating64PAN[6]);
158
USB.printHex(xbeeZB.operating64PAN[7]);
159
USB.println();
160
161
USB.print(F("channel: "));
162
USB.printHex(xbeeZB.channel);
163
USB.println();
164
165
}
Copied!

Output

1
E#
2
Receiving BROADCAST packets example
3
4
Joined a network!
5
operating 16-b PAN ID: D27C
6
operating 64-b PAN ID: 84D87D34C4AE4CB1
7
channel: 0B
8
Data: <=>€#387245265#node_01#0#STR:new_sensor_frame#BAT:1#
9
Length: 53
10
Source MAC Address: 0013A200403A2A49
11
--------------------------------
12
Data: <=>€#387245265#node_01#1#STR:new_sensor_frame#BAT:1#
13
Length: 53
14
Source MAC Address: 0013A200403A2A49
15
--------------------------------
16
Data: <=>€#387245265#node_01#2#STR:new_sensor_frame#BAT:1#
17
Length: 53
18
Source MAC Address: 0013A200403A2A49
19
--------------------------------
20
...
Copied!
Last modified 2yr ago