WiFi WebServer for ESP8266

Fork of ESP8266_WebServer by Sebastian Schocke

Revision:
5:48b7fd921bef
Parent:
3:4ef7db7a95a7
Child:
6:34d93ea4d519
--- a/ESP8266_WebServer.cpp	Thu Jan 01 16:27:58 2015 +0000
+++ b/ESP8266_WebServer.cpp	Sun Jan 04 11:48:11 2015 +0000
@@ -1,5 +1,7 @@
 #include "ESP8266_WebServer.h"
 
+const char* opModes[] = {"ERROR", "Station", "SoftAP", "Station+SoftAP"};
+
 ESP8266_WebServer::ESP8266_WebServer(Serial *espUART) {
     serial = espUART;
     rxptr = buffer;
@@ -35,9 +37,6 @@
         }
         reqLen += ipdLen + 1;
     }
-    if( echoMode ) {
-        debugSerial->putc('@');
-    }
     *rxptr = c;
     rxptr++;
     *rxptr = 0;
@@ -201,6 +200,9 @@
 void ESP8266_WebServer::SendReply(int linkID, std::string reply, const char* mimeType) {
     SendReply(linkID, reply.c_str(), reply.length(), mimeType, 60);
 }
+void ESP8266_WebServer::SendReply(int linkID, std::string reply, const char* mimeType, int maxAge) {
+    SendReply(linkID, reply.c_str(), reply.length(), mimeType, maxAge);
+}
 
 void ESP8266_WebServer::SendReply(int linkID, char const* reply, int replySize, const char* mimeType) {
     SendReply(linkID, reply, replySize, mimeType, 600);
@@ -239,6 +241,199 @@
     }
 }
 
+std::string ESP8266_WebServer::GetStationMAC(void) {
+    while( reqMode == true ) { wait_ms(1); }
+    readBuffer();
+    serial->printf("AT+CIPSTAMAC?\r\n");
+    while( data_waiting() == 0 ) {
+        wait_ms(10);
+    }
+    readBuffer();
+    if( debugSerial != NULL ) {
+        debugSerial->printf("Station MAC Reply: %s\r\n", reply);
+    }
+    
+    std::string mac = std::string(reply);
+    mac = mac.substr(1, 17);
+    
+    return mac;
+}
+std::string ESP8266_WebServer::GetAPMAC(void) {
+    while( reqMode == true ) { wait_ms(1); }
+    readBuffer();
+    serial->printf("AT+CIPAPMAC?\r\n");
+    while( data_waiting() == 0 ) {
+        wait_ms(10);
+    }
+    readBuffer();
+    if( debugSerial != NULL ) {
+        debugSerial->printf("SoftAP MAC Reply: %s\r\n", reply);
+    }
+    
+    std::string mac = std::string(reply);
+    mac = mac.substr(1, 17);
+    
+    return mac;
+}
+std::string ESP8266_WebServer::GetStationIP(void) {
+    while( reqMode == true ) { wait_ms(1); }
+    readBuffer();
+    serial->printf("AT+CIPSTA?\r\n");
+    while( data_waiting() == 0 ) {
+        wait_ms(10);
+    }
+    readBuffer();
+    if( debugSerial != NULL ) {
+        debugSerial->printf("Station IP Reply: %s\r\n", reply);
+    }
+    
+    std::string ip = std::string(reply);
+    ip = ip.substr(1, ip.find('"', 1) - 1);
+    
+    return ip;
+}
+std::string ESP8266_WebServer::GetAPIP(void) {
+    while( reqMode == true ) { wait_ms(1); }
+    readBuffer();
+    serial->printf("AT+CIPAP?\r\n");
+    while( data_waiting() == 0 ) {
+        wait_ms(10);
+    }
+    readBuffer();
+    if( debugSerial != NULL ) {
+        debugSerial->printf("SoftAP IP Reply: %s\r\n", reply);
+    }
+    
+    std::string ip = std::string(reply);
+    ip = ip.substr(1, ip.find('"', 1) - 1);
+    
+    return ip;
+}
+int ESP8266_WebServer::GetOperatingMode(void) {
+    while( reqMode == true ) { wait_ms(1); }
+    readBuffer();
+    serial->printf("AT+CWMODE?\r\n");
+    while( data_waiting() == 0 ) {
+        wait_ms(10);
+    }
+    readBuffer();
+    if( debugSerial != NULL ) {
+        debugSerial->printf("Operating Mode Reply: %s\r\n", reply);
+    }
+    
+    return atoi(reply);
+}
+std::string ESP8266_WebServer::GetStationSSID(void) {
+    while( reqMode == true ) { wait_ms(1); }
+    readBuffer();
+    serial->printf("AT+CWJAP?\r\n");
+    while( data_waiting() == 0 ) {
+        wait_ms(10);
+    }
+    readBuffer();
+    if( debugSerial != NULL ) {
+        debugSerial->printf("Station SSID Reply: %s\r\n", reply);
+    }
+    
+    std::string ssid = std::string(reply);
+    if( strstr(reply, "No AP\r\n") != NULL ) { return "(None)"; }
+    ssid = ssid.substr(1, ssid.find('"', 1) - 1);
+    
+    return ssid;
+}
+std::list<std::string> ESP8266_WebServer::ListAvailableSSID(void) {
+    std::list<std::string> apList;
+    
+    while( reqMode == true ) { wait_ms(1); }
+    readBuffer();
+    serial->printf("AT+CWLAP\r\n");
+    while( data_waiting() == 0 ) {
+        wait_ms(10);
+    }
+    readBuffer();
+    if( debugSerial != NULL ) {
+        debugSerial->printf("SSID List Reply: %s\r\n", reply);
+    }
+    
+    char ssid[65];
+    char apmac[20];
+    int ecn;
+    int rssi;
+    int tokenLength;
+    char* token = strstr(reply, "(");
+    while( token != NULL ) {
+        int numMatched = sscanf(token, "(%d,\"%[^\"]\",%d,\"%[0123456789abcdef:]\",%*d)\r\n%n", &ecn, ssid, &rssi, apmac, &tokenLength);
+        if( numMatched < 4 ) {
+            if( debugSerial != NULL ) {
+                debugSerial->printf("SSID List Token Error: NumMatched=%d, SSID=%s\r\n", numMatched, ssid);
+            }
+            return apList;
+        }
+        if( debugSerial != NULL ) {
+            debugSerial->printf("SSID List Token : SSID=%s, ECN=%d, RSSI=%d, APMAC=%s, TokenLength=%d\r\n", ssid, ecn, rssi, apmac, tokenLength);
+        }
+        apList.push_back(std::string(ssid));
+        token += tokenLength;
+        token = strstr(token, "(");
+    }
+    
+    return apList;
+}
+std::string ESP8266_WebServer::GetFirmwareVersion(void) {
+    while( reqMode == true ) { wait_ms(1); }
+    readBuffer();
+    serial->printf("AT+GMR\r\n");
+    while( data_waiting() == 0 ) {
+        wait_ms(10);
+    }
+    readBuffer();
+    if( debugSerial != NULL ) {
+        debugSerial->printf("Firmware Version Reply: %s\r\n", reply);
+    }
+    
+    std::string ver = std::string(reply);
+    ver = ver.substr(0, ver.find('\r'));
+    
+    return ver;
+}
+
+bool ESP8266_WebServer::SetOperatingMode(int mode) {
+    if( debugSerial != NULL ) {
+        debugSerial->printf("Set Operating Mode to %s(%d)\r\n", opModes[mode], mode);
+    }
+    while( reqMode == true ) { wait_ms(1); }
+    readBuffer();
+    serial->printf("AT+CWMODE=%d\r\n", mode);
+    while( data_waiting() == 0 ) {
+        wait_ms(10);
+    }
+    readBuffer();
+    if( debugSerial != NULL ) {
+        debugSerial->printf("Set Operating Mode Reply: %s\r\n", reply);
+    }
+    if( strstr(reply, "\r\nOK\r\n") != NULL ) { return true; }
+    return false;
+}
+
+bool ESP8266_WebServer::SetStationSSID(std::string newAP, std::string password) {
+    if( debugSerial != NULL ) {
+        debugSerial->printf("Set Station SSID to %s, Password=%s\r\n", newAP.c_str(), password.c_str());
+    }
+    while( reqMode == true ) { wait_ms(1); }
+    readBuffer();
+    serial->printf("AT+CWJAP=\"%s\",\"%s\"\r\n", newAP.c_str(), password.c_str());
+    while( data_waiting() == 0 ) {
+        wait_ms(10);
+    }
+    readBuffer();
+    if( debugSerial != NULL ) {
+        debugSerial->printf("Set Station SSID Reply: %s\r\n", reply);
+    }
+    if( strstr(reply, "\r\nOK\r\n") != NULL ) { return true; }
+    
+    return false;
+}
+
 ESP8266_WebRequest::ESP8266_WebRequest(const char* packet, Serial* debug) {
     int sz = strlen(packet);
     data = (char *)malloc(sz+1);