-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxbee_http_example.ino
189 lines (156 loc) · 4.25 KB
/
xbee_http_example.ino
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
#include <SD.h>
#include <Wire.h>
#include <EEPROM.h>
#include <SoftwareSerial.h>
#include "xbee.h"
#include "xbee_print.h"
// Pins used:
// Digital
// 2 - XBee DIN
#define XBEE_DIN 2
// 3 - XBee DOUT
#define XBEE_DOUT 3
#define WIFI_SSID "ssid"
#define WIFI_PASSWORD "password"
class CallbacksExample
{
public:
CallbacksExample(XbeeService* xbee)
: _xbee(xbee)
{
}
void setup()
{
// Setup callbacks.
_xbee->setAtCallback(closure_bind(this, &CallbacksExample::processStartupAtCommand));
_xbee->setRequestCallback(closure_bind(this, &CallbacksExample::processIpRequest));
// Get current time from NTP server.
_xbee->requestCurrentTime();
}
private:
// Processes HTTP requests.
void processIpRequest(IpAddress* address, HttpService::RequestType type, uint8_t* location, int locationLength, uint8_t* data, int dataLength)
{
// Process time responses.
if (_xbee->isTimeResponse(-4, address, type, data, dataLength, _hours, _minutes, _seconds))
{
Serial.print("Time is ");
Serial.print(_hours);
Serial.print(":");
Serial.print(_minutes);
Serial.print(":");
Serial.println(_seconds);
}
else
{
// Check type of HTTP request.
if (type == HttpService::request_post)
{
// Return something back.
_xbee->ipSend(address, XbeeService::http_found_non_cached, (uint8_t*)"POST not supported");
}
// Process get requests.
if (type == HttpService::request_get)
{
// Get current time.
if (locationLength >= 5 && memcmp(location, "/time", 5) == 0)
{
// Use printing class.
XbeePrint r(_xbee, address, XbeeService::http_found_non_cached);
r.print(_hours);
r.print(":");
r.println(_minutes);
// Packet is sent when XbeePrint instance is destroyed.
}
else
{
// Reply something for all other requests, otherwise requestor will hang until TCP timeout.
_xbee->ipSend(address, XbeeService::http_found_non_cached, (uint8_t*)"OK");
}
}
}
}
// Processes async AT commands.
void processStartupAtCommand(uint16_t atCommand, uint8_t frame, uint8_t status, uint8_t* data, int dataLength)
{
}
private:
uint8_t _hours;
uint8_t _minutes;
uint8_t _seconds;
XbeeService* _xbee;
};
XbeeService _xbee(XBEE_DIN, XBEE_DOUT);
CallbacksExample _example(&_xbee);
void printHex(const uint8_t* data, uint8_t count)
{
for (; count != 0; --count, ++data)
Serial.print(*data, HEX);
}
void printChar(const uint8_t* data, uint8_t count)
{
for (; count != 0; --count, ++data)
Serial.print((char)*data);
}
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
_xbee.setup();
// Reset xbee just in case, for consistency.
if (_xbee.atCommandSync("FR"))
{
Serial.println("Reset...");
delay(4000);
}
// Set wifi network SSID.
if (!_xbee.atCommandSync(WIFI_SSID))
Serial.println("Error settings SSID");
// Set wifi password.
if (!_xbee.atCommandSync(WIFI_PASSWORD))
Serial.println("Error setting password");
// Apply changes.
if (!_xbee.atCommandSync("AC"))
Serial.println("Error");
// Get software version.
if (_xbee.atCommandSync("VR"))
{
Serial.print("SV: ");
printHex(_xbee.atBufferSync(), _xbee.atBufferCountSync());
Serial.println();
}
// Wait till XBee joined network.
for (;;)
{
if (_xbee.atCommandSync("AI"))
{
if (_xbee.atBufferCountSync() > 0 && _xbee.atBufferSync()[0] == 0x0)
{
break;
}
}
// Still in process of connecting to AP, request status.
delay(250);
}
// Get current IP address.
if (_xbee.atCommandSync("MY"))
{
Serial.print("IP: ");
printChar(_xbee.atBufferSync(), _xbee.atBufferCountSync());
Serial.println();
}
// Get current listening port.
if (_xbee.atCommandSync("C0"))
{
Serial.print("Port: ");
Serial.println((_xbee.atBufferSync()[0] << 8) +_xbee.atBufferSync()[1]);
}
// Initialize our example app.
_example.setup();
}
void loop()
{
unsigned long mills = millis();
// XBee update method must be called from time to time.
_xbee.update(mills);
}