2015年1月11日 星期日

Arduino_Ethercard_4 Relayboard_web control

#include static byte mymac[] = { 0x5A,0x5A,0x5A,0x5A,0x5A,0x5A }; static byte myip[] = { 192,168,1,222 }; byte Ethernet::buffer[900]; BufferFiller bfill; int LedPins[] = { 2,3,4,5}; boolean PinStatus[] = { 1,2,3,4}; const char http_OK[] PROGMEM = "HTTP/1.0 200 OK\r\n" "Content-Type: text/html\r\n" "Pragma: no-cache\r\n\r\n"; const char http_Found[] PROGMEM = "HTTP/1.0 302 Found\r\n" "Location: /\r\n\r\n"; const char http_Unauthorized[] PROGMEM = "HTTP/1.0 401 Unauthorized\r\n" "Content-Type: text/html\r\n\r\n" "

401 Unauthorized

"; void homePage() { bfill.emit_p(PSTR("$F" "ArduinoPIN Webserver" "Relay 1: $F
" "Relay 2: $F
" "Relay 3: $F
" "Relay 4: $F"), http_OK, PinStatus[1]?PSTR("off"):PSTR("on"), PinStatus[1]?PSTR("ON"):PSTR("OFF"), PinStatus[2]?PSTR("off"):PSTR("on"), PinStatus[2]?PSTR("ON"):PSTR("OFF"), PinStatus[3]?PSTR("off"):PSTR("on"), PinStatus[3]?PSTR("ON"):PSTR("OFF"), PinStatus[4]?PSTR("off"):PSTR("on"), PinStatus[4]?PSTR("ON"):PSTR("OFF")); } void setup() { Serial.begin(9600); if (ether.begin(sizeof Ethernet::buffer, mymac,10) == 0); if (!ether.dhcpSetup()); ether.printIp("My Router IP: ", ether.myip); ether.printIp("My SET IP: ", ether.myip); for(int i = 0; i <= 4; i++) { pinMode(LedPins[i],OUTPUT); digitalWrite (LedPins[i],HIGH); PinStatus[i]=false; } } void loop() { delay(1); word len = ether.packetReceive(); // check for ethernet packet / word pos = ether.packetLoop(len); // check for tcp packet / if (pos) { bfill = ether.tcpOffset(); char *data = (char *) Ethernet::buffer + pos; if (strncmp("GET /", data, 5) != 0) { bfill.emit_p(http_Unauthorized); } else { data += 5; if (data[0] == ' ') { homePage(); // Return home page for (int i = 0; i <= 3; i++)digitalWrite(LedPins[i],!PinStatus[i+1]); } else if (strncmp("?ArduinoPIN1=on ", data, 16) == 0) { PinStatus[1] = true; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN2=on ", data, 16) == 0) { PinStatus[2] = true; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN3=on ", data, 16) == 0) { PinStatus[3] = true; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN4=on ", data, 16) == 0) { PinStatus[4] = true; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN1=off ", data, 17) == 0) { PinStatus[1] = false; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN2=off ", data, 17) == 0) { PinStatus[2] = false; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN3=off ", data, 17) == 0) { PinStatus[3] = false; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN4=off ", data, 17) == 0) { PinStatus[4] = false; bfill.emit_p(http_Found); } else { // Page not found bfill.emit_p(http_Unauthorized); } } ether.httpServerReply(bfill.position()); // send http response } }

沒有留言:

張貼留言