ZB 04: Receive packets
This example shows hot to receive packets with XBee modules

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_04] - receive packet --------
3
*
4
* Explanation: This example shows hot to receive packets with XBee
5
* modules
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 example"));
39
40
//////////////////////////
41
// 1. init XBee
42
//////////////////////////
43
xbeeZB.ON();
44
45
delay(3000);
46
47
48
//////////////////////////
49
// 2. check XBee's network parameters
50
//////////////////////////
51
checkNetworkParams();
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
else
72
{
73
// Print error message:
74
/*
75
* '7' : Buffer full. Not enough memory space
76
* '6' : Error escaping character within payload bytes
77
* '5' : Error escaping character in checksum byte
78
* '4' : Checksum is not correct
79
* '3' : Checksum byte is not available
80
* '2' : Frame Type is not valid
81
* '1' : Timeout when receiving answer
82
*/
83
USB.print(F("Error receiving a packet:"));
84
USB.println(error,DEC);
85
}
86
}
87
88
89
/*******************************************
90
*
91
* checkNetworkParams - Check operating
92
* network parameters in the XBee module
93
*
94
*******************************************/
95
void checkNetworkParams()
96
{
97
// 1. get operating 64-b PAN ID
98
xbeeZB.getOperating64PAN();
99
100
// 2. wait for association indication
101
xbeeZB.getAssociationIndication();
102
103
while( xbeeZB.associationIndication != 0 )
104
{
105
delay(2000);
106
107
// get operating 64-b PAN ID
108
xbeeZB.getOperating64PAN();
109
110
USB.print(F("operating 64-b PAN ID: "));
111
USB.printHex(xbeeZB.operating64PAN[0]);
112
USB.printHex(xbeeZB.operating64PAN[1]);
113
USB.printHex(xbeeZB.operating64PAN[2]);
114
USB.printHex(xbeeZB.operating64PAN[3]);
115
USB.printHex(xbeeZB.operating64PAN[4]);
116
USB.printHex(xbeeZB.operating64PAN[5]);
117
USB.printHex(xbeeZB.operating64PAN[6]);
118
USB.printHex(xbeeZB.operating64PAN[7]);
119
USB.println();
120
121
xbeeZB.getAssociationIndication();
122
}
123
124
USB.println(F("\nJoined a network!"));
125
126
// 3. get network parameters
127
xbeeZB.getOperating16PAN();
128
xbeeZB.getOperating64PAN();
129
xbeeZB.getChannel();
130
131
USB.print(F("operating 16-b PAN ID: "));
132
USB.printHex(xbeeZB.operating16PAN[0]);
133
USB.printHex(xbeeZB.operating16PAN[1]);
134
USB.println();
135
136
USB.print(F("operating 64-b PAN ID: "));
137
USB.printHex(xbeeZB.operating64PAN[0]);
138
USB.printHex(xbeeZB.operating64PAN[1]);
139
USB.printHex(xbeeZB.operating64PAN[2]);
140
USB.printHex(xbeeZB.operating64PAN[3]);
141
USB.printHex(xbeeZB.operating64PAN[4]);
142
USB.printHex(xbeeZB.operating64PAN[5]);
143
USB.printHex(xbeeZB.operating64PAN[6]);
144
USB.printHex(xbeeZB.operating64PAN[7]);
145
USB.println();
146
147
USB.print(F("channel: "));
148
USB.printHex(xbeeZB.channel);
149
USB.println();
150
151
}
Copied!

Output

1
E#
2
Receiving 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#1#STR:new_sensor_frame#BAT:1#
9
Length: 53
10
Data: <=>€#387245265#node_01#2#STR:new_sensor_frame#BAT:1#
11
Length: 53
12
Data: <=>€#387245265#node_01#3#STR:new_sensor_frame#BAT:1#
13
Length: 53
14
...
Copied!
Last modified 2yr ago