Repository navigation
Expand file tree
/
Copy pathRESTduino.pde
More file actions
249 lines (191 loc) · 5.96 KB
/
Copy pathRESTduino.pde
File metadata and controls
249 lines (191 loc) · 5.96 KB
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
/*
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 1
#include <SPI.h>
#include <Ethernet.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};
byte ip[] = {192,168,1,177};
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);
void setup()
{
#ifdef DEBUG
// turn on serial (for debuggin)
Serial.begin(9600);
#endif
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
}
// url buffer size
#define BUFSIZE 255
void loop()
{
char clientline[BUFSIZE];
int index = 0;
// listen for incoming clients
Client client = server.available();
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'){
clientline[index] = c;
index++;
// if we run out of buffer, overwrite the end
if(index >= BUFSIZE)
index = BUFSIZE -1;
continue;
}
// 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
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";
//outValue = "MU";
String jsonOut = String();
if(pin != NULL){
if(value != NULL){
#ifdef DEBUG
// set the pin value
Serial.println("setting pin");
#endif
// select the pin
int selectedPin = atoi (pin);
#ifdef 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){
#ifdef DEBUG
// digital
Serial.println("digital");
#endif
if(strncmp(value, "HIGH", 4) == 0){
#ifdef DEBUG
Serial.println("HIGH");
#endif
digitalWrite(selectedPin, HIGH);
}
if(strncmp(value, "LOW", 3) == 0){
#ifdef DEBUG
Serial.println("LOW");
#endif
digitalWrite(selectedPin, LOW);
}
} else {
#ifdef DEBUG
// analog
Serial.println("analog");
#endif
// get numeric value
int selectedValue = atoi(value);
#ifdef 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();
}
else {
#ifdef 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';
#ifdef DEBUG
Serial.println(selectedPin);
Serial.println("analog");
#endif
sprintf(outValue,"%d",analogRead(selectedPin));
#ifdef DEBUG
Serial.println(outValue);
#endif
}
else if(pin[0] != NULL) {
// digital
int selectedPin = pin[0] - '0';
#ifdef 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");
}
#ifdef 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
#ifdef 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();
}
}