LeeT WiFiLamp code and test

Dependencies:   ESP8266_WebServer mbed

Fork of WiFiLamp by Sebastian Schocke

main.cpp

Committer:
sschocke
Date:
2014-11-20
Revision:
1:f07afcffeb5a
Parent:
0:d21e3e1c0a4b
Child:
4:4a502f72cbe3

File content as of revision 1:f07afcffeb5a:

#include "mbed.h"
#include "PololuLedStrip.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);

char buffer[1024];
char reply[1024];
char response[2048];
char httpMethod[64];
char httpURI[512];
char* rxptr = buffer;

void rxint() {
    char c = wifiSerial.getc();
    if( wifiOn == 0 ) return;
#ifdef DEBUG_WIFI
    pc.putc(c);
#endif
    *rxptr = c;
    rxptr++;
    *rxptr = 0;
}

void readBuffer() {
    strncpy(reply, buffer, 1024);
    rxptr = buffer;
    *rxptr = 0;
}

short data_waiting(void)
{
    char* ok = strstr(buffer, "OK\r\n");
    char* error = strstr(buffer, "ERROR\r\n");
    char* nochange = strstr(buffer, "no change\r\n");
    
    if( (ok != NULL) || (error != NULL ) || (nochange != NULL ) )
    {
        return 1;
    }

    return 0;
}

short string_waiting(const char* str)
{
    char* pr = strstr(buffer, str);
    char* error = strstr(buffer, "ERROR\r\n");
    
    if( (pr != NULL) || (error != NULL ) )
    {
        return 1;
    }

    return 0;
}

int main() {
    pc.printf("WiFi Lamp Test...\r\n");    
    colors[0].red = 0;
    colors[0].green = 0;
    colors[0].blue = 0;
    colors[1].red = 0;
    colors[1].green = 0;
    colors[1].blue = 0;
    colors[2].red = 0;
    colors[2].green = 0;
    colors[2].blue = 0;
    colors[3].red = 0;
    colors[3].green = 0;
    colors[3].blue = 0;
    wifiCHPD = 0;
    wifiReset = 0;
    wifiSerial.baud(9600);
    wifiSerial.attach(&rxint);
    ledStrip.write(colors, LED_COUNT);
    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;
    readBuffer();
    while( string_waiting("\r\nready\r\n") == 0 ) {
        wait_ms(10);
    }
    readBuffer();

    pc.printf("Starting WiFi...\r\n");
    pc.printf("Setting Operating Mode...");    
    wifiSerial.printf("AT+CWMODE=3\r\n");
    while( data_waiting() == 0 ) {
        wait_ms(10);
    }
    readBuffer();
    
    pc.printf("Done\r\nAccept Multiple connections...");    
    wifiSerial.printf("AT+CIPMUX=1\r\n");
    while( data_waiting() == 0 ) {
        wait_ms(10);
    }
    readBuffer();
    
    pc.printf("Done\r\nStarting Web Server...");    
    wifiSerial.printf("AT+CIPSERVER=1,80\r\n");
    while( data_waiting() == 0 ) {
        wait_ms(10);
    }
    readBuffer();
    
    pc.printf("Done\r\n");    
    
    while(true) {
        if( (string_waiting("+IPD") == 1) && (string_waiting("\r\nOK\r\n") == 1) ) {
            pc.printf("\r\nGot Data\r\n");
            readBuffer();
            
            char* ipdPacket = strstr(reply, "+IPD");
            int linkID, bytesRecv, ipdLen;
            int numMatched = sscanf(ipdPacket,"+IPD,%d,%d:%n", &linkID, &bytesRecv, &ipdLen);
            if( numMatched != 2 ) {
                pc.printf("IPD ERROR : Matched %d, LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", numMatched, linkID, bytesRecv, ipdLen);
                continue;
            }
            
            pc.printf("IPD Data: LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", linkID, bytesRecv, ipdLen);
            if( strstr(ipdPacket, "HTTP") != NULL ) {
                pc.printf("Got HTTP Request\r\n");
                char* httpPacket = ipdPacket + ipdLen;
                //pc.printf("HTTP Packet: %s\r\n", httpPacket);
                
                numMatched = sscanf(httpPacket, "%s %s HTTP/%*c.%*c", httpMethod, httpURI);
                if( numMatched != 2 ) {
                    pc.printf("HTTP ERROR : Matched %d, Method=%s, URI=%s\r\n", numMatched, httpMethod, httpURI);
                    continue;
                }
                pc.printf("HTTP %s %s\r\n", httpMethod, httpURI);
                
                std::string method = httpMethod;
                std::string uri = httpURI;
                
                std::string httpReply;
                std::string httpReplyPacket;
                if( uri == "/" ) {
                    httpReply = "<html><head><title>WiFi Lamp</title></head><body><h1>The WiFi Lamp is alive(<a href='/red'>Red</a>)</h1></body></html>";
                    httpReplyPacket = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s";
                    colors[0].red = 0;
                    colors[0].green = 0;
                    colors[0].blue = 0;
                    ledStrip.write(colors, LED_COUNT);
                } else if( 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>";
                    httpReplyPacket = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s";
                    colors[0].red = 64;
                    colors[0].green = 0;
                    colors[0].blue = 0;
                    ledStrip.write(colors, LED_COUNT);
                } else {
                    httpReply = "404 Not Found";
                    httpReplyPacket = "HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s";
                }
                
                sprintf(response, httpReplyPacket.c_str(), httpReply.length(), httpReply.c_str());
                int bytes = strlen(response);
                pc.printf("HTTP Reply Packet(%d bytes): %s\r\n", bytes, response);
                wifiSerial.printf("AT+CIPSEND=%d,%d\r\n", linkID, bytes);
                wait_ms(500);
                if( (string_waiting("\r\n>") == 1) ) {
                    wifiSerial.printf(response);
                }
                while( string_waiting("\r\nSEND OK\r\n") == 0 ) {
                    wait_ms(10);
                }
                pc.printf("\r\nHTTP Reply Sent\r\n");
            }
        }
        
        wait_ms(10);
    }
}