ZB 02a: Join known network
This program shows how to join a router device to a known network. It is mandatory to known the operating 64-bit PAN ID the coordinator is running on.

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_02a] - router joins a known network --------
3
4
Explanation: This program shows how to join a router device to
5
a known network. It is mandatory to known the operating 64-bit
6
PAN ID the coordinator is running on.
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
/**************************************************
32
IMPORTANT: Before running this example, it is
33
necessary to set a coordinator to the known PANID
34
**************************************************/
35
36
// known coordinator's operating 64-bit PAN ID to set
37
////////////////////////////////////////////////////////////////////////
38
uint8_t PANID[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
39
////////////////////////////////////////////////////////////////////////
40
41
42
void setup()
43
{
44
// open USB
45
USB.ON();
46
USB.println(F("ZB_02a example"));
47
48
49
///////////////////////////////////////////////
50
// Init XBee
51
///////////////////////////////////////////////
52
xbeeZB.ON();
53
54
55
///////////////////////////////////////////////
56
// 1. Disable Coordinator mode
57
///////////////////////////////////////////////
58
59
/*************************************
60
WARNING: Only XBee ZigBee S2C and
61
XBee ZigBee S2D are able to use
62
this function properly
63
************************************/
64
xbeeZB.setCoordinator(DISABLED);
65
66
// check at command flag
67
if (xbeeZB.error_AT == 0)
68
{
69
USB.println(F("1. Coordinator mode disabled"));
70
}
71
else
72
{
73
USB.println(F("1. Error while disabling Coordinator mode"));
74
}
75
76
77
///////////////////////////////////////////////
78
// 2. Set PANID
79
///////////////////////////////////////////////
80
xbeeZB.setPAN(PANID);
81
82
// check at command flag
83
if (xbeeZB.error_AT == 0)
84
{
85
USB.println(F("2. PANID set OK"));
86
}
87
else
88
{
89
USB.println(F("2. Error while setting PANID"));
90
}
91
92
93
///////////////////////////////////////////////
94
// 3. Set channels to be scanned before creating network
95
///////////////////////////////////////////////
96
// channels from 0x0B to 0x18 (0x19 and 0x1A are excluded)
97
/* Range:[0x0 to 0x3FFF]
98
Channels are scpedified as a bitmap where depending on
99
the bit a channel is selected --> Bit (Channel):
100
0 (0x0B) 4 (0x0F) 8 (0x13) 12 (0x17)
101
1 (0x0C) 5 (0x10) 9 (0x14) 13 (0x18)
102
2 (0x0D) 6 (0x11) 10 (0x15)
103
3 (0x0E) 7 (0x12) 11 (0x16) */
104
xbeeZB.setScanningChannels(0x3F, 0xFF);
105
106
// check at command flag
107
if (xbeeZB.error_AT == 0)
108
{
109
USB.println(F("3. Scanning channels set OK"));
110
}
111
else
112
{
113
USB.println(F("3. Error while setting 'Scanning channels'"));
114
}
115
116
117
///////////////////////////////////////////////
118
// Save values
119
///////////////////////////////////////////////
120
xbeeZB.writeValues();
121
122
// wait for the module to set the parameters
123
delay(10000);
124
USB.println();
125
126
}
127
128
129
void loop()
130
{
131
///////////////////////////////
132
// get network parameters
133
///////////////////////////////
134
135
xbeeZB.getOperating16PAN();
136
xbeeZB.getOperating64PAN();
137
xbeeZB.getChannel();
138
139
USB.print(F("operatingPAN: "));
140
USB.printHex(xbeeZB.operating16PAN[0]);
141
USB.printHex(xbeeZB.operating16PAN[1]);
142
USB.println();
143
144
USB.print(F("extendedPAN: "));
145
USB.printHex(xbeeZB.operating64PAN[0]);
146
USB.printHex(xbeeZB.operating64PAN[1]);
147
USB.printHex(xbeeZB.operating64PAN[2]);
148
USB.printHex(xbeeZB.operating64PAN[3]);
149
USB.printHex(xbeeZB.operating64PAN[4]);
150
USB.printHex(xbeeZB.operating64PAN[5]);
151
USB.printHex(xbeeZB.operating64PAN[6]);
152
USB.printHex(xbeeZB.operating64PAN[7]);
153
USB.println();
154
155
USB.print(F("channel: "));
156
USB.printHex(xbeeZB.channel);
157
USB.println();
158
159
delay(3000);
160
}
Copied!

Output

1
E#
2
ZB_02a example
3
PANID set OK
4
operatingPAN: 7C7A
5
extendedPAN: 1122334455667788
6
channel: 0D
7
...
Copied!
Last modified 2yr ago