802 01: Configure XBee
This program shows how to configure basic XBee parameters in order to communicate between different XBee devices using the same network.

Required Materials

1 x Waspmote PRO 1 x Battery 1 x MiniUSB wire 1 x XBee-802.15.4 module

Notes

- The XBee module must be configured to the default baud rate (BD=7) and AP parameter (AP=2). - 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. - The battery has to be connected. - This example can be executed in Waspmote v12 and Waspmote v15

Code

1
/*
2
* ------ [802_01] - configure XBee basic parameters --------
3
*
4
* Explanation: This program shows how to configure basic XBee
5
* parameters in order to communicate between different XBee
6
* devices using the same network.
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: 3.0
25
* Design: David Gascón
26
* Implementation: Yuri Carmona
27
*/
28
29
#include
30
31
// PAN (Personal Area Network) Identifier
32
uint8_t panID[2] = {0x12,0x34};
33
34
// Define Freq Channel to be set:
35
// Center Frequency = 2.405 + (CH - 11d) * 5 MHz
36
// Range: 0x0B - 0x1A (XBee)
37
// Range: 0x0C - 0x17 (XBee-PRO)
38
uint8_t channel = 0x0F;
39
40
// Define the Encryption mode: 1 (enabled) or 0 (disabled)
41
uint8_t encryptionMode = 0;
42
43
// Define the AES 16-byte Encryption Key
44
char encryptionKey[] = "WaspmoteLinkKey!";
45
46
47
void setup()
48
{
49
// open USB port
50
USB.ON();
51
52
USB.println(F("-------------------------------"));
53
USB.println(F("Configure XBee 802.15.4"));
54
USB.println(F("-------------------------------"));
55
56
// init XBee
57
xbee802.ON();
58
59
60
/////////////////////////////////////
61
// 1. set channel
62
/////////////////////////////////////
63
xbee802.setChannel( channel );
64
65
// check at commmand execution flag
66
if( xbee802.error_AT == 0 )
67
{
68
USB.print(F("1. Channel set OK to: 0x"));
69
USB.printHex( xbee802.channel );
70
USB.println();
71
}
72
else
73
{
74
USB.println(F("1. Error calling 'setChannel()'"));
75
}
76
77
78
/////////////////////////////////////
79
// 2. set PANID
80
/////////////////////////////////////
81
xbee802.setPAN( panID );
82
83
// check the AT commmand execution flag
84
if( xbee802.error_AT == 0 )
85
{
86
USB.print(F("2. PAN ID set OK to: 0x"));
87
USB.printHex( xbee802.PAN_ID[0] );
88
USB.printHex( xbee802.PAN_ID[1] );
89
USB.println();
90
}
91
else
92
{
93
USB.println(F("2. Error calling 'setPAN()'"));
94
}
95
96
/////////////////////////////////////
97
// 3. set encryption mode (1:enable; 0:disable)
98
/////////////////////////////////////
99
xbee802.setEncryptionMode( encryptionMode );
100
101
// check the AT commmand execution flag
102
if( xbee802.error_AT == 0 )
103
{
104
USB.print(F("3. AES encryption configured (1:enabled; 0:disabled):"));
105
USB.println( xbee802.encryptMode, DEC );
106
}
107
else
108
{
109
USB.println(F("3. Error calling 'setEncryptionMode()'"));
110
}
111
112
/////////////////////////////////////
113
// 4. set encryption key
114
/////////////////////////////////////
115
xbee802.setLinkKey( encryptionKey );
116
117
// check the AT commmand execution flag
118
if( xbee802.error_AT == 0 )
119
{
120
USB.println(F("4. AES encryption key set OK"));
121
}
122
else
123
{
124
USB.println(F("4. Error calling 'setLinkKey()'"));
125
}
126
127
/////////////////////////////////////
128
// 5. write values to XBee module memory
129
/////////////////////////////////////
130
xbee802.writeValues();
131
132
// check the AT commmand execution flag
133
if( xbee802.error_AT == 0 )
134
{
135
USB.println(F("5. Changes stored OK"));
136
}
137
else
138
{
139
USB.println(F("5. Error calling 'writeValues()'"));
140
}
141
142
USB.println(F("-------------------------------"));
143
}
144
145
146
147
void loop()
148
{
149
150
/////////////////////////////////////
151
// 1. get channel
152
/////////////////////////////////////
153
xbee802.getChannel();
154
USB.print(F("channel: "));
155
USB.printHex(xbee802.channel);
156
USB.println();
157
158
/////////////////////////////////////
159
// 2. get PANID
160
/////////////////////////////////////
161
xbee802.getPAN();
162
USB.print(F("panid: "));
163
USB.printHex(xbee802.PAN_ID[0]);
164
USB.printHex(xbee802.PAN_ID[1]);
165
USB.println();
166
167
/////////////////////////////////////
168
// 3. get encryption mode (1:enable; 0:disable)
169
/////////////////////////////////////
170
xbee802.getEncryptionMode();
171
USB.print(F("encryption mode: "));
172
USB.printHex(xbee802.encryptMode);
173
USB.println();
174
175
USB.println(F("-------------------------------"));
176
177
delay(3000);
178
}
Copied!

Output

1
H#
2
-------------------------------
3
Configure XBee 802.15.4
4
-------------------------------
5
1. Channel set OK to: 0x0F
6
2. PAN ID set OK to: 0x1234
7
3. AES encryption configured (1:enabled; 0:disabled):0
8
4. AES encryption key set OK
9
5. Changes stored OK
10
-------------------------------
11
channel: 0F
12
panid: 1234
13
encryption mode: 00
14
-------------------------------
15
channel: 0F
16
panid: 1234
17
encryption mode: 00
18
-------------------------------
Copied!
Last modified 1yr ago