/* ****************************************************** * WebServer_XbeeComm_POST.ino * ****************************************************** */ #include #include byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; byte ip[] = { 192, 168, 11, 81 }; EthernetServer server(80); 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; EthernetClient 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 // >> ledEffect=K or F or H 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("ledEffect", "\n\r")) { client.read(); // Read '=' inChar = client.read(); // Read 'K','F','H' if (inChar == 'K') sendCommand(" K@"); else if (inChar == 'F') sendCommand(" F@"); else if (inChar == 'H') sendCommand(" H@"); } } // Send back HTTP Response to Client sendHeader(client,"Magicwand LED Effects"); client.print( "

"); client.println("Click Buttons To Show LED Effects on NXT


"); client.print( "

"); client.println("


"); client.print( "

"); client.println("


"); client.print( "

"); client.println("

"); client.println(""); 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 = 5000; 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("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(); client.print(""); client.print(title); client.println(""); }