-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathRESTduino.pde
More file actions
220 lines (159 loc) · 5.55 KB
/
RESTduino.pde
File metadata and controls
220 lines (159 loc) · 5.55 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
/*
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
*/
#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()
{
// turn on serial (for debuggin)
Serial.begin(9600);
// 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){
// set the pin value
Serial.println("setting pin");
// select the pin
int selectedPin = pin[0] -'0';
Serial.println(selectedPin);
// set the pin for output
pinMode(selectedPin, OUTPUT);
// determine digital or analog (PWM)
if(strncmp(value, "HIGH", 4) == 0 || strncmp(value, "LOW", 3) == 0){
// digital
Serial.println("digital");
if(strncmp(value, "HIGH", 4) == 0){
Serial.println("HIGH");
digitalWrite(selectedPin, HIGH);
}
if(strncmp(value, "LOW", 3) == 0){
Serial.println("LOW");
digitalWrite(selectedPin, LOW);
}
} else {
// analog
Serial.println("analog");
// get numeric value
int selectedValue = atoi(value);
Serial.println(selectedValue);
analogWrite(selectedPin, selectedValue);
}
// return status
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
}
else {
// read the pin value
Serial.println("reading pin");
// determine analog or digital
if(pin[0] == 'a' || pin[0] == 'A'){
// analog
int selectedPin = pin[1] - '0';
Serial.println(selectedPin);
Serial.println("analog");
sprintf(outValue,"%d",analogRead(selectedPin));
Serial.println(outValue);
}
else if(pin[0] != NULL) {
// digital
int selectedPin = pin[0] - '0';
Serial.println(selectedPin);
Serial.println("digital");
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");
}
Serial.println(outValue);
}
// assemble the json output
jsonOut += "{\"";
jsonOut += pin;
jsonOut += "\":\"";
jsonOut += outValue;
jsonOut += "\"}";
// return value
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println(jsonOut);
}
}
else {
// error
Serial.println("erroring");
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();
}
}