LeeT WiFiLamp code and test
Dependencies: ESP8266_WebServer mbed
Fork of WiFiLamp by
Diff: main.cpp
- Revision:
- 23:3563e1699fb9
- Parent:
- 22:6d7a72fab8ff
- Child:
- 24:849265425708
--- a/main.cpp Thu Jan 01 15:00:33 2015 +0000 +++ b/main.cpp Sun Jan 04 12:15:05 2015 +0000 @@ -14,16 +14,24 @@ DigitalOut wifiReset(D9); int wifiOn = 0; +int opMode = 0; +std::string stationMAC; +std::string stationIP; +std::string ssid; +std::string apMAC; +std::string apIP; Serial wifiSerial(D8,D2); Serial pc(USBTX,USBRX); ESP8266_WebServer server(&wifiSerial); char temp[200]; +const char* VERSION = "0.5-alpha"; #ifdef DEBUG_WIFI void pcrxint(void) { - server.debugBuffers(&pc); + //server.debugBuffers(&pc); + pc.putc('@'); server.echoMode = true; } #endif @@ -44,13 +52,18 @@ ledStrip.write(colors, LED_COUNT); } +void sendConfigJSONReply(int linkID, const char* result) { + sprintf(temp, jsonConfigReply, opMode, stationIP.c_str(), ssid.c_str(), result); + server.SendReply(linkID, temp, mimeJSON, 0); +} + int main() { pc.baud(115200); #ifdef DEBUG_WIFI pc.attach(&pcrxint); #endif - pc.printf("WiFi Lamp - v0.02 ...\r\n"); + pc.printf("WiFi Lamp - %s...\r\n", VERSION); setColor( 25, 0, 0); @@ -70,14 +83,40 @@ pc.printf("Hardware Reset WiFi...\r\n"); wifiOn = 1; + std::string fwVer = server.GetFirmwareVersion(); + pc.printf("ESP Firmware Version: %s\r\n", fwVer.c_str()); + pc.printf("Starting Web Server...\r\n"); server.Initialize(); - pc.printf("Done\r\n"); + pc.printf("Done\r\n"); + + opMode = server.GetOperatingMode(); + pc.printf("Operating Mode: %s(%d)\r\n", opModes[opMode], opMode); + if( (opMode & OPMODE_STATION) ) { + pc.printf("Waiting 5 secs for join to WiFi Network...\r\n"); + wait_ms(5000); + stationMAC = server.GetStationMAC(); + stationIP = server.GetStationIP(); + ssid = server.GetStationSSID(); + pc.printf("Station MAC: %s, Station SSID: %s, Station IP: %s\r\n", stationMAC.c_str(), ssid.c_str(), stationIP.c_str()); + } + if( (opMode & OPMODE_SOFTAP) ) { + apMAC = server.GetAPMAC(); + apIP = server.GetAPIP(); + pc.printf("SoftAP MAC: %s, SoftAP IP: %s\r\n", apMAC.c_str(), apIP.c_str()); + } + + //std::list<std::string> apList = server.ListAvailableSSID(); + + //for( std::list<std::string>::iterator it = apList.begin(); it!=apList.end(); ++it ) { + // pc.printf("Found SSID %s\r\n", (*it).c_str()); + //} setColor( 0, 25, 0); wait_ms(500); setColor( 0, 0, 0); + std::string httpReply; while(true) { ESP8266_WebRequest* request = server.GetRequest(); if( request != NULL ) { @@ -85,58 +124,90 @@ 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; + httpReply = htmlHead; + httpReply += "<table><tr><td align='right'><a href='config'><img src='config.gif' /></a></td></tr>"; + httpReply += "<tr><td align='center'>"; + httpReply += "<img style='margin-right:2px' src='colormap.gif' usemap='#colormap' />"; + httpReply += "<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></td></tr><tr><td><span onClick=\"changeColor('#000000')\">Turn Off</span></td></tr></table>"; + httpReply += htmlTail; + server.SendReply(request->LinkID, httpReply, mimeHTML); + } else if( request->URI == "/config" ) { + if( (opMode & OPMODE_STATION) ) { + stationIP = server.GetStationIP(); + ssid = server.GetStationSSID(); + } + httpReply = htmlHead; + httpReply += htmlConfigHTML; + httpReply += htmlTail; + httpReply = httpReply.replace(httpReply.find("%opmode1%"), 9, (opMode==1? " selected" : "")); + httpReply = httpReply.replace(httpReply.find("%opmode2%"), 9, (opMode==2? " selected" : "")); + httpReply = httpReply.replace(httpReply.find("%opmode3%"), 9, (opMode==3? " selected" : "")); + httpReply = httpReply.replace(httpReply.find("%stamac%"), 8, stationMAC); + httpReply = httpReply.replace(httpReply.find("%staip%"), 7, stationIP); + httpReply = httpReply.replace(httpReply.find("%apmac%"), 7, apMAC); + httpReply = httpReply.replace(httpReply.find("%apip%"), 6, apIP); + httpReply = httpReply.replace(httpReply.find("%ssid%"), 6, ssid); + server.SendReply(request->LinkID, httpReply, mimeHTML, 0); + } else if( request->URI == "/updateconfig" ) { + bool result = true; + int newOpMode = atoi(request->Parameters["opmode"].c_str()); + std::string newSSID = request->Parameters["ssid"]; + std::string newPassword = request->Parameters["pwd"]; + if( newOpMode != opMode ) { + result = server.SetOperatingMode(newOpMode); + if( result == false ) { + sendConfigJSONReply(request->LinkID, "Error setting Operating Mode"); + delete request; + continue; } - if( y < 90 ) { - startx -= 9; - countx++; - } else { - startx += 9; - countx--; + opMode = newOpMode; + } + if( (opMode & OPMODE_STATION) ) { + if( (ssid.compare(newSSID) != 0) || (newPassword.empty() == false) ) { + result = server.SetStationSSID(newSSID, newPassword); + if( result == false ) { + sendConfigJSONReply(request->LinkID, "Error connecting to SSID"); + delete request; + continue; + } + // Wait for SSID to connect + for( int retries=0; retries < 6; retries++) { + wait_ms(5000); + ssid = server.GetStationSSID(); + if( ssid.compare(newSSID) == 0 ) { + break; + } + } + stationIP = server.GetStationIP(); } } - httpReply += "</map><br/><span onClick=\"changeColor('#000000')\">Turn Off</span></td><td align='right'></td></tr></table></body>"; - server.SendReply(request->LinkID, httpReply, mimeHTML); + sendConfigJSONReply(request->LinkID, "Success"); } 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); + server.SendReply(request->LinkID, (char*)colormap, sizeof(colormap), mimeGIF); + } else if( request->URI == "/config.gif" ) { + server.SendReply(request->LinkID, (char*)configIcon, sizeof(configIcon), mimeGIF); } else if( request->URI == "/setcolour" || request->URI == "/setcolor" ) { int r=0, g=0, b=0; @@ -148,8 +219,7 @@ setColor( r,g,b ); - httpReply = "<html><head><title>WiFi Lamp</title></head><body>ok</body></html>"; - server.SendReply(request->LinkID, httpReply, mimeHTML); + server.SendReply(request->LinkID, "OK", mimeText, 0); } else { server.Send404Error(request->LinkID); }