LeeT WiFiLamp code and test
Dependencies: ESP8266_WebServer mbed
Fork of WiFiLamp by
main.cpp
- Committer:
- sschocke
- Date:
- 2015-01-01
- Revision:
- 22:6d7a72fab8ff
- Parent:
- 20:f5a6527bfda6
- Child:
- 23:3563e1699fb9
File content as of revision 22:6d7a72fab8ff:
#include "mbed.h" #include "PololuLedStrip.h" #include "ESP8266_WebServer.h" #include "resource.h" #include <string> //#define DEBUG_WIFI PololuLedStrip ledStrip(D11); #define LED_COUNT 8 rgb_color colors[LED_COUNT]; DigitalOut wifiCHPD(D4); DigitalOut wifiReset(D9); int wifiOn = 0; Serial wifiSerial(D8,D2); Serial pc(USBTX,USBRX); ESP8266_WebServer server(&wifiSerial); char temp[200]; #ifdef DEBUG_WIFI void pcrxint(void) { server.debugBuffers(&pc); server.echoMode = true; } #endif void rxint(void) { server.rxint(); } void setColor( uint8_t r, uint8_t g, uint8_t b ) { for ( int c = 0; c < LED_COUNT; c++ ) { colors[c].red = r; colors[c].green = g; colors[c].blue = b; } ledStrip.write(colors, LED_COUNT); } int main() { pc.baud(115200); #ifdef DEBUG_WIFI pc.attach(&pcrxint); #endif pc.printf("WiFi Lamp - v0.02 ...\r\n"); setColor( 25, 0, 0); wifiCHPD = 0; wifiReset = 0; wifiSerial.baud(115200); wifiSerial.attach(&rxint); #ifdef DEBUG_WIFI server.debugSerial = &pc; #endif wait_ms(1000); pc.printf("Powering WiFi...\r\n"); wifiCHPD = 1; wifiReset = 1; wait_ms(250); pc.printf("Hardware Reset WiFi...\r\n"); wifiOn = 1; pc.printf("Starting Web Server...\r\n"); server.Initialize(); pc.printf("Done\r\n"); setColor( 0, 25, 0); wait_ms(500); setColor( 0, 0, 0); while(true) { ESP8266_WebRequest* request = server.GetRequest(); if( request != NULL ) { pc.printf("HTTP %s %s\r\n", request->Method.c_str(), request->URI.c_str()); for( std::map<std::string,std::string>::iterator it = request->Parameters.begin(); it!=request->Parameters.end(); ++it ) { pc.printf("HTTP Parameter %s = %s\r\n", it->first.c_str(), it->second.c_str()); } std::string httpReply; if( request->URI == "/" ) { httpReply = "<html><head><title>RGB WiFi Lamp</title>\ <link rel='stylesheet' href='wifilamp.css' />\ <script src='wifilamp.js'></script>\ </head>\ <body onLoad='onLoad()'>\ <table>\ <tr>\ <td style='width:250px' valign='top'>\ <img style='margin-right:2px' src='colormap.gif' usemap='#colormap' />\ <map id='colormap' name='colormap'>"; int startx = 63; int countx = 7; for( int y=0; y<=180; y+=15) { int endx = startx + (countx*18); for( int x=startx; x<endx; x+= 18) { sprintf(temp, areaHTML, x,y, x+9,y+4, x+9,y+15, x,y+19, x-9,y+15, x-9,y+4); httpReply += temp; } if( y < 90 ) { startx -= 9; countx++; } else { startx += 9; countx--; } } httpReply += "</map><br/><span onClick=\"changeColor('#000000')\">Turn Off</span></td><td align='right'></td></tr></table></body>"; server.SendReply(request->LinkID, httpReply, mimeHTML); } else if( request->URI == "/wifilamp.js" ) { server.SendReply(request->LinkID, javascript, strlen(javascript), mimeJavaScript); } else if( request->URI == "/wifilamp.css" ) { server.SendReply(request->LinkID, css, strlen(css), mimeCSS); } else if( request->URI == "/colormap.gif" ) { server.SendReply(request->LinkID, (char*)colormap, sizeof(colormap), "image/gif"); } else if( request->URI == "/red" ) { httpReply = "<html><head><title>WiFi Lamp</title></head><body><h1>The WiFi Lamp is now Red - To Turn <a href='/'>Off</a></h1></body></html>"; setColor(100, 0, 0); server.SendReply(request->LinkID, httpReply, mimeHTML); } else if( request->URI == "/green" ) { httpReply = "<html><head><title>WiFi Lamp</title></head><body><h1>The WiFi Lamp is now Green - To Turn <a href='/'>Off</a></h1></body></html>"; setColor(0, 100, 0); server.SendReply(request->LinkID, httpReply, mimeHTML); } else if( request->URI == "/blue" ) { httpReply = "<html><head><title>WiFi Lamp</title></head><body><h1>The WiFi Lamp is now Blue - To Turn <a href='/'>Off</a></h1></body></html>"; setColor(0, 0, 100); server.SendReply(request->LinkID, httpReply, mimeHTML); } else if( request->URI == "/white" ) { httpReply = "<html><head><title>WiFi Lamp</title></head><body><h1>The WiFi Lamp is now White - To Turn <a href='/'>Off</a></h1></body></html>"; setColor(100, 100, 100); server.SendReply(request->LinkID, httpReply, mimeHTML); } else if( request->URI == "/setcolour" || request->URI == "/setcolor" ) { int r=0, g=0, b=0; if(request->Parameters.count("r") > 0) r=atoi(request->Parameters["r"].c_str()); if(request->Parameters.count("g") > 0) g=atoi(request->Parameters["g"].c_str()); if(request->Parameters.count("b") > 0) b=atoi(request->Parameters["b"].c_str()); pc.printf( "Set colour to (%i, %i, %i)\r\n", r,g,b); setColor( r,g,b ); httpReply = "<html><head><title>WiFi Lamp</title></head><body>ok</body></html>"; server.SendReply(request->LinkID, httpReply, mimeHTML); } else { server.Send404Error(request->LinkID); } pc.printf("\r\nHTTP Reply Sent\r\n"); delete request; } } }