LeeT WiFiLamp code and test

Dependencies:   ESP8266_WebServer mbed

Fork of WiFiLamp by Sebastian Schocke

Revision:
0:d21e3e1c0a4b
Child:
1:f07afcffeb5a
diff -r 000000000000 -r d21e3e1c0a4b main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Nov 16 13:28:19 2014 +0000
@@ -0,0 +1,144 @@
+#include "mbed.h"
+#include <string>
+#define DEBUG_WIFI
+
+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* 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");    
+    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");    
+    
+    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);
+                
+                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";
+                
+                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);
+    }
+}
\ No newline at end of file