LeeT WiFiLamp code and test

Dependencies:   ESP8266_WebServer mbed

Fork of WiFiLamp by Sebastian Schocke

Committer:
sschocke
Date:
Mon Dec 29 11:42:53 2014 +0000
Revision:
13:1e8f27da036a
Parent:
12:fbf950985e1c
Child:
19:7fdccfd5b50b
Updated revision after ESP8266 Firmware upgrade

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 0:d21e3e1c0a4b 4 #include <string>
sschocke 13:1e8f27da036a 5 //#define DEBUG_WIFI
sschocke 0:d21e3e1c0a4b 6
sschocke 8:f819de1946a7 7 PololuLedStrip ledStrip(D11);
sschocke 1:f07afcffeb5a 8
sschocke 8:f819de1946a7 9 #define LED_COUNT 8
sschocke 1:f07afcffeb5a 10 rgb_color colors[LED_COUNT];
sschocke 1:f07afcffeb5a 11
sschocke 0:d21e3e1c0a4b 12 DigitalOut wifiCHPD(D4);
sschocke 0:d21e3e1c0a4b 13 DigitalOut wifiReset(D9);
sschocke 0:d21e3e1c0a4b 14
sschocke 0:d21e3e1c0a4b 15 int wifiOn = 0;
sschocke 0:d21e3e1c0a4b 16
sschocke 0:d21e3e1c0a4b 17 Serial wifiSerial(D8,D2);
sschocke 0:d21e3e1c0a4b 18 Serial pc(USBTX,USBRX);
sschocke 7:f15c81074400 19 ESP8266_WebServer server(&wifiSerial);
sschocke 0:d21e3e1c0a4b 20
sschocke 7:f15c81074400 21 void rxint(void) {
sschocke 7:f15c81074400 22 server.rxint();
sschocke 0:d21e3e1c0a4b 23 }
sschocke 0:d21e3e1c0a4b 24
tomvdb 4:4a502f72cbe3 25 void setColor( uint8_t r, uint8_t g, uint8_t b )
tomvdb 4:4a502f72cbe3 26 {
tomvdb 4:4a502f72cbe3 27 for ( int c = 0; c < LED_COUNT; c++ )
tomvdb 4:4a502f72cbe3 28 {
tomvdb 4:4a502f72cbe3 29 colors[c].red = r;
tomvdb 4:4a502f72cbe3 30 colors[c].green = g;
tomvdb 4:4a502f72cbe3 31 colors[c].blue = b;
tomvdb 4:4a502f72cbe3 32 }
tomvdb 4:4a502f72cbe3 33
tomvdb 4:4a502f72cbe3 34 ledStrip.write(colors, LED_COUNT);
tomvdb 4:4a502f72cbe3 35 }
tomvdb 4:4a502f72cbe3 36
sschocke 0:d21e3e1c0a4b 37 int main() {
sschocke 13:1e8f27da036a 38 pc.baud(115200);
leet 10:f48a9f923271 39
leet 10:f48a9f923271 40 pc.printf("WiFi Lamp Test - v0.01 ...\r\n");
leet 10:f48a9f923271 41
sschocke 13:1e8f27da036a 42 setColor( 25, 0, 0);
tomvdb 4:4a502f72cbe3 43
sschocke 0:d21e3e1c0a4b 44 wifiCHPD = 0;
sschocke 0:d21e3e1c0a4b 45 wifiReset = 0;
sschocke 13:1e8f27da036a 46 wifiSerial.baud(115200);
sschocke 0:d21e3e1c0a4b 47 wifiSerial.attach(&rxint);
sschocke 7:f15c81074400 48 #ifdef DEBUG_WIFI
sschocke 7:f15c81074400 49 server.debugSerial = &pc;
sschocke 7:f15c81074400 50 #endif
sschocke 0:d21e3e1c0a4b 51 wait_ms(1000);
sschocke 0:d21e3e1c0a4b 52
sschocke 0:d21e3e1c0a4b 53 pc.printf("Powering WiFi...\r\n");
sschocke 0:d21e3e1c0a4b 54 wifiCHPD = 1;
sschocke 13:1e8f27da036a 55 wifiReset = 1;
sschocke 0:d21e3e1c0a4b 56 wait_ms(250);
sschocke 0:d21e3e1c0a4b 57 pc.printf("Hardware Reset WiFi...\r\n");
sschocke 0:d21e3e1c0a4b 58 wifiOn = 1;
sschocke 0:d21e3e1c0a4b 59
sschocke 7:f15c81074400 60 pc.printf("Starting Web Server...\r\n");
sschocke 7:f15c81074400 61 server.Initialize();
sschocke 0:d21e3e1c0a4b 62 pc.printf("Done\r\n");
sschocke 0:d21e3e1c0a4b 63
sschocke 13:1e8f27da036a 64 setColor( 0, 25, 0);
tomvdb 4:4a502f72cbe3 65 wait_ms(500);
tomvdb 4:4a502f72cbe3 66 setColor( 0, 0, 0);
tomvdb 4:4a502f72cbe3 67
sschocke 0:d21e3e1c0a4b 68 while(true) {
sschocke 9:319aeb6e0123 69 ESP8266_WebRequest* request = server.GetRequest();
sschocke 9:319aeb6e0123 70 if( request != NULL ) {
sschocke 9:319aeb6e0123 71 pc.printf("HTTP %s %s\r\n", request->Method.c_str(), request->URI.c_str());
sschocke 9:319aeb6e0123 72 for( std::map<std::string,std::string>::iterator it = request->Parameters.begin(); it!=request->Parameters.end(); ++it ) {
sschocke 9:319aeb6e0123 73 pc.printf("HTTP Parameter %s = %s\r\n", it->first.c_str(), it->second.c_str());
sschocke 9:319aeb6e0123 74 }
sschocke 7:f15c81074400 75 std::string httpReply;
sschocke 9:319aeb6e0123 76 if( request->URI == "/" ) {
leet 10:f48a9f923271 77 httpReply = "<html><title>WiFi Lamp</title><body><h1>The WiFi Lamp V0.01 is alive ...</h1><br>Quick colour links:<br><a href='/red'>Red</a><br><a href='/green'>Green</a><br><a href='/blue'>Blue</a><br><a href='/white'>White</a><br></body></html>";
sschocke 7:f15c81074400 78 setColor(0,0,0);
sschocke 12:fbf950985e1c 79 server.SendReply(request->LinkID, httpReply, mimeHTML);
sschocke 9:319aeb6e0123 80 } else if( request->URI == "/red" ) {
leet 10:f48a9f923271 81 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 82 setColor(100, 0, 0);
sschocke 11:3ab606a42227 83 server.SendReply(request->LinkID, httpReply, mimeHTML);
leet 10:f48a9f923271 84 } else if( request->URI == "/green" ) {
leet 10:f48a9f923271 85 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 86 setColor(0, 100, 0);
sschocke 12:fbf950985e1c 87 server.SendReply(request->LinkID, httpReply, mimeHTML);
leet 10:f48a9f923271 88 } else if( request->URI == "/blue" ) {
leet 10:f48a9f923271 89 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 90 setColor(0, 0, 100);
sschocke 12:fbf950985e1c 91 server.SendReply(request->LinkID, httpReply, mimeHTML);
leet 10:f48a9f923271 92 } else if( request->URI == "/white" ) {
leet 10:f48a9f923271 93 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 94 setColor(100, 100, 100);
sschocke 12:fbf950985e1c 95 server.SendReply(request->LinkID, httpReply, mimeHTML);
leet 10:f48a9f923271 96 } else if( request->URI == "/setcolor" ) {
sschocke 7:f15c81074400 97 int r=0, g=0, b=0;
sschocke 9:319aeb6e0123 98
sschocke 9:319aeb6e0123 99 if(request->Parameters.count("r") > 0) r=atoi(request->Parameters["r"].c_str());
sschocke 9:319aeb6e0123 100 if(request->Parameters.count("g") > 0) g=atoi(request->Parameters["g"].c_str());
sschocke 9:319aeb6e0123 101 if(request->Parameters.count("b") > 0) b=atoi(request->Parameters["b"].c_str());
sschocke 7:f15c81074400 102
sschocke 7:f15c81074400 103 pc.printf( "Set color to (%i, %i, %i)\r\n", r,g,b);
sschocke 0:d21e3e1c0a4b 104
sschocke 7:f15c81074400 105 setColor( r,g,b );
sschocke 7:f15c81074400 106
sschocke 7:f15c81074400 107 httpReply = "<html><head><title>WiFi Lamp</title></head><body>ok</body></html>";
sschocke 11:3ab606a42227 108 server.SendReply(request->LinkID, httpReply, mimeHTML);
sschocke 7:f15c81074400 109 } else {
sschocke 11:3ab606a42227 110 server.Send404Error(request->LinkID);
sschocke 0:d21e3e1c0a4b 111 }
sschocke 7:f15c81074400 112 pc.printf("\r\nHTTP Reply Sent\r\n");
sschocke 9:319aeb6e0123 113 delete request;
sschocke 0:d21e3e1c0a4b 114 }
sschocke 1:f07afcffeb5a 115
sschocke 0:d21e3e1c0a4b 116 wait_ms(10);
sschocke 0:d21e3e1c0a4b 117 }
sschocke 0:d21e3e1c0a4b 118 }