LeeT WiFiLamp code and test

Dependencies:   ESP8266_WebServer mbed

Fork of WiFiLamp by Sebastian Schocke

Committer:
sschocke
Date:
Thu Dec 18 16:06:10 2014 +0000
Revision:
7:f15c81074400
Parent:
4:4a502f72cbe3
Child:
8:f819de1946a7
Moved all current WebServer functionality to ESP8266_WebServer class

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 0:d21e3e1c0a4b 5 #define DEBUG_WIFI
sschocke 0:d21e3e1c0a4b 6
sschocke 1:f07afcffeb5a 7 PololuLedStrip ledStrip(PA_7);
sschocke 1:f07afcffeb5a 8
sschocke 1:f07afcffeb5a 9 #define LED_COUNT 4
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 0:d21e3e1c0a4b 38 pc.printf("WiFi Lamp Test...\r\n");
tomvdb 4:4a502f72cbe3 39
tomvdb 4:4a502f72cbe3 40 setColor( 250, 0, 0);
tomvdb 4:4a502f72cbe3 41
sschocke 0:d21e3e1c0a4b 42 wifiCHPD = 0;
sschocke 0:d21e3e1c0a4b 43 wifiReset = 0;
sschocke 0:d21e3e1c0a4b 44 wifiSerial.baud(9600);
sschocke 0:d21e3e1c0a4b 45 wifiSerial.attach(&rxint);
sschocke 7:f15c81074400 46 #ifdef DEBUG_WIFI
sschocke 7:f15c81074400 47 server.debugSerial = &pc;
sschocke 7:f15c81074400 48 #endif
sschocke 0:d21e3e1c0a4b 49 wait_ms(1000);
sschocke 0:d21e3e1c0a4b 50
sschocke 0:d21e3e1c0a4b 51 pc.printf("Powering WiFi...\r\n");
sschocke 0:d21e3e1c0a4b 52 wifiCHPD = 1;
sschocke 0:d21e3e1c0a4b 53 wait_ms(250);
sschocke 0:d21e3e1c0a4b 54 pc.printf("Hardware Reset WiFi...\r\n");
sschocke 0:d21e3e1c0a4b 55 wifiReset = 1;
sschocke 0:d21e3e1c0a4b 56 wifiOn = 1;
sschocke 0:d21e3e1c0a4b 57
sschocke 7:f15c81074400 58 pc.printf("Starting Web Server...\r\n");
sschocke 7:f15c81074400 59 server.Initialize();
sschocke 0:d21e3e1c0a4b 60 pc.printf("Done\r\n");
sschocke 0:d21e3e1c0a4b 61
tomvdb 4:4a502f72cbe3 62 setColor( 0, 250, 0);
tomvdb 4:4a502f72cbe3 63 wait_ms(500);
tomvdb 4:4a502f72cbe3 64 setColor( 0, 0, 0);
tomvdb 4:4a502f72cbe3 65
sschocke 0:d21e3e1c0a4b 66 while(true) {
sschocke 7:f15c81074400 67 if( server.GetRequest() == true ) {
sschocke 7:f15c81074400 68 pc.printf("HTTP %s %s\r\n", server.Method.c_str(), server.URI.c_str());
sschocke 7:f15c81074400 69 std::string httpReply;
sschocke 7:f15c81074400 70 if( server.URI == "/" ) {
sschocke 7:f15c81074400 71 httpReply = "<html><head><title>WiFi Lamp</title></head><body><h1>The WiFi Lamp is alive(<a href='/red'>Red</a>)</h1></body></html>";
sschocke 7:f15c81074400 72 setColor(0,0,0);
sschocke 7:f15c81074400 73 server.SendHTMLReply(server.LinkID, httpReply);
sschocke 7:f15c81074400 74 } else if( server.URI == "/red" ) {
sschocke 7:f15c81074400 75 httpReply = "<html><head><title>WiFi Lamp</title></head><body><h1>The WiFi Lamp(Red) is alive(<a href='/'>Off</a>)</h1></body></html>";
sschocke 7:f15c81074400 76 setColor(100, 0, 0);
sschocke 7:f15c81074400 77 server.SendHTMLReply(server.LinkID, httpReply);
sschocke 7:f15c81074400 78 } else if ( server.URI.substr(0, 9) == "/setcolor" ) {
tomvdb 4:4a502f72cbe3 79
sschocke 7:f15c81074400 80 int r=0, g=0, b=0;
sschocke 7:f15c81074400 81
sschocke 7:f15c81074400 82 r = atoi( server.URI.substr( 12,3 ).c_str() );
sschocke 7:f15c81074400 83 g = atoi( server.URI.substr( 18,3 ).c_str() );
sschocke 7:f15c81074400 84 b = atoi( server.URI.substr( 24,3 ).c_str() );
sschocke 7:f15c81074400 85
sschocke 7:f15c81074400 86 pc.printf( "Set color to (%i, %i, %i)\r\n", r,g,b);
sschocke 0:d21e3e1c0a4b 87
sschocke 7:f15c81074400 88 setColor( r,g,b );
sschocke 7:f15c81074400 89
sschocke 7:f15c81074400 90 httpReply = "<html><head><title>WiFi Lamp</title></head><body>ok</body></html>";
sschocke 7:f15c81074400 91 server.SendHTMLReply(server.LinkID, httpReply);
sschocke 7:f15c81074400 92 } else {
sschocke 7:f15c81074400 93 server.Send404Reply(server.LinkID);
sschocke 0:d21e3e1c0a4b 94 }
sschocke 7:f15c81074400 95 pc.printf("\r\nHTTP Reply Sent\r\n");
sschocke 0:d21e3e1c0a4b 96 }
sschocke 1:f07afcffeb5a 97
sschocke 0:d21e3e1c0a4b 98 wait_ms(10);
sschocke 0:d21e3e1c0a4b 99 }
sschocke 0:d21e3e1c0a4b 100 }