LeeT WiFiLamp code and test

Dependencies:   ESP8266_WebServer mbed

Fork of WiFiLamp by Sebastian Schocke

main.cpp

Committer:
sschocke
Date:
2014-12-18
Revision:
7:f15c81074400
Parent:
4:4a502f72cbe3
Child:
8:f819de1946a7

File content as of revision 7:f15c81074400:

#include "mbed.h"
#include "PololuLedStrip.h"
#include "ESP8266_WebServer.h"
#include <string>
#define DEBUG_WIFI

PololuLedStrip ledStrip(PA_7);

#define LED_COUNT 4
rgb_color colors[LED_COUNT];

DigitalOut wifiCHPD(D4);
DigitalOut wifiReset(D9);

int wifiOn = 0;

Serial wifiSerial(D8,D2);
Serial pc(USBTX,USBRX);
ESP8266_WebServer server(&wifiSerial);

void rxint(void) {
    server.rxint();
}

void setColor( uint8_t r, uint8_t g, uint8_t b )
{
    for ( int c = 0; c < LED_COUNT; c++ )
    {
        colors[c].red = r;
        colors[c].green = g;
        colors[c].blue = b;
    }
    
    ledStrip.write(colors, LED_COUNT);
}

int main() {
    pc.printf("WiFi Lamp Test...\r\n");    

    setColor( 250, 0, 0);
    
    wifiCHPD = 0;
    wifiReset = 0;
    wifiSerial.baud(9600);
    wifiSerial.attach(&rxint);
#ifdef DEBUG_WIFI
    server.debugSerial = &pc;
#endif
    wait_ms(1000);
    
    pc.printf("Powering WiFi...\r\n");    
    wifiCHPD = 1;
    wait_ms(250);
    pc.printf("Hardware Reset WiFi...\r\n");    
    wifiReset = 1;
    wifiOn = 1;
    
    pc.printf("Starting Web Server...\r\n");
    server.Initialize();
    pc.printf("Done\r\n");    
    
    setColor( 0, 250, 0);
    wait_ms(500);
    setColor( 0, 0, 0);
    
    while(true) {
        if( server.GetRequest() == true ) {
            pc.printf("HTTP %s %s\r\n", server.Method.c_str(), server.URI.c_str());
            std::string httpReply;
            if( server.URI == "/" ) {
                httpReply = "<html><head><title>WiFi Lamp</title></head><body><h1>The WiFi Lamp is alive(<a href='/red'>Red</a>)</h1></body></html>";
                setColor(0,0,0);
                server.SendHTMLReply(server.LinkID, httpReply);
            } else if( server.URI == "/red" ) {
                httpReply = "<html><head><title>WiFi Lamp</title></head><body><h1>The WiFi Lamp(Red) is alive(<a href='/'>Off</a>)</h1></body></html>";
                setColor(100, 0, 0);
                server.SendHTMLReply(server.LinkID, httpReply);
            } else if ( server.URI.substr(0, 9) == "/setcolor" ) {
                
                int r=0, g=0, b=0;
                                   
                r = atoi( server.URI.substr( 12,3 ).c_str() );
                g = atoi( server.URI.substr( 18,3 ).c_str() );
                b = atoi( server.URI.substr( 24,3 ).c_str() );
                
                pc.printf( "Set color to (%i, %i, %i)\r\n", r,g,b);
                
                setColor( r,g,b );
                
                httpReply = "<html><head><title>WiFi Lamp</title></head><body>ok</body></html>";
                server.SendHTMLReply(server.LinkID, httpReply);
            } else {
                server.Send404Reply(server.LinkID);
            }
            pc.printf("\r\nHTTP Reply Sent\r\n");
        }
        
        wait_ms(10);
    }
}