-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRestuino.ino
377 lines (288 loc) · 9.59 KB
/
Restuino.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
RESTduino
A REST-style interface to the Arduino via the
Wiznet Ethernet shield.
Based on David A. Mellis's "Web Server" ethernet
shield example sketch.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 04/12/2011
by Jason J. Gullickson
added 10/16/2011
by Edward M. Goldberg - Optional Debug flag
*/
#define DEBUG true
#define STATICIP true
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetBonjour.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
#if STATICIP
byte ip[] = {10,0,1,65 };
#endif
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
#if defined(ARDUINO) && ARDUINO >= 100
EthernetServer server(80);
#else
Server server(80);
#endif
void setup()
{
#if DEBUG
// turn on serial (for debuggin)
Serial.begin(9600);
#endif
// start the Ethernet connection and the server:
#if STATICIP
Ethernet.begin(mac, ip);
#else
if (Ethernet.begin(mac) == 0) {
#if DEBUG
Serial.println("Unable to set server IP address using DHCP");
#endif
for(;;)
;
}
#if DEBUG
// report the dhcp IP address:
Serial.println(Ethernet.localIP());
#endif
#endif
server.begin();
EthernetBonjour.begin("restduino");
}
// url buffer size
#define BUFSIZE 255
// Toggle case sensitivity
#define CASESENSE true
void loop()
{
// needed to continue Bonjour/Zeroconf name registration
EthernetBonjour.run();
char clientline[BUFSIZE];
int index = 0;
// listen for incoming clients
#if defined(ARDUINO) && ARDUINO >= 100
EthernetClient client = server.available();
#else
Client client = server.available();
#endif
if (client) {
// reset input buffer
index = 0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// fill url the buffer
if(c != '\n' && c != '\r' && index < BUFSIZE){ // Reads until either an eol character is reached or the buffer is full
clientline[index++] = c;
continue;
}
#if DEBUG
Serial.print("client available bytes before flush: "); Serial.println(client.available());
Serial.print("request = "); Serial.println(clientline);
#endif
// Flush any remaining bytes from the client buffer
client.flush();
#if DEBUG
// Should be 0
Serial.print("client available bytes after flush: "); Serial.println(client.available());
#endif
// convert clientline into a proper
// string for further processing
String urlString = String(clientline);
// extract the operation
String op = urlString.substring(0,urlString.indexOf(' '));
// we're only interested in the first part...
urlString = urlString.substring(urlString.indexOf('/'), urlString.indexOf(' ', urlString.indexOf('/')));
// put what's left of the URL back in client line
#if CASESENSE
urlString.toUpperCase();
#endif
urlString.toCharArray(clientline, BUFSIZE);
// get the first two parameters
char *pin = strtok(clientline,"/");
char *value = strtok(NULL,"/");
// this is where we actually *do something*!
char outValue[10] = "MU";
String jsonOut = String();
if(pin != NULL){
if(value != NULL){
#if DEBUG
// set the pin value
Serial.println("setting pin");
#endif
// select the pin
int selectedPin = atoi (pin);
#if DEBUG
Serial.println(selectedPin);
#endif
// set the pin for output
pinMode(selectedPin, OUTPUT);
// determine digital or analog (PWM)
if(strncmp(value, "HIGH", 4) == 0 || strncmp(value, "LOW", 3) == 0){
#if DEBUG
// digital
Serial.println("digital");
#endif
if(strncmp(value, "HIGH", 4) == 0){
#if DEBUG
Serial.println("HIGH");
#endif
digitalWrite(selectedPin, HIGH);
}
if(strncmp(value, "LOW", 3) == 0){
#if DEBUG
Serial.println("LOW");
#endif
digitalWrite(selectedPin, LOW);
}
}
else if (strncmp(pin, "GARAGE", 6) == 0) {
Serial.println("wooo garage!");
Serial.println(value);
if(strncmp(value, "OPEN", 4) == 0){
// Opening Garage
Serial.println("garage open");
int switchPin = 8;
int switchValue = digitalRead(switchPin);
// Only do something if the garage is closed
if (switchValue == 1) {
// Switch is high so the garage is closed
// Open it!
int selectedPin = 7;
pinMode(selectedPin, OUTPUT);
digitalWrite(selectedPin, HIGH);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("SUCCESS");
delay(15000);
digitalWrite(selectedPin, LOW);
} else {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("GARAGE IS ALREADY OPEN");
}
} else if(strncmp(value, "CLOSE", 4) == 0) {
// Closing Garage
Serial.println("garage close");
int switchPin = 8;
int switchValue = digitalRead(switchPin);
// Only do something if the garage is open
if (switchValue == 0) {
// Switch is low so the garage is open
// Close it!
int selectedPin = 7;
pinMode(selectedPin, OUTPUT);
digitalWrite(selectedPin, HIGH);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("SUCCESS");
delay(15000);
digitalWrite(selectedPin, LOW);
} else {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("GARAGE IS ALREADY OPEN");
}
} else {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("UNKNOWN COMMAND");
}
}
else {
#if DEBUG
// analog
Serial.println("analog");
#endif
// get numeric value
int selectedValue = atoi(value);
#if DEBUG
Serial.println(selectedValue);
#endif
analogWrite(selectedPin, selectedValue);
}
// return status
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("SUCCESS");
}
else {
#if DEBUG
// read the pin value
Serial.println("reading pin");
#endif
// determine analog or digital
if(pin[0] == 'a' || pin[0] == 'A'){
// analog
int selectedPin = pin[1] - '0';
#if DEBUG
Serial.println(selectedPin);
Serial.println("analog");
#endif
sprintf(outValue,"%d",analogRead(selectedPin));
#if DEBUG
Serial.println(outValue);
#endif
}
else if(pin[0] != NULL) {
// digital
int selectedPin = pin[0] - '0';
#if DEBUG
Serial.println(selectedPin);
Serial.println("digital");
#endif
pinMode(selectedPin, INPUT);
int inValue = digitalRead(selectedPin);
if(inValue == 0){
sprintf(outValue,"%s","LOW");
//sprintf(outValue,"%d",digitalRead(selectedPin));
}
if(inValue == 1){
sprintf(outValue,"%s","HIGH");
}
#if DEBUG
Serial.println(outValue);
#endif
}
// assemble the json output
jsonOut += "{\"";
jsonOut += pin;
jsonOut += "\":\"";
jsonOut += outValue;
jsonOut += "\"}";
// return value with wildcarded Cross-origin policy
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Access-Control-Allow-Origin: *");
client.println();
client.println(jsonOut);
}
}
else {
// error
#if DEBUG
Serial.println("erroring");
#endif
client.println("HTTP/1.1 404 Not Found");
client.println("Content-Type: text/html");
client.println();
}
break;
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
//client.stop();
client.stop();
while(client.status() != 0){
delay(5);
}
}
}