ZB 12a: Encryption in Coordinator
This program shows how to configure the encryption mode in the XBee module (COORDINATOR only)

Required Materials

1 x Waspmote 1 x Battery 1 x MiniUSB wire 1 x XBee-ZigBee module (Coordinator firmware only)

Notes

- Coordinator firmwares must be used only with this example - The battery has to be connected. - This example can be executed in Waspmote v12

Code

1
/*
2
* ------ [ZB_12a] - enable encryption in XBee module --------
3
*
4
* Explanation: This program shows how to configure the encryption
5
* mode in the XBee module
6
*
7
* Copyright (C) 2015 Libelium Comunicaciones Distribuidas S.L.
8
* http://www.libelium.com
9
*
10
* This program is free software: you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation, either version 3 of the License, or
13
* (at your option) any later version.
14
*
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
19
*
20
* You should have received a copy of the GNU General Public License
21
* along with this program. If not, see .
22
*
23
* Version: 0.2
24
* Design: David Gascón
25
* Implementation: Yuri Carmona
26
*/
27
28
#include <WaspXBeeZB.h>
29
30
// 16-Byte Network Encryption Key
31
char NETKEY[] = "WaspmoteNetwork!";
32
33
// 16-Byte Link key
34
char LINKKEY[] = "WaspmoteLinkKey!";
35
36
void setup()
37
{
38
// init USB port
39
USB.ON();
40
USB.println(F("ZB_12a example"));
41
42
////////////////////////////////////////////////////////
43
// 1. init XBee
44
////////////////////////////////////////////////////////
45
xbeeZB.ON();
46
47
delay(3000);
48
49
////////////////////////////////////////////////////////
50
// 2. check XBee's network parameters
51
////////////////////////////////////////////////////////
52
checkNetworkParams();
53
54
USB.println(F("---------------------------------"));
55
}
56
57
void loop()
58
{
59
////////////////////////////////////////////////////////
60
// 1. Enabling security
61
////////////////////////////////////////////////////////
62
63
// 1.1. set encryption mode
64
xbeeZB.setEncryptionMode(1);
65
66
// 1.2. check flag
67
if( xbeeZB.error_AT == 0 )
68
{
69
USB.println(F("1. Security enabled"));
70
}
71
else
72
{
73
USB.println(F("1. Error while enabling security"));
74
}
75
76
77
////////////////////////////////////////////////////////
78
// 2. Setting pre-configured Link Key
79
////////////////////////////////////////////////////////
80
81
// 2.1. Setting pre-configured Link Key
82
xbeeZB.setLinkKey(LINKKEY);
83
84
// 2.2. check flag
85
if( xbeeZB.error_AT == 0 )
86
{
87
USB.println(F("2. Link Key set OK"));
88
}
89
else
90
{
91
USB.println(F("2. Error while setting Key"));
92
}
93
94
95
////////////////////////////////////////////////////////
96
// 3. Configuring Trust Center
97
////////////////////////////////////////////////////////
98
99
// 3.1. Configuring Trust Center
100
xbeeZB.setEncryptionOptions(0x02);
101
102
// 3.2. check flag
103
if( xbeeZB.error_AT == 0 )
104
{
105
USB.println(F("3. Security options configured"));
106
}
107
else
108
{
109
USB.println(F("3. Error while configuring security"));
110
}
111
112
113
////////////////////////////////////////////////////////
114
// 4. Setting Network Key (ONLY IN COORDINATOR)
115
////////////////////////////////////////////////////////
116
117
// 4.1. Routers and End Devices get this key from coordinator
118
// when they join the network always they share the correct
119
// pre-configured link key
120
xbeeZB.setNetworkKey(NETKEY);
121
122
// 4.2. check flag
123
if( xbeeZB.error_AT == 0 )
124
{
125
USB.println(F("4. Network Key set OK"));
126
}
127
else
128
{
129
USB.println(F("4. Error while setting Key"));
130
}
131
132
133
////////////////////////////////////////////////////////
134
// 5. Setting APS encryption
135
////////////////////////////////////////////////////////
136
137
// 5.1. APS encryption is an optional layer of security that
138
// uses the link key to encrypt the data payload.
139
// The XBee must be configured with security enabled
140
// (call xbeeZB.encryptionMode(1) to 1) to use APS encryption
141
xbeeZB.setAPSencryption(XBEE_ON);
142
143
// 5.2. check flag
144
if( xbeeZB.error_AT == 0 )
145
{
146
USB.println(F("5. APS Encryption set OK"));
147
}
148
else
149
{
150
USB.println(F("5. Error while setting APS Encryption"));
151
}
152
153
154
////////////////////////////////////////////////////////
155
// 6. Save values
156
////////////////////////////////////////////////////////
157
158
// 6.1. Keep values
159
xbeeZB.writeValues();
160
161
// 6.2. check flag
162
if( xbeeZB.error_AT == 0 )
163
{
164
USB.println(F("6. Changes stored OK"));
165
}
166
else
167
{
168
USB.println(F("6. Error while storing values"));
169
}
170
171
172
USB.println(F("---------------------------------"));
173
delay(10000);
174
}
175
176
177
178
179
/*******************************************
180
*
181
* checkNetworkParams - Check operating
182
* network parameters in the XBee module
183
*
184
*******************************************/
185
void checkNetworkParams()
186
{
187
// 1. get operating 64-b PAN ID
188
xbeeZB.getOperating64PAN();
189
190
// 2. wait for association indication
191
xbeeZB.getAssociationIndication();
192
193
while( xbeeZB.associationIndication != 0 )
194
{
195
delay(2000);
196
197
// get operating 64-b PAN ID
198
xbeeZB.getOperating64PAN();
199
200
USB.print(F("operating 64-b PAN ID: "));
201
USB.printHex(xbeeZB.operating64PAN[0]);
202
USB.printHex(xbeeZB.operating64PAN[1]);
203
USB.printHex(xbeeZB.operating64PAN[2]);
204
USB.printHex(xbeeZB.operating64PAN[3]);
205
USB.printHex(xbeeZB.operating64PAN[4]);
206
USB.printHex(xbeeZB.operating64PAN[5]);
207
USB.printHex(xbeeZB.operating64PAN[6]);
208
USB.printHex(xbeeZB.operating64PAN[7]);
209
USB.println();
210
211
xbeeZB.getAssociationIndication();
212
}
213
214
USB.println(F("\nJoined a network!"));
215
216
// 3. get network parameters
217
xbeeZB.getOperating16PAN();
218
xbeeZB.getOperating64PAN();
219
xbeeZB.getChannel();
220
221
USB.print(F("operating 16-b PAN ID: "));
222
USB.printHex(xbeeZB.operating16PAN[0]);
223
USB.printHex(xbeeZB.operating16PAN[1]);
224
USB.println();
225
226
USB.print(F("operating 64-b PAN ID: "));
227
USB.printHex(xbeeZB.operating64PAN[0]);
228
USB.printHex(xbeeZB.operating64PAN[1]);
229
USB.printHex(xbeeZB.operating64PAN[2]);
230
USB.printHex(xbeeZB.operating64PAN[3]);
231
USB.printHex(xbeeZB.operating64PAN[4]);
232
USB.printHex(xbeeZB.operating64PAN[5]);
233
USB.printHex(xbeeZB.operating64PAN[6]);
234
USB.printHex(xbeeZB.operating64PAN[7]);
235
USB.println();
236
237
USB.print(F("channel: "));
238
USB.printHex(xbeeZB.channel);
239
USB.println();
240
241
}
242
Copied!

Output

1
E#
2
ZB_12a example
3
4
Joined a network!
5
operating 16-b PAN ID: B255
6
operating 64-b PAN ID: EC8CE63EB764C270
7
channel: 0B
8
---------------------------------
9
1. Security enabled
10
2. Link Key set OK
11
3. Security options configured
12
4. Error while setting Key
13
5. APS Encryption set OK
14
6. Changes stored OK
15
---------------------------------
16
...
Copied!
Last modified 2yr ago