#include #include #include #include "AF_XPort.h" #include "AFSoftSerial.h" /* Xport settings: *** Channel 1 Baudrate 57600, I/F Mode 4C, Flow 02 Port 00080 Connect Mode : C4 Send '+++' in Modem Mode enabled Show IP addr after 'RING' enabled Auto increment source port disabled Remote IP Adr: --- none ---, Port 00000 Disconn Mode : 80 Disconn Time: 00:03 Flush Mode : 77 */ // Strings stored in flash of the HTML we will be xmitting const char http_404header[] PROGMEM = "HTTP/1.1 404 Not Found\nServer: arduino\nContent-Type: text/html\n\n404

404: Sorry, that page cannot be found!

"; const char http_header[] PROGMEM = "HTTP/1.0 200 OK\nServer: arduino\nContent-Type: text/html\n\n"; const char html_header[] PROGMEM = "XPort Color picker "; const char javascript[] PROGMEM = ""; const char form[] PROGMEM = "

Arduino Webserver: LED color picker!

Pick LED Color:   
"; // RGB led connections #define bluePin 9 // Blue LED, connected to digital pin 9 #define greenPin 10 // Green LED, connected to digital pin 10 #define redPin 11 // Red LED, connected to digital pin 11 // EEprom locations for the data (for permanent storage) #define RED_EADDR 1 #define GREEN_EADDR 2 #define BLUE_EADDR 3 char linebuffer[128]; // a large buffer to store our data // keep track of how many connections we've got int requestNumber = 0; // the xport! #define XPORT_RX 2 #define XPORT_TX 3 #define XPORT_RESET 4 #define XPORT_CTS 6 #define XPORT_RTS 0 // not used #define XPORT_DTR 0 // not used AF_XPort xport = AF_XPort(XPORT_RX, XPORT_TX, XPORT_RESET, XPORT_DTR, XPORT_RTS, XPORT_CTS); ////////////////////////////////////////////////////// void setup() { pinMode(redPin, OUTPUT); // sets the pins as output pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); analogWrite(redPin, EEPROM.read(RED_EADDR)); // Write current values to LED pins analogWrite(greenPin, EEPROM.read(GREEN_EADDR)); analogWrite(bluePin, EEPROM.read(BLUE_EADDR)); Serial.begin(57600); Serial.println("serial port ready"); xport.begin(57600); xport.reset(); Serial.println("XPort ready"); } void loop() { uint16_t ret; ret = requested(); if (ret == 404) { xport.flush(250); // first the stuff for the web client xport.ROM_print(http_404header); xport.disconnect(); } else if (ret == 200) { changeLED(); respond(); Serial.print("Requested! No. "); Serial.println(requestNumber); delay(4000); pinMode(XPORT_RESET, HIGH); delay(50); pinMode(XPORT_RESET, LOW); requestNumber++; } } uint16_t requested(void) { uint8_t read, x; char *found; //Serial.println("Waiting for connection..."); while (1) { read = xport.readline_timeout(linebuffer, 128, 200); //Serial.println(read, DEC); // debugging output if (read == 0) // nothing read (we timed out) return 0; if (read) // we got something! Serial.println(linebuffer); if (strstr(linebuffer, "GET / ")) { return 200; // a valid request! } if (strstr(linebuffer, "GET /?")) { return 200; // a valid CGI request! } if(strstr(linebuffer, "GET ")) { return 404; // some other file, which we dont have } } } // shortcut procedure just prints out the #xxxxxx values stored in EEPROM void printLEDcolorHex(void) { uint8_t temp; xport.print('#'); temp = 255-EEPROM.read(RED_EADDR); if (temp <= 16) xport.print('0'); // print a leading zero xport.print(temp , HEX); // Write current values to LED pins temp = 255-EEPROM.read(GREEN_EADDR); if (temp <= 16) xport.print('0'); // print a leading zero xport.print(temp , HEX); // Write current values to LED pins temp = 255-EEPROM.read(BLUE_EADDR); if (temp <= 16) xport.print('0'); // print a leading zero xport.print(temp , HEX); // Write current values to LED pins } // read a Hex value and return the decimal equivalent uint8_t parseHex(char c) { if (c < '0') return 0; if (c <= '9') return c - '0'; if (c < 'A') return 0; if (c <= 'F') return (c - 'A')+10; } // check to see if we got a colorchange request void changeLED(void) { char *found=0; uint8_t red, green, blue; // Look for a ? style GET command found = strstr(linebuffer, "?color=%23"); // "?color=#" GET request if (found) { // announce that we received a proper command Serial.println("changing color"); found += 10; // skip forward in the string to the data part // extract the #xxxxxx data red = parseHex(found[0]) * 16 + parseHex(found[1]); found += 2; green = parseHex(found[0]) * 16 + parseHex(found[1]); found += 2; blue = parseHex(found[0]) * 16 + parseHex(found[1]); // Debug output Serial.print("red = "); Serial.print(red, HEX); Serial.print(" green = "); Serial.print(green, HEX); Serial.print(" blue = "); Serial.print(blue, HEX); Serial.println(""); // save the data to the EEPROM for long term storage EEPROM.write(RED_EADDR, 255-red); EEPROM.write(GREEN_EADDR, 255-green); EEPROM.write(BLUE_EADDR, 255-blue); // Set the LED. Since its common-anode, we have to invert the values (255 is off, not on) analogWrite(redPin, 255-red); // Write current values to LED pins analogWrite(greenPin, 255-green); analogWrite(bluePin, 255-blue); } } // Return the HTML place void respond(){ uint8_t temp; xport.flush(50); // first the stuff for the web client xport.ROM_print(http_header); // next the start of the html header xport.ROM_print(html_header); // the CSS code that will change the box to the right color when we first start xport.print(""); // the javascript for the color picker xport.ROM_print(javascript); // the form, first part... xport.ROM_print(form); // insert color name here, which is the default 'value' for the printLEDcolorHex(); // the second half of the form xport.ROM_print(form2); // get rid of any other data left xport.flush(255); // disconnecting doesnt work on the xport direct by default? we will just use the timeout which works fine. xport.disconnect(); }