ZB 02b: Join unkown network
This program shows how to search for a new coordinator when network parameters are unknown. It is necessary to scan a new coordinator setting different channels 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 - To store parameter changes after power cycles, it is needed to execute the "writeValues" function. If not, when the XBee is powered off, values do not remain in the module's memory.

Code

1
/*
2
------ [ZB_02b] - router joins an unknown coordinator --------
3
4
Explanation: This program shows how to search for a new coordinator
5
when network parameters are unknown. It is necessary to scan a new
6
coordinator setting different channels to be scanned
7
8
Copyright (C) 2016 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.2
25
Design: David Gascón
26
Implementation: Yuri Carmona
27
*/
28
29
#include <WaspXBeeZB.h>
30
31
// PAN ID to set in order to search a new coordinator
32
uint8_t PANID[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
33
34
35
void setup()
36
{
37
// init USB port
38
USB.ON();
39
USB.println(F("ZB_02b example"));
40
41
42
///////////////////////////////////////////////
43
// Init XBee
44
///////////////////////////////////////////////
45
xbeeZB.ON();
46
47
48
///////////////////////////////////////////////
49
// 1. Disable Coordinator mode
50
///////////////////////////////////////////////
51
52
/*************************************
53
WARNING: Only XBee ZigBee S2C and
54
XBee ZigBee S2D are able to use
55
this function properly
56
************************************/
57
xbeeZB.setCoordinator(DISABLED);
58
59
// check at command flag
60
if (xbeeZB.error_AT == 0)
61
{
62
USB.println(F("1. Coordinator mode disabled"));
63
}
64
else
65
{
66
USB.println(F("1. Error while disabling Coordinator mode"));
67
}
68
69
70
///////////////////////////////////////////////
71
// 2. Dissociation process
72
///////////////////////////////////////////////
73
74
/////////////////////////////////////
75
// 2.1. Set PANID: 0x0000000000000000
76
/////////////////////////////////////
77
xbeeZB.setPAN(PANID);
78
79
// check at command flag
80
if (xbeeZB.error_AT == 0)
81
{
82
USB.println(F("2.1. PANID set OK"));
83
}
84
else
85
{
86
USB.println(F("2.1. Error while setting PANID"));
87
}
88
89
90
/////////////////////////////////////
91
// 2.2. set all possible channels to scan
92
/////////////////////////////////////
93
// channels from 0x0B to 0x18 (0x19 and 0x1A are excluded)
94
/* Range:[0x0 to 0x3FFF]
95
Channels are scpedified as a bitmap where depending on
96
the bit a channel is selected --> Bit (Channel):
97
0 (0x0B) 4 (0x0F) 8 (0x13) 12 (0x17)
98
1 (0x0C) 5 (0x10) 9 (0x14) 13 (0x18)
99
2 (0x0D) 6 (0x11) 10 (0x15)
100
3 (0x0E) 7 (0x12) 11 (0x16) */
101
xbeeZB.setScanningChannels(0x3F, 0xFF);
102
103
// check AT command flag
104
if ( xbeeZB.error_AT == 0 )
105
{
106
USB.println(F("2.2. Scanning channels set OK"));
107
}
108
else
109
{
110
USB.println(F("2.2. Error while setting 'Scanning channels'"));
111
}
112
113
114
/////////////////////////////////////
115
// 2.3. Set channel verification JV=1
116
// in order to make the module to scan
117
// a new coordinator
118
/////////////////////////////////////
119
xbeeZB.setChannelVerification(1);
120
121
// check AT command flag
122
if ( xbeeZB.error_AT == 0 )
123
{
124
USB.println(F("2.3. Verification channel set OK"));
125
}
126
else
127
{
128
USB.println(F("2.3. Error while setting verification channel"));
129
}
130
131
132
///////////////////////////////////////////////
133
// Save values
134
///////////////////////////////////////////////
135
xbeeZB.writeValues();
136
137
138
///////////////////////////////////////////////
139
// Reboot XBee module
140
///////////////////////////////////////////////
141
xbeeZB.OFF();
142
delay(3000);
143
xbeeZB.ON();
144
145
delay(3000);
146
147
148
149
/////////////////////////////////////
150
// 3. Wait for Association
151
/////////////////////////////////////
152
153
// Wait for association indication
154
xbeeZB.getAssociationIndication();
155
156
while ( xbeeZB.associationIndication != 0 )
157
{
158
delay(2000);
159
160
// get operating 64-b PAN ID
161
xbeeZB.getOperating64PAN();
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
xbeeZB.getAssociationIndication();
175
}
176
177
178
USB.println(F("\n\nJoined a coordinator!"));
179
180
// 2.2. When XBee is associated print all network
181
// parameters unset channel verification JV=0
182
xbeeZB.setChannelVerification(0);
183
xbeeZB.writeValues();
184
185
// 2.3. get network parameters
186
xbeeZB.getOperating16PAN();
187
xbeeZB.getOperating64PAN();
188
xbeeZB.getChannel();
189
190
USB.print(F("Operating 16-b PAN ID: "));
191
USB.printHex(xbeeZB.operating16PAN[0]);
192
USB.printHex(xbeeZB.operating16PAN[1]);
193
USB.println();
194
195
USB.print(F("Operating 64-b PAN ID: "));
196
USB.printHex(xbeeZB.operating64PAN[0]);
197
USB.printHex(xbeeZB.operating64PAN[1]);
198
USB.printHex(xbeeZB.operating64PAN[2]);
199
USB.printHex(xbeeZB.operating64PAN[3]);
200
USB.printHex(xbeeZB.operating64PAN[4]);
201
USB.printHex(xbeeZB.operating64PAN[5]);
202
USB.printHex(xbeeZB.operating64PAN[6]);
203
USB.printHex(xbeeZB.operating64PAN[7]);
204
USB.println();
205
206
USB.print(F("Channel: "));
207
USB.printHex(xbeeZB.channel);
208
USB.println();
209
210
}
211
212
213
void loop()
214
{
215
// Do nothing
216
}
Copied!

Output

1
E#
2
ZB_02b example
3
PANID set OK
4
scanning channels set OK
5
verification channel set OK
6
operating 64-b PAN ID: 0000000000000000
7
operating 64-b PAN ID: 0000000000000000
8
operating 64-b PAN ID: 1122334455667788
9
10
11
Joined a coordinator!
12
operating 16-b PAN ID: 7C7A
13
operating 64-b PAN ID: 1122334455667788
14
channel: 0E
Copied!
Last modified 2yr ago