This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
EtherShield_RESTduino.pde
300 lines (227 loc) · 6.54 KB
/
EtherShield_RESTduino.pde
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
// EtherShield webserver restduino for ENC28J60
//
// Based on RESTduino http://jasongullickson.posterous.com/restduino-arduino-hacking-for-the-rest-of-us
//
// By Andrew Lindsay June 2011
// http://blog.thiseldo.co.uk
// Use first line to include debug output, second line to remove it
#define DEBUG 1
//#undef DEBUG
#include "EtherShield.h"
// please modify the following three lines. mac and ip have to be unique
// in your local area network. You can not have the same numbers in
// two devices:
static uint8_t mymac[6] = { 0x54,0x55,0x58,0x10,0x20,0x35};
static uint8_t myip[4] = { 192,168,1,177};
// listen port for tcp/www (max range 1-254)
#define MYWWWPORT 80
#define BUFFER_SIZE 800
static uint8_t buf[BUFFER_SIZE+1];
#define STR_BUFFER_SIZE 22
static char strbuf[STR_BUFFER_SIZE+1];
EtherShield es=EtherShield();
uint16_t http200ok(void)
{
return(es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n")));
}
uint16_t http404(void)
{
return(es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 404 OK\r\nContent-Type: text/html\r\n\r\n")));
}
// prepare the webpage by writing the data to the tcp send buffer
uint16_t print_webpage(uint8_t *buf)
{
uint16_t plen;
plen=http200ok();
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<html><body>Invalid option selected</body></html>"));
return(plen);
}
#define CMDBUF 50
int16_t process_request(char *str)
{
int8_t r=-1;
int8_t i = 0;
char clientline[CMDBUF];
int index = 0;
int plen = 0;
#ifdef DEBUG
// Serial.println( str );
#endif
char ch = str[index];
while( ch != ' ' && index < CMDBUF) {
clientline[index] = ch;
index++;
ch = str[index];
}
clientline[index] = '\0';
#ifdef DEBUG
Serial.println( clientline );
#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
urlString.toCharArray(clientline, CMDBUF);
// 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();
char jsonOut[50];
if(pin != NULL){
if(value != NULL){
// set the pin value
#ifdef DEBUG
Serial.println("setting pin");
#endif
// select the pin
int selectedPin = pin[0] -'0';
#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){
// digital
#ifdef DEBUG
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 {
// analog
#ifdef DEBUG
Serial.println("analog");
#endif
// get numeric value
int selectedValue = atoi(value);
#ifdef DEBUG
Serial.println(selectedValue);
#endif
analogWrite(selectedPin, selectedValue);
}
// return status
return( http200ok() );
} else {
// read the pin value
#ifdef DEBUG
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
sprintf( jsonOut, "{\"%s\":\"%s\"}", pin, outValue );
#ifdef DEBUG
Serial.println( jsonOut );
#endif
// return value
plen=http200ok();
plen=es.ES_fill_tcp_data(buf,plen,jsonOut);
return(plen);
}
} else {
// error
#ifdef DEBUG
Serial.println("erroring");
#endif
plen = http404();
}
return plen;
}
void setup(){
// Init SPI
es.ES_enc28j60SpiInit();
// initialize enc28j60
es.ES_enc28j60Init(mymac); //, 8);
//init the ethernet/ip layer:
es.ES_init_ip_arp_udp_tcp(mymac,myip,80);
#ifdef DEBUG
Serial.begin(19200);
Serial.println("ENC28J60 RESTduino");
Serial.print( "ENC28J60 version " );
Serial.println( es.ES_enc28j60Revision(), HEX);
if( es.ES_enc28j60Revision() <= 0 )
Serial.println( "Failed to access ENC28J60");
#endif
#ifdef DEBUG
Serial.println("Ready");
#endif
}
void loop(){
uint16_t plen, dat_p;
int8_t cmd;
while(1) {
// read packet, handle ping and wait for a tcp packet:
dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
/* dat_p will be unequal to zero if there is a valid http get */
if(dat_p==0){
// no http request
continue;
}
// tcp port 80 begin
if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){
// head, post and other methods:
dat_p = print_webpage(buf);
goto SENDTCP;
}
// just one web page in the "root directory" of the web server
if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){
#ifdef DEBUG
Serial.println("GET / request");
#endif
dat_p=print_webpage(buf);
goto SENDTCP;
}
dat_p = process_request((char *)&(buf[dat_p+4]));
SENDTCP:
es.ES_www_server_reply(buf,dat_p); // send web page data
// tcp port 80 end
}
}