LeeT WiFiLamp code and test

Dependencies:   ESP8266_WebServer mbed

Fork of WiFiLamp by Sebastian Schocke

Revision:
9:319aeb6e0123
Parent:
8:f819de1946a7
Child:
10:f48a9f923271
Child:
11:3ab606a42227
--- a/main.cpp	Fri Dec 19 09:08:56 2014 +0000
+++ b/main.cpp	Sat Dec 27 05:56:47 2014 +0000
@@ -64,35 +64,39 @@
     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());
+        ESP8266_WebRequest* request = server.GetRequest();
+        if( request != NULL ) {
+            pc.printf("HTTP %s %s\r\n", request->Method.c_str(), request->URI.c_str());
+            for( std::map<std::string,std::string>::iterator it = request->Parameters.begin(); it!=request->Parameters.end(); ++it ) {
+                pc.printf("HTTP Parameter %s = %s\r\n", it->first.c_str(), it->second.c_str());
+            }
             std::string httpReply;
-            if( server.URI == "/" ) {
+            if( request->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" ) {
+                server.SendHTMLReply(request->LinkID, httpReply);
+            } else if( request->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" ) {
-                
+                server.SendHTMLReply(request->LinkID, httpReply);
+            } else if ( request->URI == "/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() );
+                
+                if(request->Parameters.count("r") > 0) r=atoi(request->Parameters["r"].c_str());
+                if(request->Parameters.count("g") > 0) g=atoi(request->Parameters["g"].c_str());
+                if(request->Parameters.count("b") > 0) b=atoi(request->Parameters["b"].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);
+                server.SendHTMLReply(request->LinkID, httpReply);
             } else {
-                server.Send404Reply(server.LinkID);
+                server.Send404Reply(request->LinkID);
             }
             pc.printf("\r\nHTTP Reply Sent\r\n");
+            delete request;
         }
         
         wait_ms(10);