Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 6:34d93ea4d519, committed 2015-01-04
- Comitter:
- sschocke
- Date:
- Sun Jan 04 12:11:33 2015 +0000
- Parent:
- 4:759de84e790b
- Parent:
- 5:48b7fd921bef
- Child:
- 7:f6172ba3e807
- Commit message:
- Merge WiFi parameters query and setting API with SendStream changes
Changed in this revision
| ESP8266_WebServer.cpp | Show annotated file Show diff for this revision Revisions of this file |
| ESP8266_WebServer.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/ESP8266_WebServer.cpp Sat Jan 03 17:36:41 2015 +0000
+++ b/ESP8266_WebServer.cpp Sun Jan 04 12:11:33 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);
@@ -245,6 +247,162 @@
return SendStream(linkID, reply, WindowSize);
}
+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;
+}
+
int ESP8266_WebServer::SendStream(int linkID, char const* reply, int WindowSize) {
char const* sendPtr = reply;
int bytesSent = 0;
@@ -263,6 +421,44 @@
return bytesSent;
}
+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);
--- a/ESP8266_WebServer.h Sat Jan 03 17:36:41 2015 +0000
+++ b/ESP8266_WebServer.h Sun Jan 04 12:11:33 2015 +0000
@@ -14,6 +14,13 @@
const char mimePNG[] = "image/png";
const char mimeGIF[] = "image/gif";
const char mimeText[] = "text/plain";
+const char mimeJSON[] = "application/json";
+
+#define OPMODE_STATION 1
+#define OPMODE_SOFTAP 2
+#define OPMODE_ALL 3
+
+extern const char* opModes[];
class ESP8266_WebRequest
{
@@ -66,6 +73,7 @@
void SendError(int linkID, const char* error);
void Send404Error(int linkID);
void SendReply(int linkID, std::string reply, const char* mimeType);
+ void SendReply(int linkID, std::string reply, const char* mimeType, int maxAge);
void SendReply(int linkID, char const* reply, int replySize, const char* mimeType);
void SendReply(int linkID, char const* reply, int replySize, const char* mimeType, int maxAge);
void SendReply(int linkID, char const* reply, const char* mimeType, int maxAge);
@@ -73,6 +81,18 @@
void SendFile(int linkID, FileHandle* file, const char* mimeType, int maxAge);
int SendStream(int linkID, char const* reply, int StreamSize, int WindowSize, const char* mimeType, int maxAge);
int SendStream(int linkID, char const* reply, int WindowSize);
+
+ std::string GetStationMAC(void);
+ std::string GetAPMAC(void);
+ std::string GetStationIP(void);
+ std::string GetAPIP(void);
+ int GetOperatingMode(void);
+ std::string GetStationSSID(void);
+ std::list<std::string> ListAvailableSSID(void);
+ std::string GetFirmwareVersion(void);
+
+ bool SetOperatingMode(int mode);
+ bool SetStationSSID(std::string newAP, std::string password);
};
#endif
\ No newline at end of file