LeeT WiFiLamp code and test
Dependencies: ESP8266_WebServer mbed
Fork of WiFiLamp by
main.cpp@24:849265425708, 2015-01-05 (annotated)
- Committer:
- sschocke
- Date:
- Mon Jan 05 19:23:20 2015 +0000
- Revision:
- 24:849265425708
- Parent:
- 23:3563e1699fb9
- Child:
- 25:841fc9daffa5
Fixed bug with ESP module startup sequence
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sschocke | 0:d21e3e1c0a4b | 1 | #include "mbed.h" |
sschocke | 1:f07afcffeb5a | 2 | #include "PololuLedStrip.h" |
sschocke | 7:f15c81074400 | 3 | #include "ESP8266_WebServer.h" |
sschocke | 22:6d7a72fab8ff | 4 | #include "resource.h" |
sschocke | 0:d21e3e1c0a4b | 5 | #include <string> |
sschocke | 13:1e8f27da036a | 6 | //#define DEBUG_WIFI |
sschocke | 0:d21e3e1c0a4b | 7 | |
sschocke | 8:f819de1946a7 | 8 | PololuLedStrip ledStrip(D11); |
sschocke | 1:f07afcffeb5a | 9 | |
sschocke | 8:f819de1946a7 | 10 | #define LED_COUNT 8 |
sschocke | 1:f07afcffeb5a | 11 | rgb_color colors[LED_COUNT]; |
sschocke | 1:f07afcffeb5a | 12 | |
sschocke | 0:d21e3e1c0a4b | 13 | DigitalOut wifiCHPD(D4); |
sschocke | 0:d21e3e1c0a4b | 14 | DigitalOut wifiReset(D9); |
sschocke | 0:d21e3e1c0a4b | 15 | |
sschocke | 0:d21e3e1c0a4b | 16 | int wifiOn = 0; |
sschocke | 23:3563e1699fb9 | 17 | int opMode = 0; |
sschocke | 23:3563e1699fb9 | 18 | std::string stationMAC; |
sschocke | 23:3563e1699fb9 | 19 | std::string stationIP; |
sschocke | 23:3563e1699fb9 | 20 | std::string ssid; |
sschocke | 23:3563e1699fb9 | 21 | std::string apMAC; |
sschocke | 23:3563e1699fb9 | 22 | std::string apIP; |
sschocke | 0:d21e3e1c0a4b | 23 | |
sschocke | 0:d21e3e1c0a4b | 24 | Serial wifiSerial(D8,D2); |
sschocke | 0:d21e3e1c0a4b | 25 | Serial pc(USBTX,USBRX); |
sschocke | 7:f15c81074400 | 26 | ESP8266_WebServer server(&wifiSerial); |
sschocke | 0:d21e3e1c0a4b | 27 | |
sschocke | 22:6d7a72fab8ff | 28 | char temp[200]; |
sschocke | 23:3563e1699fb9 | 29 | const char* VERSION = "0.5-alpha"; |
sschocke | 20:f5a6527bfda6 | 30 | |
sschocke | 20:f5a6527bfda6 | 31 | #ifdef DEBUG_WIFI |
sschocke | 20:f5a6527bfda6 | 32 | void pcrxint(void) { |
sschocke | 23:3563e1699fb9 | 33 | //server.debugBuffers(&pc); |
sschocke | 23:3563e1699fb9 | 34 | pc.putc('@'); |
sschocke | 20:f5a6527bfda6 | 35 | server.echoMode = true; |
sschocke | 20:f5a6527bfda6 | 36 | } |
sschocke | 20:f5a6527bfda6 | 37 | #endif |
sschocke | 20:f5a6527bfda6 | 38 | |
sschocke | 7:f15c81074400 | 39 | void rxint(void) { |
sschocke | 7:f15c81074400 | 40 | server.rxint(); |
sschocke | 0:d21e3e1c0a4b | 41 | } |
sschocke | 0:d21e3e1c0a4b | 42 | |
tomvdb | 4:4a502f72cbe3 | 43 | void setColor( uint8_t r, uint8_t g, uint8_t b ) |
tomvdb | 4:4a502f72cbe3 | 44 | { |
tomvdb | 4:4a502f72cbe3 | 45 | for ( int c = 0; c < LED_COUNT; c++ ) |
tomvdb | 4:4a502f72cbe3 | 46 | { |
tomvdb | 4:4a502f72cbe3 | 47 | colors[c].red = r; |
tomvdb | 4:4a502f72cbe3 | 48 | colors[c].green = g; |
tomvdb | 4:4a502f72cbe3 | 49 | colors[c].blue = b; |
tomvdb | 4:4a502f72cbe3 | 50 | } |
tomvdb | 4:4a502f72cbe3 | 51 | |
tomvdb | 4:4a502f72cbe3 | 52 | ledStrip.write(colors, LED_COUNT); |
tomvdb | 4:4a502f72cbe3 | 53 | } |
tomvdb | 4:4a502f72cbe3 | 54 | |
sschocke | 23:3563e1699fb9 | 55 | void sendConfigJSONReply(int linkID, const char* result) { |
sschocke | 23:3563e1699fb9 | 56 | sprintf(temp, jsonConfigReply, opMode, stationIP.c_str(), ssid.c_str(), result); |
sschocke | 23:3563e1699fb9 | 57 | server.SendReply(linkID, temp, mimeJSON, 0); |
sschocke | 23:3563e1699fb9 | 58 | } |
sschocke | 23:3563e1699fb9 | 59 | |
sschocke | 0:d21e3e1c0a4b | 60 | int main() { |
sschocke | 13:1e8f27da036a | 61 | pc.baud(115200); |
sschocke | 20:f5a6527bfda6 | 62 | #ifdef DEBUG_WIFI |
sschocke | 20:f5a6527bfda6 | 63 | pc.attach(&pcrxint); |
sschocke | 20:f5a6527bfda6 | 64 | #endif |
leet | 10:f48a9f923271 | 65 | |
sschocke | 23:3563e1699fb9 | 66 | pc.printf("WiFi Lamp - %s...\r\n", VERSION); |
leet | 10:f48a9f923271 | 67 | |
sschocke | 13:1e8f27da036a | 68 | setColor( 25, 0, 0); |
tomvdb | 4:4a502f72cbe3 | 69 | |
sschocke | 0:d21e3e1c0a4b | 70 | wifiCHPD = 0; |
sschocke | 0:d21e3e1c0a4b | 71 | wifiReset = 0; |
sschocke | 13:1e8f27da036a | 72 | wifiSerial.baud(115200); |
sschocke | 0:d21e3e1c0a4b | 73 | wifiSerial.attach(&rxint); |
sschocke | 7:f15c81074400 | 74 | #ifdef DEBUG_WIFI |
sschocke | 7:f15c81074400 | 75 | server.debugSerial = &pc; |
sschocke | 7:f15c81074400 | 76 | #endif |
sschocke | 0:d21e3e1c0a4b | 77 | wait_ms(1000); |
sschocke | 0:d21e3e1c0a4b | 78 | |
sschocke | 0:d21e3e1c0a4b | 79 | pc.printf("Powering WiFi...\r\n"); |
sschocke | 0:d21e3e1c0a4b | 80 | wifiCHPD = 1; |
sschocke | 13:1e8f27da036a | 81 | wifiReset = 1; |
sschocke | 0:d21e3e1c0a4b | 82 | wait_ms(250); |
sschocke | 0:d21e3e1c0a4b | 83 | pc.printf("Hardware Reset WiFi...\r\n"); |
sschocke | 24:849265425708 | 84 | server.ResetModule(); |
sschocke | 0:d21e3e1c0a4b | 85 | wifiOn = 1; |
sschocke | 0:d21e3e1c0a4b | 86 | |
sschocke | 24:849265425708 | 87 | |
sschocke | 23:3563e1699fb9 | 88 | std::string fwVer = server.GetFirmwareVersion(); |
sschocke | 23:3563e1699fb9 | 89 | pc.printf("ESP Firmware Version: %s\r\n", fwVer.c_str()); |
sschocke | 23:3563e1699fb9 | 90 | |
sschocke | 7:f15c81074400 | 91 | pc.printf("Starting Web Server...\r\n"); |
sschocke | 7:f15c81074400 | 92 | server.Initialize(); |
sschocke | 23:3563e1699fb9 | 93 | pc.printf("Done\r\n"); |
sschocke | 23:3563e1699fb9 | 94 | |
sschocke | 23:3563e1699fb9 | 95 | opMode = server.GetOperatingMode(); |
sschocke | 23:3563e1699fb9 | 96 | pc.printf("Operating Mode: %s(%d)\r\n", opModes[opMode], opMode); |
sschocke | 23:3563e1699fb9 | 97 | if( (opMode & OPMODE_STATION) ) { |
sschocke | 23:3563e1699fb9 | 98 | pc.printf("Waiting 5 secs for join to WiFi Network...\r\n"); |
sschocke | 23:3563e1699fb9 | 99 | wait_ms(5000); |
sschocke | 23:3563e1699fb9 | 100 | stationMAC = server.GetStationMAC(); |
sschocke | 23:3563e1699fb9 | 101 | stationIP = server.GetStationIP(); |
sschocke | 23:3563e1699fb9 | 102 | ssid = server.GetStationSSID(); |
sschocke | 23:3563e1699fb9 | 103 | pc.printf("Station MAC: %s, Station SSID: %s, Station IP: %s\r\n", stationMAC.c_str(), ssid.c_str(), stationIP.c_str()); |
sschocke | 23:3563e1699fb9 | 104 | } |
sschocke | 23:3563e1699fb9 | 105 | if( (opMode & OPMODE_SOFTAP) ) { |
sschocke | 23:3563e1699fb9 | 106 | apMAC = server.GetAPMAC(); |
sschocke | 23:3563e1699fb9 | 107 | apIP = server.GetAPIP(); |
sschocke | 23:3563e1699fb9 | 108 | pc.printf("SoftAP MAC: %s, SoftAP IP: %s\r\n", apMAC.c_str(), apIP.c_str()); |
sschocke | 23:3563e1699fb9 | 109 | } |
sschocke | 23:3563e1699fb9 | 110 | |
sschocke | 23:3563e1699fb9 | 111 | //std::list<std::string> apList = server.ListAvailableSSID(); |
sschocke | 23:3563e1699fb9 | 112 | |
sschocke | 23:3563e1699fb9 | 113 | //for( std::list<std::string>::iterator it = apList.begin(); it!=apList.end(); ++it ) { |
sschocke | 23:3563e1699fb9 | 114 | // pc.printf("Found SSID %s\r\n", (*it).c_str()); |
sschocke | 23:3563e1699fb9 | 115 | //} |
sschocke | 0:d21e3e1c0a4b | 116 | |
sschocke | 13:1e8f27da036a | 117 | setColor( 0, 25, 0); |
tomvdb | 4:4a502f72cbe3 | 118 | wait_ms(500); |
tomvdb | 4:4a502f72cbe3 | 119 | setColor( 0, 0, 0); |
tomvdb | 4:4a502f72cbe3 | 120 | |
sschocke | 23:3563e1699fb9 | 121 | std::string httpReply; |
sschocke | 0:d21e3e1c0a4b | 122 | while(true) { |
sschocke | 9:319aeb6e0123 | 123 | ESP8266_WebRequest* request = server.GetRequest(); |
sschocke | 9:319aeb6e0123 | 124 | if( request != NULL ) { |
sschocke | 9:319aeb6e0123 | 125 | pc.printf("HTTP %s %s\r\n", request->Method.c_str(), request->URI.c_str()); |
sschocke | 9:319aeb6e0123 | 126 | for( std::map<std::string,std::string>::iterator it = request->Parameters.begin(); it!=request->Parameters.end(); ++it ) { |
sschocke | 9:319aeb6e0123 | 127 | pc.printf("HTTP Parameter %s = %s\r\n", it->first.c_str(), it->second.c_str()); |
sschocke | 9:319aeb6e0123 | 128 | } |
sschocke | 9:319aeb6e0123 | 129 | if( request->URI == "/" ) { |
sschocke | 23:3563e1699fb9 | 130 | httpReply = htmlHead; |
sschocke | 23:3563e1699fb9 | 131 | httpReply += "<table><tr><td align='right'><a href='config'><img src='config.gif' /></a></td></tr>"; |
sschocke | 23:3563e1699fb9 | 132 | httpReply += "<tr><td align='center'>"; |
sschocke | 23:3563e1699fb9 | 133 | httpReply += "<img style='margin-right:2px' src='colormap.gif' usemap='#colormap' />"; |
sschocke | 23:3563e1699fb9 | 134 | httpReply += "<map id='colormap' name='colormap'>"; |
sschocke | 23:3563e1699fb9 | 135 | //int startx = 63; |
sschocke | 23:3563e1699fb9 | 136 | //int countx = 7; |
sschocke | 23:3563e1699fb9 | 137 | //for( int y=0; y<=180; y+=15) { |
sschocke | 23:3563e1699fb9 | 138 | // int endx = startx + (countx*18); |
sschocke | 23:3563e1699fb9 | 139 | // for( int x=startx; x<endx; x+= 18) { |
sschocke | 23:3563e1699fb9 | 140 | // sprintf(temp, areaHTML, x,y, x+9,y+4, x+9,y+15, x,y+19, x-9,y+15, x-9,y+4); |
sschocke | 23:3563e1699fb9 | 141 | // httpReply += temp; |
sschocke | 23:3563e1699fb9 | 142 | // } |
sschocke | 23:3563e1699fb9 | 143 | // if( y < 90 ) { |
sschocke | 23:3563e1699fb9 | 144 | // startx -= 9; |
sschocke | 23:3563e1699fb9 | 145 | // countx++; |
sschocke | 23:3563e1699fb9 | 146 | // } else { |
sschocke | 23:3563e1699fb9 | 147 | // startx += 9; |
sschocke | 23:3563e1699fb9 | 148 | // countx--; |
sschocke | 23:3563e1699fb9 | 149 | // } |
sschocke | 23:3563e1699fb9 | 150 | //} |
sschocke | 23:3563e1699fb9 | 151 | httpReply += "</map></td></tr><tr><td><span onClick=\"changeColor('#000000')\">Turn Off</span></td></tr></table>"; |
sschocke | 23:3563e1699fb9 | 152 | httpReply += htmlTail; |
sschocke | 23:3563e1699fb9 | 153 | server.SendReply(request->LinkID, httpReply, mimeHTML); |
sschocke | 23:3563e1699fb9 | 154 | } else if( request->URI == "/config" ) { |
sschocke | 23:3563e1699fb9 | 155 | if( (opMode & OPMODE_STATION) ) { |
sschocke | 23:3563e1699fb9 | 156 | stationIP = server.GetStationIP(); |
sschocke | 23:3563e1699fb9 | 157 | ssid = server.GetStationSSID(); |
sschocke | 23:3563e1699fb9 | 158 | } |
sschocke | 23:3563e1699fb9 | 159 | httpReply = htmlHead; |
sschocke | 23:3563e1699fb9 | 160 | httpReply += htmlConfigHTML; |
sschocke | 23:3563e1699fb9 | 161 | httpReply += htmlTail; |
sschocke | 23:3563e1699fb9 | 162 | httpReply = httpReply.replace(httpReply.find("%opmode1%"), 9, (opMode==1? " selected" : "")); |
sschocke | 23:3563e1699fb9 | 163 | httpReply = httpReply.replace(httpReply.find("%opmode2%"), 9, (opMode==2? " selected" : "")); |
sschocke | 23:3563e1699fb9 | 164 | httpReply = httpReply.replace(httpReply.find("%opmode3%"), 9, (opMode==3? " selected" : "")); |
sschocke | 23:3563e1699fb9 | 165 | httpReply = httpReply.replace(httpReply.find("%stamac%"), 8, stationMAC); |
sschocke | 23:3563e1699fb9 | 166 | httpReply = httpReply.replace(httpReply.find("%staip%"), 7, stationIP); |
sschocke | 23:3563e1699fb9 | 167 | httpReply = httpReply.replace(httpReply.find("%apmac%"), 7, apMAC); |
sschocke | 23:3563e1699fb9 | 168 | httpReply = httpReply.replace(httpReply.find("%apip%"), 6, apIP); |
sschocke | 23:3563e1699fb9 | 169 | httpReply = httpReply.replace(httpReply.find("%ssid%"), 6, ssid); |
sschocke | 23:3563e1699fb9 | 170 | server.SendReply(request->LinkID, httpReply, mimeHTML, 0); |
sschocke | 23:3563e1699fb9 | 171 | } else if( request->URI == "/updateconfig" ) { |
sschocke | 23:3563e1699fb9 | 172 | bool result = true; |
sschocke | 23:3563e1699fb9 | 173 | int newOpMode = atoi(request->Parameters["opmode"].c_str()); |
sschocke | 23:3563e1699fb9 | 174 | std::string newSSID = request->Parameters["ssid"]; |
sschocke | 23:3563e1699fb9 | 175 | std::string newPassword = request->Parameters["pwd"]; |
sschocke | 23:3563e1699fb9 | 176 | if( newOpMode != opMode ) { |
sschocke | 23:3563e1699fb9 | 177 | result = server.SetOperatingMode(newOpMode); |
sschocke | 23:3563e1699fb9 | 178 | if( result == false ) { |
sschocke | 23:3563e1699fb9 | 179 | sendConfigJSONReply(request->LinkID, "Error setting Operating Mode"); |
sschocke | 23:3563e1699fb9 | 180 | delete request; |
sschocke | 23:3563e1699fb9 | 181 | continue; |
sschocke | 22:6d7a72fab8ff | 182 | } |
sschocke | 23:3563e1699fb9 | 183 | opMode = newOpMode; |
sschocke | 23:3563e1699fb9 | 184 | } |
sschocke | 23:3563e1699fb9 | 185 | if( (opMode & OPMODE_STATION) ) { |
sschocke | 23:3563e1699fb9 | 186 | if( (ssid.compare(newSSID) != 0) || (newPassword.empty() == false) ) { |
sschocke | 23:3563e1699fb9 | 187 | result = server.SetStationSSID(newSSID, newPassword); |
sschocke | 23:3563e1699fb9 | 188 | if( result == false ) { |
sschocke | 23:3563e1699fb9 | 189 | sendConfigJSONReply(request->LinkID, "Error connecting to SSID"); |
sschocke | 23:3563e1699fb9 | 190 | delete request; |
sschocke | 23:3563e1699fb9 | 191 | continue; |
sschocke | 23:3563e1699fb9 | 192 | } |
sschocke | 23:3563e1699fb9 | 193 | // Wait for SSID to connect |
sschocke | 23:3563e1699fb9 | 194 | for( int retries=0; retries < 6; retries++) { |
sschocke | 23:3563e1699fb9 | 195 | wait_ms(5000); |
sschocke | 23:3563e1699fb9 | 196 | ssid = server.GetStationSSID(); |
sschocke | 23:3563e1699fb9 | 197 | if( ssid.compare(newSSID) == 0 ) { |
sschocke | 23:3563e1699fb9 | 198 | break; |
sschocke | 23:3563e1699fb9 | 199 | } |
sschocke | 23:3563e1699fb9 | 200 | } |
sschocke | 23:3563e1699fb9 | 201 | stationIP = server.GetStationIP(); |
sschocke | 22:6d7a72fab8ff | 202 | } |
sschocke | 22:6d7a72fab8ff | 203 | } |
sschocke | 23:3563e1699fb9 | 204 | sendConfigJSONReply(request->LinkID, "Success"); |
sschocke | 20:f5a6527bfda6 | 205 | } else if( request->URI == "/wifilamp.js" ) { |
sschocke | 20:f5a6527bfda6 | 206 | server.SendReply(request->LinkID, javascript, strlen(javascript), mimeJavaScript); |
sschocke | 20:f5a6527bfda6 | 207 | } else if( request->URI == "/wifilamp.css" ) { |
sschocke | 20:f5a6527bfda6 | 208 | server.SendReply(request->LinkID, css, strlen(css), mimeCSS); |
sschocke | 22:6d7a72fab8ff | 209 | } else if( request->URI == "/colormap.gif" ) { |
sschocke | 23:3563e1699fb9 | 210 | server.SendReply(request->LinkID, (char*)colormap, sizeof(colormap), mimeGIF); |
sschocke | 23:3563e1699fb9 | 211 | } else if( request->URI == "/config.gif" ) { |
sschocke | 23:3563e1699fb9 | 212 | server.SendReply(request->LinkID, (char*)configIcon, sizeof(configIcon), mimeGIF); |
leet | 16:f2f2da9ef9ab | 213 | } else if( request->URI == "/setcolour" || request->URI == "/setcolor" ) { |
sschocke | 7:f15c81074400 | 214 | int r=0, g=0, b=0; |
sschocke | 9:319aeb6e0123 | 215 | |
sschocke | 9:319aeb6e0123 | 216 | if(request->Parameters.count("r") > 0) r=atoi(request->Parameters["r"].c_str()); |
sschocke | 9:319aeb6e0123 | 217 | if(request->Parameters.count("g") > 0) g=atoi(request->Parameters["g"].c_str()); |
sschocke | 9:319aeb6e0123 | 218 | if(request->Parameters.count("b") > 0) b=atoi(request->Parameters["b"].c_str()); |
sschocke | 7:f15c81074400 | 219 | |
leet | 16:f2f2da9ef9ab | 220 | pc.printf( "Set colour to (%i, %i, %i)\r\n", r,g,b); |
sschocke | 0:d21e3e1c0a4b | 221 | |
sschocke | 7:f15c81074400 | 222 | setColor( r,g,b ); |
sschocke | 7:f15c81074400 | 223 | |
sschocke | 23:3563e1699fb9 | 224 | server.SendReply(request->LinkID, "OK", mimeText, 0); |
sschocke | 7:f15c81074400 | 225 | } else { |
sschocke | 11:3ab606a42227 | 226 | server.Send404Error(request->LinkID); |
sschocke | 0:d21e3e1c0a4b | 227 | } |
sschocke | 7:f15c81074400 | 228 | pc.printf("\r\nHTTP Reply Sent\r\n"); |
sschocke | 9:319aeb6e0123 | 229 | delete request; |
sschocke | 0:d21e3e1c0a4b | 230 | } |
sschocke | 0:d21e3e1c0a4b | 231 | } |
sschocke | 0:d21e3e1c0a4b | 232 | } |