LeeT WiFiLamp code and test
Dependencies: ESP8266_WebServer mbed
Fork of WiFiLamp by
Diff: main.cpp
- Revision:
- 7:f15c81074400
- Parent:
- 4:4a502f72cbe3
- Child:
- 8:f819de1946a7
--- a/main.cpp Thu Dec 18 15:02:20 2014 +0000 +++ b/main.cpp Thu Dec 18 16:06:10 2014 +0000 @@ -1,5 +1,6 @@ #include "mbed.h" #include "PololuLedStrip.h" +#include "ESP8266_WebServer.h" #include <string> #define DEBUG_WIFI @@ -15,56 +16,10 @@ 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; -} +ESP8266_WebServer server(&wifiSerial); -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; +void rxint(void) { + server.rxint(); } void setColor( uint8_t r, uint8_t g, uint8_t b ) @@ -88,6 +43,9 @@ wifiReset = 0; wifiSerial.baud(9600); wifiSerial.attach(&rxint); +#ifdef DEBUG_WIFI + server.debugSerial = &pc; +#endif wait_ms(1000); pc.printf("Powering WiFi...\r\n"); @@ -96,34 +54,9 @@ 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("Starting Web Server...\r\n"); + server.Initialize(); pc.printf("Done\r\n"); setColor( 0, 250, 0); @@ -131,80 +64,35 @@ setColor( 0, 0, 0); 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"; - setColor(0,0,0); + 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" ) { - } 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"; - setColor(100, 0, 0); - - } else if ( uri.substr(0, 9) == "/setcolor" ) { - - int r=0, g=0, b=0; - - r = atoi( uri.substr( 12,3 ).c_str() ); - g = atoi( uri.substr( 18,3 ).c_str() ); - b = atoi( 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>"; - httpReplyPacket = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s"; - - } 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"; - } + 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); - 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"); + 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);