LeeT WiFiLamp code and test

Dependencies:   ESP8266_WebServer mbed

Fork of WiFiLamp by Sebastian Schocke

Committer:
sschocke
Date:
Thu Jan 01 15:00:33 2015 +0000
Revision:
22:6d7a72fab8ff
Parent:
20:f5a6527bfda6
Child:
23:3563e1699fb9
Added Color Picker function to home screen

Who changed what in which revision?

UserRevisionLine numberNew 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 0:d21e3e1c0a4b 17
sschocke 0:d21e3e1c0a4b 18 Serial wifiSerial(D8,D2);
sschocke 0:d21e3e1c0a4b 19 Serial pc(USBTX,USBRX);
sschocke 7:f15c81074400 20 ESP8266_WebServer server(&wifiSerial);
sschocke 0:d21e3e1c0a4b 21
sschocke 22:6d7a72fab8ff 22 char temp[200];
sschocke 20:f5a6527bfda6 23
sschocke 20:f5a6527bfda6 24 #ifdef DEBUG_WIFI
sschocke 20:f5a6527bfda6 25 void pcrxint(void) {
sschocke 20:f5a6527bfda6 26 server.debugBuffers(&pc);
sschocke 20:f5a6527bfda6 27 server.echoMode = true;
sschocke 20:f5a6527bfda6 28 }
sschocke 20:f5a6527bfda6 29 #endif
sschocke 20:f5a6527bfda6 30
sschocke 7:f15c81074400 31 void rxint(void) {
sschocke 7:f15c81074400 32 server.rxint();
sschocke 0:d21e3e1c0a4b 33 }
sschocke 0:d21e3e1c0a4b 34
tomvdb 4:4a502f72cbe3 35 void setColor( uint8_t r, uint8_t g, uint8_t b )
tomvdb 4:4a502f72cbe3 36 {
tomvdb 4:4a502f72cbe3 37 for ( int c = 0; c < LED_COUNT; c++ )
tomvdb 4:4a502f72cbe3 38 {
tomvdb 4:4a502f72cbe3 39 colors[c].red = r;
tomvdb 4:4a502f72cbe3 40 colors[c].green = g;
tomvdb 4:4a502f72cbe3 41 colors[c].blue = b;
tomvdb 4:4a502f72cbe3 42 }
tomvdb 4:4a502f72cbe3 43
tomvdb 4:4a502f72cbe3 44 ledStrip.write(colors, LED_COUNT);
tomvdb 4:4a502f72cbe3 45 }
tomvdb 4:4a502f72cbe3 46
sschocke 0:d21e3e1c0a4b 47 int main() {
sschocke 13:1e8f27da036a 48 pc.baud(115200);
sschocke 20:f5a6527bfda6 49 #ifdef DEBUG_WIFI
sschocke 20:f5a6527bfda6 50 pc.attach(&pcrxint);
sschocke 20:f5a6527bfda6 51 #endif
leet 10:f48a9f923271 52
leet 16:f2f2da9ef9ab 53 pc.printf("WiFi Lamp - v0.02 ...\r\n");
leet 10:f48a9f923271 54
sschocke 13:1e8f27da036a 55 setColor( 25, 0, 0);
tomvdb 4:4a502f72cbe3 56
sschocke 0:d21e3e1c0a4b 57 wifiCHPD = 0;
sschocke 0:d21e3e1c0a4b 58 wifiReset = 0;
sschocke 13:1e8f27da036a 59 wifiSerial.baud(115200);
sschocke 0:d21e3e1c0a4b 60 wifiSerial.attach(&rxint);
sschocke 7:f15c81074400 61 #ifdef DEBUG_WIFI
sschocke 7:f15c81074400 62 server.debugSerial = &pc;
sschocke 7:f15c81074400 63 #endif
sschocke 0:d21e3e1c0a4b 64 wait_ms(1000);
sschocke 0:d21e3e1c0a4b 65
sschocke 0:d21e3e1c0a4b 66 pc.printf("Powering WiFi...\r\n");
sschocke 0:d21e3e1c0a4b 67 wifiCHPD = 1;
sschocke 13:1e8f27da036a 68 wifiReset = 1;
sschocke 0:d21e3e1c0a4b 69 wait_ms(250);
sschocke 0:d21e3e1c0a4b 70 pc.printf("Hardware Reset WiFi...\r\n");
sschocke 0:d21e3e1c0a4b 71 wifiOn = 1;
sschocke 0:d21e3e1c0a4b 72
sschocke 7:f15c81074400 73 pc.printf("Starting Web Server...\r\n");
sschocke 7:f15c81074400 74 server.Initialize();
sschocke 0:d21e3e1c0a4b 75 pc.printf("Done\r\n");
sschocke 0:d21e3e1c0a4b 76
sschocke 13:1e8f27da036a 77 setColor( 0, 25, 0);
tomvdb 4:4a502f72cbe3 78 wait_ms(500);
tomvdb 4:4a502f72cbe3 79 setColor( 0, 0, 0);
tomvdb 4:4a502f72cbe3 80
sschocke 0:d21e3e1c0a4b 81 while(true) {
sschocke 9:319aeb6e0123 82 ESP8266_WebRequest* request = server.GetRequest();
sschocke 9:319aeb6e0123 83 if( request != NULL ) {
sschocke 9:319aeb6e0123 84 pc.printf("HTTP %s %s\r\n", request->Method.c_str(), request->URI.c_str());
sschocke 9:319aeb6e0123 85 for( std::map<std::string,std::string>::iterator it = request->Parameters.begin(); it!=request->Parameters.end(); ++it ) {
sschocke 9:319aeb6e0123 86 pc.printf("HTTP Parameter %s = %s\r\n", it->first.c_str(), it->second.c_str());
sschocke 9:319aeb6e0123 87 }
sschocke 7:f15c81074400 88 std::string httpReply;
sschocke 9:319aeb6e0123 89 if( request->URI == "/" ) {
sschocke 22:6d7a72fab8ff 90 httpReply = "<html><head><title>RGB WiFi Lamp</title>\
sschocke 22:6d7a72fab8ff 91 <link rel='stylesheet' href='wifilamp.css' />\
sschocke 22:6d7a72fab8ff 92 <script src='wifilamp.js'></script>\
sschocke 22:6d7a72fab8ff 93 </head>\
sschocke 22:6d7a72fab8ff 94 <body onLoad='onLoad()'>\
sschocke 22:6d7a72fab8ff 95 <table>\
sschocke 22:6d7a72fab8ff 96 <tr>\
sschocke 22:6d7a72fab8ff 97 <td style='width:250px' valign='top'>\
sschocke 22:6d7a72fab8ff 98 <img style='margin-right:2px' src='colormap.gif' usemap='#colormap' />\
sschocke 22:6d7a72fab8ff 99 <map id='colormap' name='colormap'>";
sschocke 22:6d7a72fab8ff 100 int startx = 63;
sschocke 22:6d7a72fab8ff 101 int countx = 7;
sschocke 22:6d7a72fab8ff 102 for( int y=0; y<=180; y+=15) {
sschocke 22:6d7a72fab8ff 103 int endx = startx + (countx*18);
sschocke 22:6d7a72fab8ff 104 for( int x=startx; x<endx; x+= 18) {
sschocke 22:6d7a72fab8ff 105 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 22:6d7a72fab8ff 106 httpReply += temp;
sschocke 22:6d7a72fab8ff 107 }
sschocke 22:6d7a72fab8ff 108 if( y < 90 ) {
sschocke 22:6d7a72fab8ff 109 startx -= 9;
sschocke 22:6d7a72fab8ff 110 countx++;
sschocke 22:6d7a72fab8ff 111 } else {
sschocke 22:6d7a72fab8ff 112 startx += 9;
sschocke 22:6d7a72fab8ff 113 countx--;
sschocke 22:6d7a72fab8ff 114 }
sschocke 22:6d7a72fab8ff 115 }
sschocke 22:6d7a72fab8ff 116 httpReply += "</map><br/><span onClick=\"changeColor('#000000')\">Turn Off</span></td><td align='right'></td></tr></table></body>";
sschocke 12:fbf950985e1c 117 server.SendReply(request->LinkID, httpReply, mimeHTML);
sschocke 20:f5a6527bfda6 118 } else if( request->URI == "/wifilamp.js" ) {
sschocke 20:f5a6527bfda6 119 server.SendReply(request->LinkID, javascript, strlen(javascript), mimeJavaScript);
sschocke 20:f5a6527bfda6 120 } else if( request->URI == "/wifilamp.css" ) {
sschocke 20:f5a6527bfda6 121 server.SendReply(request->LinkID, css, strlen(css), mimeCSS);
sschocke 22:6d7a72fab8ff 122 } else if( request->URI == "/colormap.gif" ) {
sschocke 22:6d7a72fab8ff 123 server.SendReply(request->LinkID, (char*)colormap, sizeof(colormap), "image/gif");
sschocke 9:319aeb6e0123 124 } else if( request->URI == "/red" ) {
leet 10:f48a9f923271 125 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>";
sschocke 7:f15c81074400 126 setColor(100, 0, 0);
sschocke 11:3ab606a42227 127 server.SendReply(request->LinkID, httpReply, mimeHTML);
leet 10:f48a9f923271 128 } else if( request->URI == "/green" ) {
leet 10:f48a9f923271 129 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>";
leet 10:f48a9f923271 130 setColor(0, 100, 0);
sschocke 12:fbf950985e1c 131 server.SendReply(request->LinkID, httpReply, mimeHTML);
leet 10:f48a9f923271 132 } else if( request->URI == "/blue" ) {
leet 10:f48a9f923271 133 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>";
leet 10:f48a9f923271 134 setColor(0, 0, 100);
sschocke 12:fbf950985e1c 135 server.SendReply(request->LinkID, httpReply, mimeHTML);
leet 10:f48a9f923271 136 } else if( request->URI == "/white" ) {
leet 10:f48a9f923271 137 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>";
leet 10:f48a9f923271 138 setColor(100, 100, 100);
sschocke 12:fbf950985e1c 139 server.SendReply(request->LinkID, httpReply, mimeHTML);
leet 16:f2f2da9ef9ab 140 } else if( request->URI == "/setcolour" || request->URI == "/setcolor" ) {
sschocke 7:f15c81074400 141 int r=0, g=0, b=0;
sschocke 9:319aeb6e0123 142
sschocke 9:319aeb6e0123 143 if(request->Parameters.count("r") > 0) r=atoi(request->Parameters["r"].c_str());
sschocke 9:319aeb6e0123 144 if(request->Parameters.count("g") > 0) g=atoi(request->Parameters["g"].c_str());
sschocke 9:319aeb6e0123 145 if(request->Parameters.count("b") > 0) b=atoi(request->Parameters["b"].c_str());
sschocke 7:f15c81074400 146
leet 16:f2f2da9ef9ab 147 pc.printf( "Set colour to (%i, %i, %i)\r\n", r,g,b);
sschocke 0:d21e3e1c0a4b 148
sschocke 7:f15c81074400 149 setColor( r,g,b );
sschocke 7:f15c81074400 150
sschocke 7:f15c81074400 151 httpReply = "<html><head><title>WiFi Lamp</title></head><body>ok</body></html>";
sschocke 11:3ab606a42227 152 server.SendReply(request->LinkID, httpReply, mimeHTML);
sschocke 7:f15c81074400 153 } else {
sschocke 11:3ab606a42227 154 server.Send404Error(request->LinkID);
sschocke 0:d21e3e1c0a4b 155 }
sschocke 7:f15c81074400 156 pc.printf("\r\nHTTP Reply Sent\r\n");
sschocke 9:319aeb6e0123 157 delete request;
sschocke 0:d21e3e1c0a4b 158 }
sschocke 0:d21e3e1c0a4b 159 }
sschocke 0:d21e3e1c0a4b 160 }