1 | #undef DEBUG
|
2 |
|
3 | #include "EtherShield.h"
|
4 |
|
5 | // please modify the following three lines. mac and ip have to be unique
|
6 | // in your local area network. You can not have the same numbers in
|
7 | // two devices:
|
8 | static uint8_t mymac[6] = { 0x54,0x55,0x58,0x10,0x20,0x35};
|
9 | static uint8_t myip[4] = { 192,168,1,98};
|
10 |
|
11 | // listen port for tcp/www (max range 1-254)
|
12 | #define MYWWWPORT 80
|
13 |
|
14 | #define BUFFER_SIZE 800
|
15 | static uint8_t buf[BUFFER_SIZE+1];
|
16 | #define STR_BUFFER_SIZE 22
|
17 | static char strbuf[STR_BUFFER_SIZE+1];
|
18 |
|
19 | EtherShield es=EtherShield();
|
20 |
|
21 | uint16_t http200ok(void)
|
22 | {
|
23 | 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")));
|
24 | }
|
25 |
|
26 | uint16_t http404(void)
|
27 | {
|
28 | return(es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 404 OK\r\nContent-Type: text/html\r\n\r\n")));
|
29 | }
|
30 |
|
31 | // prepare the webpage by writing the data to the tcp send buffer
|
32 | uint16_t print_webpage(uint8_t *buf)
|
33 | {
|
34 | uint16_t plen;
|
35 | plen=http200ok();
|
36 | plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<html><body><h1>Error 404</h1><p>Diese Seite existiert nicht</body></html>"));
|
37 |
|
38 | return(plen);
|
39 | }
|
40 |
|
41 |
|
42 | #define CMDBUF 50
|
43 |
|
44 | int16_t process_request(char *str)
|
45 | {
|
46 | int8_t r=-1;
|
47 | int8_t i = 0;
|
48 | char clientline[CMDBUF];
|
49 | int index = 0;
|
50 | int plen = 0;
|
51 |
|
52 | #ifdef DEBUG
|
53 | // Serial.println( str );
|
54 | #endif
|
55 |
|
56 | char ch = str[index];
|
57 |
|
58 | while( ch != ' ' && index < CMDBUF) {
|
59 | clientline[index] = ch;
|
60 | index++;
|
61 | ch = str[index];
|
62 | }
|
63 | clientline[index] = '\0';
|
64 |
|
65 | #ifdef DEBUG
|
66 | Serial.println( clientline );
|
67 | #endif
|
68 |
|
69 | // convert clientline into a proper
|
70 | // string for further processing
|
71 | String urlString = String(clientline);
|
72 |
|
73 | // extract the operation
|
74 | String op = urlString.substring(0,urlString.indexOf(' '));
|
75 |
|
76 | // we're only interested in the first part...
|
77 | urlString = urlString.substring(urlString.indexOf('/'), urlString.indexOf(' ', urlString.indexOf('/')));
|
78 |
|
79 | // put what's left of the URL back in client line
|
80 | urlString.toCharArray(clientline, CMDBUF);
|
81 |
|
82 | // get the first two parameters
|
83 | char *pin = strtok(clientline,"/");
|
84 | char *value = strtok(NULL,"/");
|
85 |
|
86 | // this is where we actually *do something*!
|
87 | char outValue[10] = "MU";
|
88 | //outValue = "MU";
|
89 | // String jsonOut = String();
|
90 | char jsonOut[50];
|
91 |
|
92 | if(pin != NULL){
|
93 | if(value != NULL){
|
94 |
|
95 | // set the pin value
|
96 | #ifdef DEBUG
|
97 | Serial.println("setting pin");
|
98 | #endif
|
99 |
|
100 | // select the pin
|
101 | int selectedPin = pin[0] -'0';
|
102 | #ifdef DEBUG
|
103 | Serial.println(selectedPin);
|
104 | #endif
|
105 |
|
106 | // set the pin for output
|
107 | pinMode(selectedPin, OUTPUT);
|
108 |
|
109 | // determine digital or analog (PWM)
|
110 | if(strncmp(value, "HIGH", 4) == 0 || strncmp(value, "LOW", 3) == 0){
|
111 |
|
112 | // digital
|
113 | #ifdef DEBUG
|
114 | Serial.println("digital");
|
115 | #endif
|
116 |
|
117 | if(strncmp(value, "HIGH", 4) == 0){
|
118 | #ifdef DEBUG
|
119 | Serial.println("HIGH");
|
120 | #endif
|
121 | digitalWrite(selectedPin, HIGH);
|
122 | }
|
123 |
|
124 | if(strncmp(value, "LOW", 3) == 0){
|
125 | #ifdef DEBUG
|
126 | Serial.println("LOW");
|
127 | #endif
|
128 | digitalWrite(selectedPin, LOW);
|
129 | }
|
130 |
|
131 | } else {
|
132 |
|
133 | // analog
|
134 | #ifdef DEBUG
|
135 | Serial.println("analog");
|
136 | #endif
|
137 |
|
138 | // get numeric value
|
139 | int selectedValue = atoi(value);
|
140 | #ifdef DEBUG
|
141 | Serial.println(selectedValue);
|
142 | #endif
|
143 |
|
144 | analogWrite(selectedPin, selectedValue);
|
145 |
|
146 | }
|
147 | // return status
|
148 | return( http200ok() );
|
149 |
|
150 | } else {
|
151 |
|
152 | // read the pin value
|
153 | #ifdef DEBUG
|
154 | Serial.println("reading pin");
|
155 | #endif
|
156 |
|
157 | // determine analog or digital
|
158 | if(pin[0] == 'a' || pin[0] == 'A'){
|
159 |
|
160 | // analog
|
161 | int selectedPin = pin[1] - '0';
|
162 |
|
163 | #ifdef DEBUG
|
164 | Serial.println(selectedPin);
|
165 | Serial.println("analog");
|
166 | #endif
|
167 |
|
168 | sprintf(outValue,"%d",analogRead(selectedPin));
|
169 |
|
170 | #ifdef DEBUG
|
171 | Serial.println(outValue);
|
172 | #endif
|
173 |
|
174 | } else if(pin[0] != NULL) {
|
175 |
|
176 | // digital
|
177 | int selectedPin = pin[0] - '0';
|
178 |
|
179 | #ifdef DEBUG
|
180 | Serial.println(selectedPin);
|
181 | Serial.println("digital");
|
182 | #endif
|
183 |
|
184 | pinMode(selectedPin, INPUT);
|
185 |
|
186 | int inValue = digitalRead(selectedPin);
|
187 |
|
188 | if(inValue == 0){
|
189 | sprintf(outValue,"%s","LOW");
|
190 | //sprintf(outValue,"%d",digitalRead(selectedPin));
|
191 | }
|
192 |
|
193 | if(inValue == 1){
|
194 | sprintf(outValue,"%s","HIGH");
|
195 | }
|
196 |
|
197 | #ifdef DEBUG
|
198 | Serial.println(outValue);
|
199 | #endif
|
200 | }
|
201 |
|
202 | // assemble the json output
|
203 | sprintf( jsonOut, "%s", outValue );
|
204 | #ifdef DEBUG
|
205 | Serial.println( jsonOut );
|
206 | #endif
|
207 | // return value
|
208 | plen=http200ok();
|
209 | plen=es.ES_fill_tcp_data(buf,plen,jsonOut);
|
210 | return(plen);
|
211 | }
|
212 | } else {
|
213 |
|
214 | // error
|
215 | #ifdef DEBUG
|
216 | Serial.println("erroring");
|
217 | #endif
|
218 | plen = http404();
|
219 | }
|
220 |
|
221 | return plen;
|
222 | }
|
223 |
|
224 |
|
225 |
|
226 | void setup(){
|
227 | // Init SPI
|
228 | es.ES_enc28j60SpiInit();
|
229 |
|
230 | // initialize enc28j60
|
231 | es.ES_enc28j60Init(mymac); //, 8);
|
232 |
|
233 | //init the ethernet/ip layer:
|
234 | es.ES_init_ip_arp_udp_tcp(mymac,myip,80);
|
235 |
|
236 | #ifdef DEBUG
|
237 | Serial.begin(19200);
|
238 | Serial.println("ENC28J60 RESTduino");
|
239 |
|
240 | Serial.print( "ENC28J60 version " );
|
241 | Serial.println( es.ES_enc28j60Revision(), HEX);
|
242 | if( es.ES_enc28j60Revision() <= 0 )
|
243 | Serial.println( "Failed to access ENC28J60");
|
244 | #endif
|
245 |
|
246 | #ifdef DEBUG
|
247 | Serial.println("Ready");
|
248 | #endif
|
249 |
|
250 | }
|
251 |
|
252 | void loop(){
|
253 | uint16_t plen, dat_p;
|
254 | int8_t cmd;
|
255 |
|
256 | while(1) {
|
257 | // read packet, handle ping and wait for a tcp packet:
|
258 | dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
|
259 |
|
260 | /* dat_p will be unequal to zero if there is a valid http get */
|
261 | if(dat_p==0){
|
262 | // no http request
|
263 | continue;
|
264 | }
|
265 |
|
266 | // tcp port 80 begin
|
267 | if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){
|
268 | // head, post and other methods:
|
269 | dat_p = print_webpage(buf);
|
270 | goto SENDTCP;
|
271 | }
|
272 |
|
273 | // just one web page in the "root directory" of the web server
|
274 | if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){
|
275 | #ifdef DEBUG
|
276 | Serial.println("GET / request");
|
277 | #endif
|
278 | dat_p=print_webpage(buf);
|
279 | goto SENDTCP;
|
280 | }
|
281 | dat_p = process_request((char *)&(buf[dat_p+4]));
|
282 |
|
283 | SENDTCP:
|
284 |
|
285 | es.ES_www_server_reply(buf,dat_p); // send web page data
|
286 | // tcp port 80 end
|
287 | }
|
288 |
|
289 | }
|