DM 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 1 x Battery 1 x MiniUSB wire 1 x XBee-Digimesh

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

Code

1
/*
2
* ------ [DM_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) 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.2
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
// Digimesh 2.4Ghz. Range from 0x0B to 0x1A
36
// Digimesh 900Mhz. Range from 0x00 to 0x0B
37
uint8_t channel = 0x0F;
38
39
// Define the Encryption mode: 1 (enabled) or 0 (disabled)
40
uint8_t encryptionMode = 0;
41
42
// Define the AES 16-byte Encryption Key
43
char encryptionKey[] = "WaspmoteLinkKey!";
44
45
46
47
void setup()
48
{
49
// open USB port
50
USB.ON();
51
52
USB.println(F("-------------------------------"));
53
USB.println(F("Configure XBee Digimesh"));
54
USB.println(F("-------------------------------"));
55
56
57
// init XBee
58
xbeeDM.ON();
59
60
/////////////////////////////////////
61
// 1. set channel
62
/////////////////////////////////////
63
xbeeDM.setChannel( channel );
64
65
// check at commmand execution flag
66
if( xbeeDM.error_AT == 0 )
67
{
68
USB.print(F("1. Channel set OK to: 0x"));
69
USB.printHex( xbeeDM.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
xbeeDM.setPAN( panID );
82
83
// check the AT commmand execution flag
84
if( xbeeDM.error_AT == 0 )
85
{
86
USB.print(F("2. PAN ID set OK to: 0x"));
87
USB.printHex( xbeeDM.PAN_ID[0] );
88
USB.printHex( xbeeDM.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
xbeeDM.setEncryptionMode( encryptionMode );
100
101
// check the AT commmand execution flag
102
if( xbeeDM.error_AT == 0 )
103
{
104
USB.print(F("3. AES encryption configured (1:enabled; 0:disabled):"));
105
USB.println( xbeeDM.encryptMode, DEC );
106
}
107
else
108
{
109
USB.println(F("3. Error calling 'setEncryptionMode()'"));
110
}
111
112
/////////////////////////////////////
113
// 4. set encryption key
114
/////////////////////////////////////
115
xbeeDM.setLinkKey( encryptionKey );
116
117
// check the AT commmand execution flag
118
if( xbeeDM.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
xbeeDM.writeValues();
131
132
// check the AT commmand execution flag
133
if( xbeeDM.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
void loop()
146
{
147
148
/////////////////////////////////////
149
// 1. get channel
150
/////////////////////////////////////
151
xbeeDM.getChannel();
152
USB.print(F("channel: "));
153
USB.printHex(xbeeDM.channel);
154
USB.println();
155
156
/////////////////////////////////////
157
// 2. get PAN ID
158
/////////////////////////////////////
159
xbeeDM.getPAN();
160
USB.print(F("panid: "));
161
USB.printHex(xbeeDM.PAN_ID[0]);
162
USB.printHex(xbeeDM.PAN_ID[1]);
163
USB.println();
164
165
/////////////////////////////////////
166
// 3. get Encryption mode (1:enable; 0:disable)
167
/////////////////////////////////////
168
xbeeDM.getEncryptionMode();
169
USB.print(F("encryption mode: "));
170
USB.printHex(xbeeDM.encryptMode);
171
USB.println();
172
173
USB.println(F("-------------------------------"));
174
175
delay(3000);
176
}
177
178
Copied!

Output

1
E#
2
-------------------------------
3
Configure XBee Digimesh
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
-------------------------------
19
...
Copied!
Last modified 1yr ago