LeeT WiFiLamp code and test
Dependencies: ESP8266_WebServer mbed
Fork of WiFiLamp by
main.cpp
- Committer:
- tomvdb
- Date:
- 2014-12-01
- Revision:
- 4:4a502f72cbe3
- Parent:
- 1:f07afcffeb5a
- Child:
- 7:f15c81074400
File content as of revision 4:4a502f72cbe3:
#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; } 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); 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"); setColor( 0, 250, 0); wait_ms(500); 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); } 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"; } 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); } }