ZB 11b: Search node RX
This program shows how to search for a specific node inside the XBee's network. Once the node has been found a packet is sent to this node. Keep in mind that there must be a node with the specific Node ID(NI parameter) so as to be scanned

Required Materials

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

Notes

- The coordinator must be turned on - Keep in mind that there must be a node with the specific Node ID (NI parameter) so as to be scanned - The battery has to be connected. - This example can be executed in Waspmote v12

Code

1
/*
2
* ------ [ZB_11b] - node search --------
3
*
4
* Explanation: This program shows how to search for a specific
5
* node inside the XBee's network. Once the node has been found a
6
* packet is sent to this node.
7
*
8
* Copyright (C) 2015 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: 0.1
25
* Design: David Gascón
26
* Implementation: Yuri Carmona
27
*/
28
29
#include <WaspXBeeZB.h>
30
31
// node ID
32
char nodeID[] = "node_RX";
33
34
// define variable
35
uint8_t error;
36
37
38
39
void setup()
40
{
41
// init USB port
42
USB.ON();
43
USB.println(F("Node search example (RX node)"));
44
45
//////////////////////////
46
// 1. XBee set up
47
//////////////////////////
48
49
// 1.1. init XBee
50
xbeeZB.ON();
51
52
// 1.2. set NI (Node Identifier)
53
xbeeZB.setNodeIdentifier( nodeID );
54
55
// check the AT commmand execution flag
56
if( xbeeZB.error_AT == 0 )
57
{
58
USB.println(F("Node ID set OK"));
59
}
60
else
61
{
62
USB.println(F("Error calling 'setNodeIdentifier()'"));
63
}
64
65
// 1.3. save XBee values in memory
66
xbeeZB.writeValues();
67
68
69
//////////////////////////
70
// 2. check XBee's network parameters
71
//////////////////////////
72
USB.println(F("Check Network parameters:"));
73
checkNetworkParams();
74
75
}
76
77
78
79
void loop()
80
{
81
// receive XBee packet (wait for 10 seconds)
82
error = xbeeZB.receivePacketTimeout( 10000 );
83
84
// check answer
85
if( error == 0 )
86
{
87
// Show data stored in '_payload' buffer indicated by '_length'
88
USB.print(F("Data: "));
89
USB.println( xbeeZB._payload, xbeeZB._length);
90
91
// Show data stored in '_payload' buffer indicated by '_length'
92
USB.print(F("Length: "));
93
USB.println( xbeeZB._length,DEC);
94
}
95
else
96
{
97
// Print error message:
98
/*
99
* '7' : Buffer full. Not enough memory space
100
* '6' : Error escaping character within payload bytes
101
* '5' : Error escaping character in checksum byte
102
* '4' : Checksum is not correct
103
* '3' : Checksum byte is not available
104
* '2' : Frame Type is not valid
105
* '1' : Timeout when receiving answer
106
*/
107
USB.print(F("Error receiving a packet:"));
108
USB.println(error,DEC);
109
}
110
111
}
112
113
114
115
116
/*******************************************
117
*
118
* checkNetworkParams - Check operating
119
* network parameters in the XBee module
120
*
121
*******************************************/
122
void checkNetworkParams()
123
{
124
// 1. get operating 64-b PAN ID
125
xbeeZB.getOperating64PAN();
126
127
// 2. wait for association indication
128
xbeeZB.getAssociationIndication();
129
130
while( xbeeZB.associationIndication != 0 )
131
{
132
delay(2000);
133
134
// get operating 64-b PAN ID
135
xbeeZB.getOperating64PAN();
136
137
USB.print(F("operating 64-b PAN ID: "));
138
USB.printHex(xbeeZB.operating64PAN[0]);
139
USB.printHex(xbeeZB.operating64PAN[1]);
140
USB.printHex(xbeeZB.operating64PAN[2]);
141
USB.printHex(xbeeZB.operating64PAN[3]);
142
USB.printHex(xbeeZB.operating64PAN[4]);
143
USB.printHex(xbeeZB.operating64PAN[5]);
144
USB.printHex(xbeeZB.operating64PAN[6]);
145
USB.printHex(xbeeZB.operating64PAN[7]);
146
USB.println();
147
148
xbeeZB.getAssociationIndication();
149
}
150
151
USB.println(F("\nJoined a network!"));
152
153
// 3. get network parameters
154
xbeeZB.getOperating16PAN();
155
xbeeZB.getOperating64PAN();
156
xbeeZB.getChannel();
157
158
USB.print(F("operating 16-b PAN ID: "));
159
USB.printHex(xbeeZB.operating16PAN[0]);
160
USB.printHex(xbeeZB.operating16PAN[1]);
161
USB.println();
162
163
USB.print(F("operating 64-b PAN ID: "));
164
USB.printHex(xbeeZB.operating64PAN[0]);
165
USB.printHex(xbeeZB.operating64PAN[1]);
166
USB.printHex(xbeeZB.operating64PAN[2]);
167
USB.printHex(xbeeZB.operating64PAN[3]);
168
USB.printHex(xbeeZB.operating64PAN[4]);
169
USB.printHex(xbeeZB.operating64PAN[5]);
170
USB.printHex(xbeeZB.operating64PAN[6]);
171
USB.printHex(xbeeZB.operating64PAN[7]);
172
USB.println();
173
174
USB.print(F("channel: "));
175
USB.printHex(xbeeZB.channel);
176
USB.println();
177
178
}
Copied!

Output

1
E#
2
Node search example (RX node)
3
Node ID set OK
4
Check Network parameters:
5
6
Joined a network!
7
operating 16-b PAN ID: D27C
8
operating 64-b PAN ID: 84D87D34C4AE4CB1
9
channel: 0B
10
Data: Message_from_TX_node
11
Length: 20
12
Data: Message_from_TX_node
13
Length: 20
14
...
Copied!
Last modified 2yr ago