WiFi WebServer for ESP8266

Fork of ESP8266_WebServer by Sebastian Schocke

Committer:
sschocke
Date:
Sun Jan 04 11:48:11 2015 +0000
Revision:
5:48b7fd921bef
Parent:
3:4ef7db7a95a7
Child:
6:34d93ea4d519
Added functions for querying and setting WiFi related parameters

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sschocke 0:56c72c87d2f5 1 #include "ESP8266_WebServer.h"
sschocke 0:56c72c87d2f5 2
sschocke 5:48b7fd921bef 3 const char* opModes[] = {"ERROR", "Station", "SoftAP", "Station+SoftAP"};
sschocke 5:48b7fd921bef 4
sschocke 0:56c72c87d2f5 5 ESP8266_WebServer::ESP8266_WebServer(Serial *espUART) {
sschocke 0:56c72c87d2f5 6 serial = espUART;
sschocke 0:56c72c87d2f5 7 rxptr = buffer;
sschocke 0:56c72c87d2f5 8 debugSerial = NULL;
sschocke 0:56c72c87d2f5 9 reqMode = false;
sschocke 0:56c72c87d2f5 10 reqLen = 0;
sschocke 0:56c72c87d2f5 11 }
sschocke 0:56c72c87d2f5 12
sschocke 0:56c72c87d2f5 13 void ESP8266_WebServer::rxint(void) {
sschocke 0:56c72c87d2f5 14 char c = serial->getc();
sschocke 0:56c72c87d2f5 15 if( (c != 0x0A) && (c != 0x0D) && ((c < 0x20) || (c > 0x80)) ) return;
sschocke 0:56c72c87d2f5 16 if( !reqMode && c == '+' ) {
sschocke 0:56c72c87d2f5 17 if( echoMode ) {
sschocke 0:56c72c87d2f5 18 debugSerial->putc('~');
sschocke 0:56c72c87d2f5 19 }
sschocke 0:56c72c87d2f5 20 rxptrStored = rxptr;
sschocke 0:56c72c87d2f5 21 rxptr = reqBuffer;
sschocke 0:56c72c87d2f5 22 reqMode = true;
sschocke 0:56c72c87d2f5 23 }
sschocke 0:56c72c87d2f5 24 if( echoMode ) {
sschocke 0:56c72c87d2f5 25 debugSerial->putc(c);
sschocke 0:56c72c87d2f5 26 }
sschocke 0:56c72c87d2f5 27 if( reqMode && reqLen == 0 && c == ':' ) {
sschocke 0:56c72c87d2f5 28 if( echoMode ) {
sschocke 0:56c72c87d2f5 29 debugSerial->putc('!');
sschocke 0:56c72c87d2f5 30 }
sschocke 0:56c72c87d2f5 31 int numMatched = sscanf(reqBuffer,"+IPD,%*d,%d%n", &reqLen, &ipdLen);
sschocke 0:56c72c87d2f5 32 if( numMatched < 1 ) {
sschocke 0:56c72c87d2f5 33 reqMode = false;
sschocke 0:56c72c87d2f5 34 reqLen = 0;
sschocke 0:56c72c87d2f5 35 rxptr = rxptrStored;
sschocke 0:56c72c87d2f5 36 return;
sschocke 0:56c72c87d2f5 37 }
sschocke 0:56c72c87d2f5 38 reqLen += ipdLen + 1;
sschocke 0:56c72c87d2f5 39 }
sschocke 0:56c72c87d2f5 40 *rxptr = c;
sschocke 0:56c72c87d2f5 41 rxptr++;
sschocke 0:56c72c87d2f5 42 *rxptr = 0;
sschocke 0:56c72c87d2f5 43 if( reqMode && reqLen > 0 ) {
sschocke 0:56c72c87d2f5 44 if( echoMode ) {
sschocke 0:56c72c87d2f5 45 debugSerial->putc('#');
sschocke 0:56c72c87d2f5 46 }
sschocke 0:56c72c87d2f5 47 if( (int)(rxptr - reqBuffer) == reqLen ) {
sschocke 0:56c72c87d2f5 48 // Process it
sschocke 0:56c72c87d2f5 49 queueRequest();
sschocke 0:56c72c87d2f5 50 // Return to normal buffer mode
sschocke 0:56c72c87d2f5 51 reqMode = false;
sschocke 0:56c72c87d2f5 52 reqLen = 0;
sschocke 0:56c72c87d2f5 53 rxptr = rxptrStored;
sschocke 0:56c72c87d2f5 54 }
sschocke 0:56c72c87d2f5 55 }
sschocke 0:56c72c87d2f5 56 }
sschocke 0:56c72c87d2f5 57
sschocke 0:56c72c87d2f5 58 void ESP8266_WebServer::debugBuffers(Serial* target) {
sschocke 0:56c72c87d2f5 59 target->printf("\r\n\r\nRequest Buffer '%s'\r\nReqLen=%d,ReqMode=%d\r\n", reqBuffer, reqLen, reqMode);
sschocke 0:56c72c87d2f5 60 }
sschocke 0:56c72c87d2f5 61
sschocke 0:56c72c87d2f5 62 void ESP8266_WebServer::readBuffer(void) {
sschocke 0:56c72c87d2f5 63 strncpy(reply, buffer, 1024);
sschocke 0:56c72c87d2f5 64 while(reqMode == true ) { wait_ms(10); }
sschocke 0:56c72c87d2f5 65
sschocke 0:56c72c87d2f5 66 rxptr = buffer;
sschocke 0:56c72c87d2f5 67 *rxptr = 0;
sschocke 0:56c72c87d2f5 68 }
sschocke 0:56c72c87d2f5 69
sschocke 0:56c72c87d2f5 70 short ESP8266_WebServer::data_waiting(void)
sschocke 0:56c72c87d2f5 71 {
sschocke 0:56c72c87d2f5 72 char* ok = strstr(buffer, "OK\r\n");
sschocke 0:56c72c87d2f5 73 char* error = strstr(buffer, "ERROR\r\n");
sschocke 0:56c72c87d2f5 74 char* nochange = strstr(buffer, "no change\r\n");
sschocke 0:56c72c87d2f5 75
sschocke 0:56c72c87d2f5 76 if( (ok != NULL) || (error != NULL ) || (nochange != NULL ) )
sschocke 0:56c72c87d2f5 77 {
sschocke 0:56c72c87d2f5 78 return 1;
sschocke 0:56c72c87d2f5 79 }
sschocke 0:56c72c87d2f5 80
sschocke 0:56c72c87d2f5 81 return 0;
sschocke 0:56c72c87d2f5 82 }
sschocke 0:56c72c87d2f5 83
sschocke 0:56c72c87d2f5 84 short ESP8266_WebServer::string_waiting(const char* str)
sschocke 0:56c72c87d2f5 85 {
sschocke 0:56c72c87d2f5 86 char* pr = strstr(buffer, str);
sschocke 0:56c72c87d2f5 87 char* error = strstr(buffer, "ERROR\r\n");
sschocke 0:56c72c87d2f5 88
sschocke 0:56c72c87d2f5 89 if( (pr != NULL) || (error != NULL ) )
sschocke 0:56c72c87d2f5 90 {
sschocke 0:56c72c87d2f5 91 return 1;
sschocke 0:56c72c87d2f5 92 }
sschocke 0:56c72c87d2f5 93
sschocke 0:56c72c87d2f5 94 return 0;
sschocke 0:56c72c87d2f5 95 }
sschocke 0:56c72c87d2f5 96
sschocke 0:56c72c87d2f5 97 void ESP8266_WebServer::Initialize(void) {
sschocke 0:56c72c87d2f5 98 readBuffer();
sschocke 0:56c72c87d2f5 99 serial->printf("AT+RST\r\n");
sschocke 0:56c72c87d2f5 100 wait_ms(1000);
sschocke 0:56c72c87d2f5 101 while( string_waiting("\r\nready\r\n") == 0 ) {
sschocke 0:56c72c87d2f5 102 wait_ms(100);
sschocke 0:56c72c87d2f5 103 }
sschocke 0:56c72c87d2f5 104 readBuffer();
sschocke 0:56c72c87d2f5 105 serial->printf("ATE0\r\n");
sschocke 0:56c72c87d2f5 106 while( data_waiting() == 0 ) {
sschocke 0:56c72c87d2f5 107 wait_ms(10);
sschocke 0:56c72c87d2f5 108 }
sschocke 0:56c72c87d2f5 109 readBuffer();
sschocke 0:56c72c87d2f5 110
sschocke 0:56c72c87d2f5 111 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 112 debugSerial->printf("Done\r\nSetting operating mode...");
sschocke 0:56c72c87d2f5 113 }
sschocke 0:56c72c87d2f5 114 serial->printf("AT+CWMODE=3\r\n");
sschocke 0:56c72c87d2f5 115 while( data_waiting() == 0 ) {
sschocke 0:56c72c87d2f5 116 wait_ms(10);
sschocke 0:56c72c87d2f5 117 }
sschocke 0:56c72c87d2f5 118 readBuffer();
sschocke 0:56c72c87d2f5 119
sschocke 0:56c72c87d2f5 120 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 121 debugSerial->printf("Done\r\nAccept Multiple connections...");
sschocke 0:56c72c87d2f5 122 }
sschocke 0:56c72c87d2f5 123 serial->printf("AT+CIPMUX=1\r\n");
sschocke 0:56c72c87d2f5 124 while( data_waiting() == 0 ) {
sschocke 0:56c72c87d2f5 125 wait_ms(10);
sschocke 0:56c72c87d2f5 126 }
sschocke 0:56c72c87d2f5 127 readBuffer();
sschocke 0:56c72c87d2f5 128
sschocke 0:56c72c87d2f5 129 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 130 debugSerial->printf("Done\r\nStarting Web Server...");
sschocke 0:56c72c87d2f5 131 }
sschocke 0:56c72c87d2f5 132
sschocke 0:56c72c87d2f5 133 serial->printf("AT+CIPSERVER=1,80\r\n");
sschocke 0:56c72c87d2f5 134 while( data_waiting() == 0 ) {
sschocke 0:56c72c87d2f5 135 wait_ms(10);
sschocke 0:56c72c87d2f5 136 }
sschocke 0:56c72c87d2f5 137 readBuffer();
sschocke 0:56c72c87d2f5 138 }
sschocke 0:56c72c87d2f5 139
sschocke 0:56c72c87d2f5 140 void ESP8266_WebServer::queueRequest(void) {
sschocke 0:56c72c87d2f5 141 if( strstr(reqBuffer, "HTTP") != NULL ) {
sschocke 0:56c72c87d2f5 142 ESP8266_WebRequest* request = new ESP8266_WebRequest(reqBuffer, debugSerial);
sschocke 0:56c72c87d2f5 143
sschocke 0:56c72c87d2f5 144 incoming.push(request);
sschocke 0:56c72c87d2f5 145 }
sschocke 0:56c72c87d2f5 146 }
sschocke 0:56c72c87d2f5 147
sschocke 0:56c72c87d2f5 148 ESP8266_WebRequest* ESP8266_WebServer::GetRequest(void)
sschocke 0:56c72c87d2f5 149 {
sschocke 0:56c72c87d2f5 150 if( incoming.empty() == false ) {
sschocke 0:56c72c87d2f5 151 ESP8266_WebRequest* temp = incoming.front();
sschocke 0:56c72c87d2f5 152 incoming.pop();
sschocke 0:56c72c87d2f5 153 temp->Read();
sschocke 0:56c72c87d2f5 154
sschocke 0:56c72c87d2f5 155 return temp;
sschocke 0:56c72c87d2f5 156 }
sschocke 0:56c72c87d2f5 157
sschocke 0:56c72c87d2f5 158 return NULL;
sschocke 0:56c72c87d2f5 159 }
sschocke 0:56c72c87d2f5 160
sschocke 0:56c72c87d2f5 161 void ESP8266_WebServer::sendResponse(int linkID, int numBytes) {
sschocke 0:56c72c87d2f5 162 bool sent = false;
sschocke 0:56c72c87d2f5 163
sschocke 0:56c72c87d2f5 164 readBuffer();
sschocke 0:56c72c87d2f5 165 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 166 debugSerial->printf("HTTP Reply Packet(%d bytes)\r\n", numBytes);
sschocke 0:56c72c87d2f5 167 }
sschocke 0:56c72c87d2f5 168 while( sent == false ) {
sschocke 0:56c72c87d2f5 169 while( reqMode == true ) { wait_ms(1); }
sschocke 0:56c72c87d2f5 170 serial->printf("AT+CIPSEND=%d,%d\r\n", linkID, numBytes);
sschocke 0:56c72c87d2f5 171 wait_ms(100);
sschocke 0:56c72c87d2f5 172 if( (string_waiting(">") == 1) ) {
sschocke 0:56c72c87d2f5 173 char* error = strstr(buffer, "ERROR\r\n");
sschocke 0:56c72c87d2f5 174 if( error != NULL ) { continue; }
sschocke 0:56c72c87d2f5 175 for( int i=0; i<numBytes; i++ ) {
sschocke 0:56c72c87d2f5 176 serial->putc(response[i]);
sschocke 0:56c72c87d2f5 177 }
sschocke 0:56c72c87d2f5 178 while( string_waiting("\r\nSEND OK\r\n") == 0 ) {
sschocke 0:56c72c87d2f5 179 wait_ms(10);
sschocke 0:56c72c87d2f5 180 }
sschocke 0:56c72c87d2f5 181 error = strstr(buffer, "ERROR\r\n");
sschocke 0:56c72c87d2f5 182 if( error != NULL ) { continue; }
sschocke 0:56c72c87d2f5 183 sent = true;
sschocke 0:56c72c87d2f5 184 }
sschocke 0:56c72c87d2f5 185 }
sschocke 0:56c72c87d2f5 186 readBuffer();
sschocke 0:56c72c87d2f5 187 }
sschocke 0:56c72c87d2f5 188
sschocke 0:56c72c87d2f5 189 void ESP8266_WebServer::SendError(int linkID, std::string error) {
sschocke 0:56c72c87d2f5 190 SendError(linkID, error.c_str());
sschocke 0:56c72c87d2f5 191 }
sschocke 0:56c72c87d2f5 192 void ESP8266_WebServer::SendError(int linkID, const char* error) {
sschocke 0:56c72c87d2f5 193 sprintf(response, "HTTP/1.1 %s\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s", error, strlen(error), error);
sschocke 0:56c72c87d2f5 194 sendResponse(linkID, strlen(response));
sschocke 0:56c72c87d2f5 195 }
sschocke 0:56c72c87d2f5 196 void ESP8266_WebServer::Send404Error(int linkID) {
sschocke 0:56c72c87d2f5 197 SendError(linkID, "404 Not Found");
sschocke 0:56c72c87d2f5 198 }
sschocke 0:56c72c87d2f5 199
sschocke 0:56c72c87d2f5 200 void ESP8266_WebServer::SendReply(int linkID, std::string reply, const char* mimeType) {
sschocke 3:4ef7db7a95a7 201 SendReply(linkID, reply.c_str(), reply.length(), mimeType, 60);
sschocke 0:56c72c87d2f5 202 }
sschocke 5:48b7fd921bef 203 void ESP8266_WebServer::SendReply(int linkID, std::string reply, const char* mimeType, int maxAge) {
sschocke 5:48b7fd921bef 204 SendReply(linkID, reply.c_str(), reply.length(), mimeType, maxAge);
sschocke 5:48b7fd921bef 205 }
sschocke 0:56c72c87d2f5 206
sschocke 2:6079554681d6 207 void ESP8266_WebServer::SendReply(int linkID, char const* reply, int replySize, const char* mimeType) {
sschocke 3:4ef7db7a95a7 208 SendReply(linkID, reply, replySize, mimeType, 600);
sschocke 3:4ef7db7a95a7 209 }
sschocke 3:4ef7db7a95a7 210 void ESP8266_WebServer::SendReply(int linkID, char const* reply, const char* mimeType, int maxAge) {
sschocke 3:4ef7db7a95a7 211 SendReply(linkID, reply, strlen(reply), mimeType, maxAge);
sschocke 3:4ef7db7a95a7 212 }
sschocke 3:4ef7db7a95a7 213 void ESP8266_WebServer::SendReply(int linkID, char const* reply, int replySize, const char* mimeType, int maxAge) {
sschocke 3:4ef7db7a95a7 214 sprintf(response, "HTTP/1.1 200 OK\r\nContent-Type:%s\r\nContent-Length: %d\r\nCache-Control:max-age=%d\r\n\r\n", mimeType, replySize, maxAge);
sschocke 0:56c72c87d2f5 215 sendResponse(linkID, strlen(response));
sschocke 2:6079554681d6 216 char const* sendPtr = reply;
sschocke 2:6079554681d6 217 int bytesSent = 0;
sschocke 2:6079554681d6 218 while( bytesSent < replySize ) {
sschocke 2:6079554681d6 219 int bytesToSend = replySize - bytesSent;
sschocke 2:6079554681d6 220 if( bytesToSend > sizeof(response) ) {
sschocke 2:6079554681d6 221 bytesToSend = sizeof(response);
sschocke 2:6079554681d6 222 }
sschocke 2:6079554681d6 223
sschocke 2:6079554681d6 224 memcpy(response, sendPtr, bytesToSend);
sschocke 2:6079554681d6 225 sendResponse(linkID, bytesToSend);
sschocke 2:6079554681d6 226 sendPtr += bytesToSend;
sschocke 2:6079554681d6 227 bytesSent += bytesToSend;
sschocke 2:6079554681d6 228 }
sschocke 0:56c72c87d2f5 229 }
sschocke 0:56c72c87d2f5 230
sschocke 1:1c4333ce7448 231 void ESP8266_WebServer::SendFile(int linkID, FileHandle* file, const char* mimeType) {
sschocke 3:4ef7db7a95a7 232 SendFile(linkID, file, mimeType, 86400);
sschocke 3:4ef7db7a95a7 233 }
sschocke 3:4ef7db7a95a7 234 void ESP8266_WebServer::SendFile(int linkID, FileHandle* file, const char* mimeType, int maxAge) {
sschocke 3:4ef7db7a95a7 235 sprintf(response, "HTTP/1.1 200 OK\r\nContent-Type:%s\r\nContent-Length: %d\r\nCache-Control:max-age=%d\r\n\r\n", mimeType, file->flen(), maxAge);
sschocke 1:1c4333ce7448 236 sendResponse(linkID, strlen(response));
sschocke 1:1c4333ce7448 237 int numBytes = file->read(response, sizeof(response));
sschocke 1:1c4333ce7448 238 while( numBytes > 0) {
sschocke 1:1c4333ce7448 239 sendResponse(linkID, numBytes);
sschocke 1:1c4333ce7448 240 numBytes = file->read(response, sizeof(response));
sschocke 1:1c4333ce7448 241 }
sschocke 1:1c4333ce7448 242 }
sschocke 1:1c4333ce7448 243
sschocke 5:48b7fd921bef 244 std::string ESP8266_WebServer::GetStationMAC(void) {
sschocke 5:48b7fd921bef 245 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 246 readBuffer();
sschocke 5:48b7fd921bef 247 serial->printf("AT+CIPSTAMAC?\r\n");
sschocke 5:48b7fd921bef 248 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 249 wait_ms(10);
sschocke 5:48b7fd921bef 250 }
sschocke 5:48b7fd921bef 251 readBuffer();
sschocke 5:48b7fd921bef 252 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 253 debugSerial->printf("Station MAC Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 254 }
sschocke 5:48b7fd921bef 255
sschocke 5:48b7fd921bef 256 std::string mac = std::string(reply);
sschocke 5:48b7fd921bef 257 mac = mac.substr(1, 17);
sschocke 5:48b7fd921bef 258
sschocke 5:48b7fd921bef 259 return mac;
sschocke 5:48b7fd921bef 260 }
sschocke 5:48b7fd921bef 261 std::string ESP8266_WebServer::GetAPMAC(void) {
sschocke 5:48b7fd921bef 262 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 263 readBuffer();
sschocke 5:48b7fd921bef 264 serial->printf("AT+CIPAPMAC?\r\n");
sschocke 5:48b7fd921bef 265 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 266 wait_ms(10);
sschocke 5:48b7fd921bef 267 }
sschocke 5:48b7fd921bef 268 readBuffer();
sschocke 5:48b7fd921bef 269 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 270 debugSerial->printf("SoftAP MAC Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 271 }
sschocke 5:48b7fd921bef 272
sschocke 5:48b7fd921bef 273 std::string mac = std::string(reply);
sschocke 5:48b7fd921bef 274 mac = mac.substr(1, 17);
sschocke 5:48b7fd921bef 275
sschocke 5:48b7fd921bef 276 return mac;
sschocke 5:48b7fd921bef 277 }
sschocke 5:48b7fd921bef 278 std::string ESP8266_WebServer::GetStationIP(void) {
sschocke 5:48b7fd921bef 279 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 280 readBuffer();
sschocke 5:48b7fd921bef 281 serial->printf("AT+CIPSTA?\r\n");
sschocke 5:48b7fd921bef 282 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 283 wait_ms(10);
sschocke 5:48b7fd921bef 284 }
sschocke 5:48b7fd921bef 285 readBuffer();
sschocke 5:48b7fd921bef 286 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 287 debugSerial->printf("Station IP Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 288 }
sschocke 5:48b7fd921bef 289
sschocke 5:48b7fd921bef 290 std::string ip = std::string(reply);
sschocke 5:48b7fd921bef 291 ip = ip.substr(1, ip.find('"', 1) - 1);
sschocke 5:48b7fd921bef 292
sschocke 5:48b7fd921bef 293 return ip;
sschocke 5:48b7fd921bef 294 }
sschocke 5:48b7fd921bef 295 std::string ESP8266_WebServer::GetAPIP(void) {
sschocke 5:48b7fd921bef 296 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 297 readBuffer();
sschocke 5:48b7fd921bef 298 serial->printf("AT+CIPAP?\r\n");
sschocke 5:48b7fd921bef 299 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 300 wait_ms(10);
sschocke 5:48b7fd921bef 301 }
sschocke 5:48b7fd921bef 302 readBuffer();
sschocke 5:48b7fd921bef 303 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 304 debugSerial->printf("SoftAP IP Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 305 }
sschocke 5:48b7fd921bef 306
sschocke 5:48b7fd921bef 307 std::string ip = std::string(reply);
sschocke 5:48b7fd921bef 308 ip = ip.substr(1, ip.find('"', 1) - 1);
sschocke 5:48b7fd921bef 309
sschocke 5:48b7fd921bef 310 return ip;
sschocke 5:48b7fd921bef 311 }
sschocke 5:48b7fd921bef 312 int ESP8266_WebServer::GetOperatingMode(void) {
sschocke 5:48b7fd921bef 313 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 314 readBuffer();
sschocke 5:48b7fd921bef 315 serial->printf("AT+CWMODE?\r\n");
sschocke 5:48b7fd921bef 316 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 317 wait_ms(10);
sschocke 5:48b7fd921bef 318 }
sschocke 5:48b7fd921bef 319 readBuffer();
sschocke 5:48b7fd921bef 320 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 321 debugSerial->printf("Operating Mode Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 322 }
sschocke 5:48b7fd921bef 323
sschocke 5:48b7fd921bef 324 return atoi(reply);
sschocke 5:48b7fd921bef 325 }
sschocke 5:48b7fd921bef 326 std::string ESP8266_WebServer::GetStationSSID(void) {
sschocke 5:48b7fd921bef 327 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 328 readBuffer();
sschocke 5:48b7fd921bef 329 serial->printf("AT+CWJAP?\r\n");
sschocke 5:48b7fd921bef 330 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 331 wait_ms(10);
sschocke 5:48b7fd921bef 332 }
sschocke 5:48b7fd921bef 333 readBuffer();
sschocke 5:48b7fd921bef 334 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 335 debugSerial->printf("Station SSID Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 336 }
sschocke 5:48b7fd921bef 337
sschocke 5:48b7fd921bef 338 std::string ssid = std::string(reply);
sschocke 5:48b7fd921bef 339 if( strstr(reply, "No AP\r\n") != NULL ) { return "(None)"; }
sschocke 5:48b7fd921bef 340 ssid = ssid.substr(1, ssid.find('"', 1) - 1);
sschocke 5:48b7fd921bef 341
sschocke 5:48b7fd921bef 342 return ssid;
sschocke 5:48b7fd921bef 343 }
sschocke 5:48b7fd921bef 344 std::list<std::string> ESP8266_WebServer::ListAvailableSSID(void) {
sschocke 5:48b7fd921bef 345 std::list<std::string> apList;
sschocke 5:48b7fd921bef 346
sschocke 5:48b7fd921bef 347 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 348 readBuffer();
sschocke 5:48b7fd921bef 349 serial->printf("AT+CWLAP\r\n");
sschocke 5:48b7fd921bef 350 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 351 wait_ms(10);
sschocke 5:48b7fd921bef 352 }
sschocke 5:48b7fd921bef 353 readBuffer();
sschocke 5:48b7fd921bef 354 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 355 debugSerial->printf("SSID List Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 356 }
sschocke 5:48b7fd921bef 357
sschocke 5:48b7fd921bef 358 char ssid[65];
sschocke 5:48b7fd921bef 359 char apmac[20];
sschocke 5:48b7fd921bef 360 int ecn;
sschocke 5:48b7fd921bef 361 int rssi;
sschocke 5:48b7fd921bef 362 int tokenLength;
sschocke 5:48b7fd921bef 363 char* token = strstr(reply, "(");
sschocke 5:48b7fd921bef 364 while( token != NULL ) {
sschocke 5:48b7fd921bef 365 int numMatched = sscanf(token, "(%d,\"%[^\"]\",%d,\"%[0123456789abcdef:]\",%*d)\r\n%n", &ecn, ssid, &rssi, apmac, &tokenLength);
sschocke 5:48b7fd921bef 366 if( numMatched < 4 ) {
sschocke 5:48b7fd921bef 367 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 368 debugSerial->printf("SSID List Token Error: NumMatched=%d, SSID=%s\r\n", numMatched, ssid);
sschocke 5:48b7fd921bef 369 }
sschocke 5:48b7fd921bef 370 return apList;
sschocke 5:48b7fd921bef 371 }
sschocke 5:48b7fd921bef 372 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 373 debugSerial->printf("SSID List Token : SSID=%s, ECN=%d, RSSI=%d, APMAC=%s, TokenLength=%d\r\n", ssid, ecn, rssi, apmac, tokenLength);
sschocke 5:48b7fd921bef 374 }
sschocke 5:48b7fd921bef 375 apList.push_back(std::string(ssid));
sschocke 5:48b7fd921bef 376 token += tokenLength;
sschocke 5:48b7fd921bef 377 token = strstr(token, "(");
sschocke 5:48b7fd921bef 378 }
sschocke 5:48b7fd921bef 379
sschocke 5:48b7fd921bef 380 return apList;
sschocke 5:48b7fd921bef 381 }
sschocke 5:48b7fd921bef 382 std::string ESP8266_WebServer::GetFirmwareVersion(void) {
sschocke 5:48b7fd921bef 383 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 384 readBuffer();
sschocke 5:48b7fd921bef 385 serial->printf("AT+GMR\r\n");
sschocke 5:48b7fd921bef 386 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 387 wait_ms(10);
sschocke 5:48b7fd921bef 388 }
sschocke 5:48b7fd921bef 389 readBuffer();
sschocke 5:48b7fd921bef 390 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 391 debugSerial->printf("Firmware Version Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 392 }
sschocke 5:48b7fd921bef 393
sschocke 5:48b7fd921bef 394 std::string ver = std::string(reply);
sschocke 5:48b7fd921bef 395 ver = ver.substr(0, ver.find('\r'));
sschocke 5:48b7fd921bef 396
sschocke 5:48b7fd921bef 397 return ver;
sschocke 5:48b7fd921bef 398 }
sschocke 5:48b7fd921bef 399
sschocke 5:48b7fd921bef 400 bool ESP8266_WebServer::SetOperatingMode(int mode) {
sschocke 5:48b7fd921bef 401 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 402 debugSerial->printf("Set Operating Mode to %s(%d)\r\n", opModes[mode], mode);
sschocke 5:48b7fd921bef 403 }
sschocke 5:48b7fd921bef 404 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 405 readBuffer();
sschocke 5:48b7fd921bef 406 serial->printf("AT+CWMODE=%d\r\n", mode);
sschocke 5:48b7fd921bef 407 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 408 wait_ms(10);
sschocke 5:48b7fd921bef 409 }
sschocke 5:48b7fd921bef 410 readBuffer();
sschocke 5:48b7fd921bef 411 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 412 debugSerial->printf("Set Operating Mode Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 413 }
sschocke 5:48b7fd921bef 414 if( strstr(reply, "\r\nOK\r\n") != NULL ) { return true; }
sschocke 5:48b7fd921bef 415 return false;
sschocke 5:48b7fd921bef 416 }
sschocke 5:48b7fd921bef 417
sschocke 5:48b7fd921bef 418 bool ESP8266_WebServer::SetStationSSID(std::string newAP, std::string password) {
sschocke 5:48b7fd921bef 419 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 420 debugSerial->printf("Set Station SSID to %s, Password=%s\r\n", newAP.c_str(), password.c_str());
sschocke 5:48b7fd921bef 421 }
sschocke 5:48b7fd921bef 422 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 423 readBuffer();
sschocke 5:48b7fd921bef 424 serial->printf("AT+CWJAP=\"%s\",\"%s\"\r\n", newAP.c_str(), password.c_str());
sschocke 5:48b7fd921bef 425 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 426 wait_ms(10);
sschocke 5:48b7fd921bef 427 }
sschocke 5:48b7fd921bef 428 readBuffer();
sschocke 5:48b7fd921bef 429 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 430 debugSerial->printf("Set Station SSID Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 431 }
sschocke 5:48b7fd921bef 432 if( strstr(reply, "\r\nOK\r\n") != NULL ) { return true; }
sschocke 5:48b7fd921bef 433
sschocke 5:48b7fd921bef 434 return false;
sschocke 5:48b7fd921bef 435 }
sschocke 5:48b7fd921bef 436
sschocke 0:56c72c87d2f5 437 ESP8266_WebRequest::ESP8266_WebRequest(const char* packet, Serial* debug) {
sschocke 0:56c72c87d2f5 438 int sz = strlen(packet);
sschocke 0:56c72c87d2f5 439 data = (char *)malloc(sz+1);
sschocke 0:56c72c87d2f5 440 memcpy(data, packet, sz+1);
sschocke 0:56c72c87d2f5 441 debugSerial = debug;
sschocke 0:56c72c87d2f5 442 }
sschocke 0:56c72c87d2f5 443
sschocke 0:56c72c87d2f5 444 void ESP8266_WebRequest::Read(void) {
sschocke 0:56c72c87d2f5 445 int bytesRecv, ipdLen, linkID;
sschocke 0:56c72c87d2f5 446 int numMatched = sscanf(data,"+IPD,%d,%d:%n", &linkID, &bytesRecv, &ipdLen);
sschocke 0:56c72c87d2f5 447 if( numMatched != 2 ) {
sschocke 0:56c72c87d2f5 448 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 449 debugSerial->printf("IPD ERROR : Matched %d, LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", numMatched, linkID, bytesRecv, ipdLen);
sschocke 0:56c72c87d2f5 450 }
sschocke 0:56c72c87d2f5 451 return;
sschocke 0:56c72c87d2f5 452 }
sschocke 0:56c72c87d2f5 453
sschocke 0:56c72c87d2f5 454 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 455 debugSerial->printf("IPD Data: LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", linkID, bytesRecv, ipdLen);
sschocke 0:56c72c87d2f5 456 }
sschocke 0:56c72c87d2f5 457 if( strstr(data, "HTTP") != NULL ) {
sschocke 0:56c72c87d2f5 458 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 459 debugSerial->printf("Got HTTP Request\r\n");
sschocke 0:56c72c87d2f5 460 }
sschocke 0:56c72c87d2f5 461 char* httpPacket = data + ipdLen;
sschocke 0:56c72c87d2f5 462 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 463 debugSerial->printf("HTTP Packet: %s\r\n", httpPacket);
sschocke 0:56c72c87d2f5 464 }
sschocke 0:56c72c87d2f5 465 int numMatched = sscanf(httpPacket, "%s %s HTTP/%*c.%*c", httpMethod, httpURI);
sschocke 0:56c72c87d2f5 466 if( numMatched != 2 ) {
sschocke 0:56c72c87d2f5 467 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 468 debugSerial->printf("HTTP ERROR : Matched %d, Method=%s, URI=%s\r\n", numMatched, httpMethod, httpURI);
sschocke 0:56c72c87d2f5 469 }
sschocke 0:56c72c87d2f5 470 return;
sschocke 0:56c72c87d2f5 471 }
sschocke 0:56c72c87d2f5 472
sschocke 0:56c72c87d2f5 473 LinkID = linkID;
sschocke 0:56c72c87d2f5 474 Method = httpMethod;
sschocke 0:56c72c87d2f5 475 URI = httpURI;
sschocke 0:56c72c87d2f5 476 int pos = URI.find('?');
sschocke 0:56c72c87d2f5 477 if(pos != string::npos ) {
sschocke 0:56c72c87d2f5 478 string params = URI.substr(pos+1);
sschocke 0:56c72c87d2f5 479 URI = URI.substr(0,pos);
sschocke 0:56c72c87d2f5 480 pos = params.find('=');
sschocke 0:56c72c87d2f5 481 while( pos != string::npos ) {
sschocke 0:56c72c87d2f5 482 string name = params.substr(0,pos);
sschocke 0:56c72c87d2f5 483 string value = params.substr(pos+1);
sschocke 0:56c72c87d2f5 484 pos = params.find('&');
sschocke 0:56c72c87d2f5 485 if( pos == string::npos )
sschocke 0:56c72c87d2f5 486 {
sschocke 0:56c72c87d2f5 487 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 488 debugSerial->printf("HTTP GET Parameter %s=%s\r\n", name.c_str(), value.c_str());
sschocke 0:56c72c87d2f5 489 }
sschocke 0:56c72c87d2f5 490 Parameters[name] = value;
sschocke 0:56c72c87d2f5 491 break;
sschocke 0:56c72c87d2f5 492 }
sschocke 0:56c72c87d2f5 493
sschocke 0:56c72c87d2f5 494 value = value.substr(0,value.find('&'));
sschocke 0:56c72c87d2f5 495 params = params.substr(pos+1);
sschocke 0:56c72c87d2f5 496 pos = params.find('=');
sschocke 0:56c72c87d2f5 497 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 498 debugSerial->printf("HTTP GET Parameter %s=%s\r\n", name.c_str(), value.c_str());
sschocke 0:56c72c87d2f5 499 }
sschocke 0:56c72c87d2f5 500 Parameters[name] = value;
sschocke 0:56c72c87d2f5 501 }
sschocke 0:56c72c87d2f5 502 }
sschocke 0:56c72c87d2f5 503 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 504 debugSerial->printf("HTTP %s %s\r\n", httpMethod, httpURI);
sschocke 0:56c72c87d2f5 505 }
sschocke 0:56c72c87d2f5 506 }
sschocke 0:56c72c87d2f5 507 }
sschocke 0:56c72c87d2f5 508
sschocke 0:56c72c87d2f5 509 ESP8266_WebRequest::~ESP8266_WebRequest()
sschocke 0:56c72c87d2f5 510 {
sschocke 0:56c72c87d2f5 511 free(data);
sschocke 0:56c72c87d2f5 512 }