-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsensall.ino
237 lines (197 loc) · 6.05 KB
/
sensall.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <SPI.h>
#include <Ethernet.h>
// MAC address
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// Ethernet Shield
//byte mac[] = {
// 0x90, 0xA2, 0xDA, 0x0D, 0x3F, 0xD1 };
// Ethernet Uno
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0D, 0x60, 0x15 };
char serverName[] = "api.pushingbox.com";
const char devid[] = "v56F4F2A48FCB259"; //Scenario
const char sensorID[] = "garageDoor";
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
EthernetServer server(80);
// xbee serial setup
String packet = String("");
boolean isPacket = false; // flag to track if we are looking at a packet
boolean packetComplete = false;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// reserve 200 bytes for the inputString:
packet.reserve(200);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("ready");
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient serverClient = server.available();
if (serverClient) {
//Serial.println("new client");
Serial.print('\n');
// an http request ends with a blank line
boolean currentLineIsBlank = true;
char request[60]; // handle up to 60 chars
memset(request, 0, 60);
int requestIndex = 0;
boolean firstLine = true;
//const char garageDoorRequest[] = "GET /garageDoor HTTP/1.1";
boolean validRequest = false;
while (serverClient.connected()) {
if (serverClient.available()) {
char c = serverClient.read();
//Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
serverClient.println("HTTP/1.1 200 OK");
serverClient.println("Content-Type: text/html");
serverClient.println("Connection: close");
serverClient.println();
serverClient.println("<!DOCTYPE HTML>");
serverClient.println("<html>");
if (validRequest) {
serverClient.print("Valid Request for: ");
serverClient.print(request);
serverClient.print(" has been initiated!");
}
else {
serverClient.print("Request for: ");
serverClient.print(request);
serverClient.print(" failed to match a valid sensor");
}
serverClient.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
// if this is still the first line
if (firstLine) {
// debug
//Serial.print("Request: ");
//Serial.println(request);
// if the request is for something I know, broadcast the status request on serial
// TODO: remove hardcoding and pass off to a parser function.
if (strcmp(request, "GET /garageDoor HTTP/1.1") == 0) {
delay(1000);
Serial.println("^{id:garageDoor;action:status}");
validRequest = true;
}
firstLine = false; // no more first line
}
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
if (firstLine) {
request[requestIndex] = c;
requestIndex++;
}
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
serverClient.stop();
//Serial.println("client disonnected");
}
if (packetComplete == true) {
boolean didSend = parsePacket();
if (didSend == false) {
Serial.println("failure");
}
packetComplete = false;
}
//delay(10);
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = Serial.read();
//find the beginning of a packet
if (inChar == '{') {
isPacket = true;
packet = "";
}
// if it is a packet, add characters
if (isPacket) {
packet += inChar;
//Serial.print("isPacket");
}
// find the end of the packet and kick off a response
if (inChar == '}') {
packetComplete = true;
isPacket = false;
}
}
}
boolean parsePacket() {
boolean result = false;
if (packet.substring(23,29) == "closed") {
Serial.println("closed");
result = sendStatus("closed");
}
if (packet.substring(23,27) == "open") {
Serial.println("open");
result = sendStatus("open");
}
return result;
}
boolean sendStatus( char *state)
{
boolean result = false;
Serial.println("connecting ...");
if (client.connect(serverName, 80) ) {
client.print("GET /pushingbox?devid=");
client.print(devid);
client.print("&sensorID=");
client.print(sensorID);
client.print("&status=");
client.print(state);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(serverName);
client.println("User-Agent: Arduino");
client.println();
}
else {
Serial.println("Connection failed");
}
// response string
if (client.connected()) {
Serial.println("Connected");
if(client.find("HTTP/1.1") && client.find("200 OK") ){
Serial.println("Success");
result = true;
}
else
Serial.println("Dropping connection - no 200 OK");
}
else {
Serial.println("Disconnected");
}
client.stop();
client.flush();
return result;
}