/* ****************************************************** * WebServer_NXT_ControlPanel.ino * Reference 'Arduino Cookbook 2nd Edition' CH15 Recipe 15.11 * ****************************************************** */ #include #include #include #define P(name) static const prog_uchar name[] PROGMEM P(up_arrow) = "
" "" "" "
"; P(left_arrow) = "
" "" "" "
"; P(right_arrow) = "
" "" "" "
"; P(down_arrow) = "
" "" "" "
"; P(nxt_icon) = "" ""; P(arduino_icon) = "" ""; /* ========================================================================== P(center_block) = "
" "" "" "
"; P(red_button) = "
" "" "" "
"; P(green_button) = "
" "" "" "
"; P(blue_button) = "
" "" "" "
"; ============================================================================= */ byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; byte ip[] = { 192, 168, 11, 81 }; EthernetServer server(80); EthernetClient client; const int MAX_PAGENAME_LEN = 8; // max characters in page name char buffer[MAX_PAGENAME_LEN+1]; // additional character for terminating null void setup() { Serial.begin(9600); Ethernet.begin(mac, ip); server.begin(); delay(2000); } void loop() { char inChar; client = server.available(); if (client) { while (client.connected()) { if (client.available()) { // Parsing HTTP Request to extract NXT command: // >> POST / HTTP/1.1 // >> body of http header // >> \n\r // >> nxtControl=F or L or R or B memset(buffer,0, sizeof(buffer)); if (client.readBytesUntil('/', buffer,sizeof(buffer))) { if (strcmp(buffer,"POST ") == 0) { client.find("\n\r"); // Skip body of http header if (client.findUntil("nxtControl", "\n\r")) { client.read(); // Read '=' inChar = client.read(); // Read command if (inChar == 'F') sendCommand(" F@"); else if (inChar == 'L') sendCommand(" L@"); else if (inChar == 'R') sendCommand(" R@"); else if (inChar == 'B') sendCommand(" B@"); } } // Send back HTTP Response to Client sendHeader(client,"HTWay Control Panel"); client.print( F(">")); client.print( F("")); client.print( F("")); client.print( F("")); client.print( F("")); client.print( F("
")); client.println(F("

HTWay Control Panel

 ")); printP(up_arrow); client.println(F(" 
")); printP(left_arrow); client.println(F("")); printP(nxt_icon); client.println(F("")); printP(right_arrow); client.println(F("
 ")); printP(down_arrow); client.println(F(" 
")); printP(arduino_icon); client.println(F("")); client.println(F("

Powered By
Arduino Web Server

")); client.println(F("
")); client.println(F("")); client.stop(); } // if (client.readBytesUntil('/', buffer,sizeof(buffer))) } // if (client.available()) } // while (client.connected()) delay(1); client.stop(); } // if (client) } /* ****************************************************** * Sends command then waitting for incoming ACK message * ****************************************************** */ boolean sendCommand(String outString) { char inChar; boolean statusReturn = false; const long waitTimeLimit = 2000; long startTimeMillis; //flashLED(1); Serial.print(outString); startTimeMillis = millis(); while((millis() - startTimeMillis) < waitTimeLimit) { inChar = ' '; if (Serial.available() > 0) { inChar = (char) Serial.read(); if (inChar == '@') { statusReturn = true; break; } } } return statusReturn; } void sendHeader(EthernetClient client, char *title) { // send a standard http response header client.println(F("HTTP/1.1 200 OK")); client.println(F("Content-Type: text/html")); client.println(); client.print(F("")); client.print(title); client.println(F("")); } void printP(const prog_uchar *str) { // copy data out of program memory into local storage, write out in // chunks of 32 bytes to avoid extra short TCP/IP packets // from webduino library Copyright 2009 Ben Combee, Ran Talbott uint8_t buffer[32]; size_t bufferEnd = 0; while (buffer[bufferEnd++] = pgm_read_byte(str++)) { if (bufferEnd == 32) { client.write(buffer, 32); bufferEnd = 0; } } // write out everything left but trailing NUL if (bufferEnd > 1) client.write(buffer, bufferEnd - 1); }