-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxBEE.cpp
202 lines (179 loc) · 4.07 KB
/
xBEE.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <Arduino.h>
#include <xBEE.h>
/**
Creates a new instance of the xBEE class.
*/
xBEE::xBEE()
{
_ni = "";
_ky = "0";
}
/**
Creates a new instance of the xBEE class.
@ni string reprensenting the Node Identifier.
*/
xBEE::xBEE(String ni)
{
_ni = ni;
_ky = "";
}
/**
Creates a new instance of the xBEE class.
@ni string reprensenting the Node Identifier.
@ky string representing the AES Encryption Key.
*/
xBEE::xBEE(String ni, String ky)
{
_ni = ni;
_ky = ky;
}
/**
Configures the xBEE module to auto configure and auto-associate to a coordinator.
*/
void xBEE::AutoConfigure()
{
startCommandMode();
String ee;
if ( _ky.length() == 0 )
{
ee = "0";
}
else
{
ee = "1";
}
if (!(configureAndCheckResponse("RE", "", "OK") &&
configureAndCheckResponse("NI", _ni, "OK") &&
configureAndCheckResponse("EE", ee , "OK") &&
configureAndCheckResponse("KY", _ky , "OK") &&
configureAndCheckResponse("A1", "7", "OK") &&
configureAndCheckResponse("NO", "1", "OK") &&
configureAndCheckResponse("WR", "", "OK")))
{
return false;
}
while(!IsAssociated())
{
delay(500);
}
endCommandMode();
}
/**
Checks if the xBee is associated to a network.
@returns True if associated, false otherwise.
*/
bool xBEE::IsAssociated(){
return configureAndCheckResponse("AI", "", "0");
}
/**
Starts the configuration of xBEE module.
@returns True if configuration starts, false otherwise.
*/
bool xBEE::Configure()
{
startCommandMode();
}
/**
Writes the configuration to the xBEE module and finishes the configuration of xBEE module.
@returns True if configuration ends, false otherwise.
*/
bool xBEE::Start()
{
endCommandMode();
}
/**
Sets the value of the xBEE register.
@configuration the register name (e.g: DH, DL).
@value the register value in hex.
@returns True if configuration was set, false otherwise.
*/
bool xBEE::Set(String configuration, String value)
{
configureAndCheckResponse(configuration, value, "OK");
}
/**
Gets the value of the xBEE register.
@configuration the register name (e.g: DH, DL).
@returns The register value in hex.
*/
String xBEE::Get(String configuration)
{
sendConfiguration(configuration, "");
}
/**
Sends a message to a remote device specified by DH, DL.
@dl The Destination Low of the remote device.
@dh The Destination High of the remote device.
@content The message content
*/
void xBEE::Send(String dl, String dh, String content)
{
startCommandMode();
configureAndCheckResponse("DL", dl, "OK");
configureAndCheckResponse("DH", dh, "OK");
endCommandMode();
Serial.println(content);
}
/**
Sends a message to a remote device specified by MY.
@dl The MY of the remote device.
@content The message content
*/
void xBEE::Send(String dl, String content)
{
startCommandMode();
configureAndCheckResponse("DL", dl, "OK");
configureAndCheckResponse("DH", "0", "OK");
endCommandMode();
Serial.println(content);
}
/**
Sends a message.
@content The message content
*/
void xBEE::Send(String content)
{
Serial.println(content);
}
/**
Checks if a message is availiable on the xBEE module.
@retruns True if a message is availiable, False otherwise.
*/
bool xBEE::Available()
{
return Serial.available();
}
/**
Reads
@retruns The message content for the xBEE module.
*/
String xBEE::Read()
{
return Serial.readString();
}
void xBEE::startCommandMode(){
Serial.print("+++");
for(int i =0; i< 3; i++){
delay(1000);
}
}
void xBEE::endCommandMode(){
configureAndCheckResponse("WR", "", "OK") &&
configureAndCheckResponse("CN", "", "OK");
}
String xBEE::sendConfiguration(String configuration, String value) {
Serial.println("AT" + configuration + " " + value);
while (!Serial.available()) {
delay(100);
}
return Serial.readString();
}
bool xBEE::configureAndCheckResponse(String configuration, String value, String expectedResponse) {
String response = sendConfiguration(configuration, value);
if (response.indexOf(expectedResponse) >= 0) {
return true;
}
else {
return false;
}
}