LeeT WiFiLamp code and test

Dependencies:   ESP8266_WebServer mbed

Fork of WiFiLamp by Sebastian Schocke

Revision:
1:f07afcffeb5a
Parent:
0:d21e3e1c0a4b
Child:
4:4a502f72cbe3
--- a/main.cpp	Sun Nov 16 13:28:19 2014 +0000
+++ b/main.cpp	Thu Nov 20 17:44:22 2014 +0000
@@ -1,7 +1,13 @@
 #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);
 
@@ -13,6 +19,8 @@
 char buffer[1024];
 char reply[1024];
 char response[2048];
+char httpMethod[64];
+char httpURI[512];
 char* rxptr = buffer;
 
 void rxint() {
@@ -61,10 +69,23 @@
 
 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");    
@@ -120,10 +141,38 @@
             if( strstr(ipdPacket, "HTTP") != NULL ) {
                 pc.printf("Got HTTP Request\r\n");
                 char* httpPacket = ipdPacket + ipdLen;
-                pc.printf("HTTP Packet: %s\r\n", httpPacket);
+                //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 = "<html><head><title>WiFi Lamp</title></head><body><h1>The WiFi Lamp is alive</h1></body></html>";
-                std::string httpReplyPacket = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s";
+                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);
@@ -139,6 +188,7 @@
                 pc.printf("\r\nHTTP Reply Sent\r\n");
             }
         }
+        
         wait_ms(10);
     }
 }
\ No newline at end of file