WiFi WebServer for ESP8266

Fork of ESP8266_WebServer by Sebastian Schocke

Committer:
sschocke
Date:
Sun Jan 04 12:11:33 2015 +0000
Revision:
6:34d93ea4d519
Parent:
4:759de84e790b
Parent:
5:48b7fd921bef
Child:
7:f6172ba3e807
Merge WiFi parameters query and setting API with SendStream changes

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
leet 4:759de84e790b 244 int ESP8266_WebServer::SendStream(int linkID, char const* reply, int StreamSize, int WindowSize, const char* mimeType, int maxAge) {
leet 4:759de84e790b 245 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, StreamSize, maxAge);
leet 4:759de84e790b 246 sendResponse(linkID, strlen(response));
leet 4:759de84e790b 247 return SendStream(linkID, reply, WindowSize);
leet 4:759de84e790b 248 }
leet 4:759de84e790b 249
sschocke 5:48b7fd921bef 250 std::string ESP8266_WebServer::GetStationMAC(void) {
sschocke 5:48b7fd921bef 251 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 252 readBuffer();
sschocke 5:48b7fd921bef 253 serial->printf("AT+CIPSTAMAC?\r\n");
sschocke 5:48b7fd921bef 254 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 255 wait_ms(10);
sschocke 5:48b7fd921bef 256 }
sschocke 5:48b7fd921bef 257 readBuffer();
sschocke 5:48b7fd921bef 258 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 259 debugSerial->printf("Station MAC Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 260 }
sschocke 5:48b7fd921bef 261
sschocke 5:48b7fd921bef 262 std::string mac = std::string(reply);
sschocke 5:48b7fd921bef 263 mac = mac.substr(1, 17);
sschocke 5:48b7fd921bef 264
sschocke 5:48b7fd921bef 265 return mac;
sschocke 5:48b7fd921bef 266 }
sschocke 5:48b7fd921bef 267 std::string ESP8266_WebServer::GetAPMAC(void) {
sschocke 5:48b7fd921bef 268 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 269 readBuffer();
sschocke 5:48b7fd921bef 270 serial->printf("AT+CIPAPMAC?\r\n");
sschocke 5:48b7fd921bef 271 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 272 wait_ms(10);
sschocke 5:48b7fd921bef 273 }
sschocke 5:48b7fd921bef 274 readBuffer();
sschocke 5:48b7fd921bef 275 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 276 debugSerial->printf("SoftAP MAC Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 277 }
sschocke 5:48b7fd921bef 278
sschocke 5:48b7fd921bef 279 std::string mac = std::string(reply);
sschocke 5:48b7fd921bef 280 mac = mac.substr(1, 17);
sschocke 5:48b7fd921bef 281
sschocke 5:48b7fd921bef 282 return mac;
sschocke 5:48b7fd921bef 283 }
sschocke 5:48b7fd921bef 284 std::string ESP8266_WebServer::GetStationIP(void) {
sschocke 5:48b7fd921bef 285 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 286 readBuffer();
sschocke 5:48b7fd921bef 287 serial->printf("AT+CIPSTA?\r\n");
sschocke 5:48b7fd921bef 288 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 289 wait_ms(10);
sschocke 5:48b7fd921bef 290 }
sschocke 5:48b7fd921bef 291 readBuffer();
sschocke 5:48b7fd921bef 292 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 293 debugSerial->printf("Station IP Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 294 }
sschocke 5:48b7fd921bef 295
sschocke 5:48b7fd921bef 296 std::string ip = std::string(reply);
sschocke 5:48b7fd921bef 297 ip = ip.substr(1, ip.find('"', 1) - 1);
sschocke 5:48b7fd921bef 298
sschocke 5:48b7fd921bef 299 return ip;
sschocke 5:48b7fd921bef 300 }
sschocke 5:48b7fd921bef 301 std::string ESP8266_WebServer::GetAPIP(void) {
sschocke 5:48b7fd921bef 302 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 303 readBuffer();
sschocke 5:48b7fd921bef 304 serial->printf("AT+CIPAP?\r\n");
sschocke 5:48b7fd921bef 305 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 306 wait_ms(10);
sschocke 5:48b7fd921bef 307 }
sschocke 5:48b7fd921bef 308 readBuffer();
sschocke 5:48b7fd921bef 309 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 310 debugSerial->printf("SoftAP IP Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 311 }
sschocke 5:48b7fd921bef 312
sschocke 5:48b7fd921bef 313 std::string ip = std::string(reply);
sschocke 5:48b7fd921bef 314 ip = ip.substr(1, ip.find('"', 1) - 1);
sschocke 5:48b7fd921bef 315
sschocke 5:48b7fd921bef 316 return ip;
sschocke 5:48b7fd921bef 317 }
sschocke 5:48b7fd921bef 318 int ESP8266_WebServer::GetOperatingMode(void) {
sschocke 5:48b7fd921bef 319 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 320 readBuffer();
sschocke 5:48b7fd921bef 321 serial->printf("AT+CWMODE?\r\n");
sschocke 5:48b7fd921bef 322 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 323 wait_ms(10);
sschocke 5:48b7fd921bef 324 }
sschocke 5:48b7fd921bef 325 readBuffer();
sschocke 5:48b7fd921bef 326 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 327 debugSerial->printf("Operating Mode Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 328 }
sschocke 5:48b7fd921bef 329
sschocke 5:48b7fd921bef 330 return atoi(reply);
sschocke 5:48b7fd921bef 331 }
sschocke 5:48b7fd921bef 332 std::string ESP8266_WebServer::GetStationSSID(void) {
sschocke 5:48b7fd921bef 333 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 334 readBuffer();
sschocke 5:48b7fd921bef 335 serial->printf("AT+CWJAP?\r\n");
sschocke 5:48b7fd921bef 336 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 337 wait_ms(10);
sschocke 5:48b7fd921bef 338 }
sschocke 5:48b7fd921bef 339 readBuffer();
sschocke 5:48b7fd921bef 340 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 341 debugSerial->printf("Station SSID Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 342 }
sschocke 5:48b7fd921bef 343
sschocke 5:48b7fd921bef 344 std::string ssid = std::string(reply);
sschocke 5:48b7fd921bef 345 if( strstr(reply, "No AP\r\n") != NULL ) { return "(None)"; }
sschocke 5:48b7fd921bef 346 ssid = ssid.substr(1, ssid.find('"', 1) - 1);
sschocke 5:48b7fd921bef 347
sschocke 5:48b7fd921bef 348 return ssid;
sschocke 5:48b7fd921bef 349 }
sschocke 5:48b7fd921bef 350 std::list<std::string> ESP8266_WebServer::ListAvailableSSID(void) {
sschocke 5:48b7fd921bef 351 std::list<std::string> apList;
sschocke 5:48b7fd921bef 352
sschocke 5:48b7fd921bef 353 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 354 readBuffer();
sschocke 5:48b7fd921bef 355 serial->printf("AT+CWLAP\r\n");
sschocke 5:48b7fd921bef 356 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 357 wait_ms(10);
sschocke 5:48b7fd921bef 358 }
sschocke 5:48b7fd921bef 359 readBuffer();
sschocke 5:48b7fd921bef 360 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 361 debugSerial->printf("SSID List Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 362 }
sschocke 5:48b7fd921bef 363
sschocke 5:48b7fd921bef 364 char ssid[65];
sschocke 5:48b7fd921bef 365 char apmac[20];
sschocke 5:48b7fd921bef 366 int ecn;
sschocke 5:48b7fd921bef 367 int rssi;
sschocke 5:48b7fd921bef 368 int tokenLength;
sschocke 5:48b7fd921bef 369 char* token = strstr(reply, "(");
sschocke 5:48b7fd921bef 370 while( token != NULL ) {
sschocke 5:48b7fd921bef 371 int numMatched = sscanf(token, "(%d,\"%[^\"]\",%d,\"%[0123456789abcdef:]\",%*d)\r\n%n", &ecn, ssid, &rssi, apmac, &tokenLength);
sschocke 5:48b7fd921bef 372 if( numMatched < 4 ) {
sschocke 5:48b7fd921bef 373 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 374 debugSerial->printf("SSID List Token Error: NumMatched=%d, SSID=%s\r\n", numMatched, ssid);
sschocke 5:48b7fd921bef 375 }
sschocke 5:48b7fd921bef 376 return apList;
sschocke 5:48b7fd921bef 377 }
sschocke 5:48b7fd921bef 378 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 379 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 380 }
sschocke 5:48b7fd921bef 381 apList.push_back(std::string(ssid));
sschocke 5:48b7fd921bef 382 token += tokenLength;
sschocke 5:48b7fd921bef 383 token = strstr(token, "(");
sschocke 5:48b7fd921bef 384 }
sschocke 5:48b7fd921bef 385
sschocke 5:48b7fd921bef 386 return apList;
sschocke 5:48b7fd921bef 387 }
sschocke 5:48b7fd921bef 388 std::string ESP8266_WebServer::GetFirmwareVersion(void) {
sschocke 5:48b7fd921bef 389 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 390 readBuffer();
sschocke 5:48b7fd921bef 391 serial->printf("AT+GMR\r\n");
sschocke 5:48b7fd921bef 392 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 393 wait_ms(10);
sschocke 5:48b7fd921bef 394 }
sschocke 5:48b7fd921bef 395 readBuffer();
sschocke 5:48b7fd921bef 396 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 397 debugSerial->printf("Firmware Version Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 398 }
sschocke 5:48b7fd921bef 399
sschocke 5:48b7fd921bef 400 std::string ver = std::string(reply);
sschocke 5:48b7fd921bef 401 ver = ver.substr(0, ver.find('\r'));
sschocke 5:48b7fd921bef 402
sschocke 5:48b7fd921bef 403 return ver;
sschocke 5:48b7fd921bef 404 }
sschocke 5:48b7fd921bef 405
leet 4:759de84e790b 406 int ESP8266_WebServer::SendStream(int linkID, char const* reply, int WindowSize) {
leet 4:759de84e790b 407 char const* sendPtr = reply;
leet 4:759de84e790b 408 int bytesSent = 0;
leet 4:759de84e790b 409 while( bytesSent < WindowSize ) {
leet 4:759de84e790b 410 int bytesToSend = WindowSize - bytesSent;
leet 4:759de84e790b 411 if( bytesToSend > sizeof(response) ) {
leet 4:759de84e790b 412 bytesToSend = sizeof(response);
leet 4:759de84e790b 413 }
leet 4:759de84e790b 414
leet 4:759de84e790b 415 memcpy(response, sendPtr, bytesToSend);
leet 4:759de84e790b 416 sendResponse(linkID, bytesToSend);
leet 4:759de84e790b 417 sendPtr += bytesToSend;
leet 4:759de84e790b 418 bytesSent += bytesToSend;
leet 4:759de84e790b 419 }
leet 4:759de84e790b 420
leet 4:759de84e790b 421 return bytesSent;
leet 4:759de84e790b 422 }
leet 4:759de84e790b 423
sschocke 5:48b7fd921bef 424 bool ESP8266_WebServer::SetOperatingMode(int mode) {
sschocke 5:48b7fd921bef 425 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 426 debugSerial->printf("Set Operating Mode to %s(%d)\r\n", opModes[mode], mode);
sschocke 5:48b7fd921bef 427 }
sschocke 6:34d93ea4d519 428
sschocke 5:48b7fd921bef 429 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 430 readBuffer();
sschocke 5:48b7fd921bef 431 serial->printf("AT+CWMODE=%d\r\n", mode);
sschocke 5:48b7fd921bef 432 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 433 wait_ms(10);
sschocke 5:48b7fd921bef 434 }
sschocke 5:48b7fd921bef 435 readBuffer();
sschocke 5:48b7fd921bef 436 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 437 debugSerial->printf("Set Operating Mode Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 438 }
sschocke 5:48b7fd921bef 439 if( strstr(reply, "\r\nOK\r\n") != NULL ) { return true; }
sschocke 5:48b7fd921bef 440 return false;
sschocke 5:48b7fd921bef 441 }
sschocke 5:48b7fd921bef 442
sschocke 5:48b7fd921bef 443 bool ESP8266_WebServer::SetStationSSID(std::string newAP, std::string password) {
sschocke 5:48b7fd921bef 444 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 445 debugSerial->printf("Set Station SSID to %s, Password=%s\r\n", newAP.c_str(), password.c_str());
sschocke 5:48b7fd921bef 446 }
sschocke 5:48b7fd921bef 447 while( reqMode == true ) { wait_ms(1); }
sschocke 5:48b7fd921bef 448 readBuffer();
sschocke 5:48b7fd921bef 449 serial->printf("AT+CWJAP=\"%s\",\"%s\"\r\n", newAP.c_str(), password.c_str());
sschocke 5:48b7fd921bef 450 while( data_waiting() == 0 ) {
sschocke 5:48b7fd921bef 451 wait_ms(10);
sschocke 5:48b7fd921bef 452 }
sschocke 5:48b7fd921bef 453 readBuffer();
sschocke 5:48b7fd921bef 454 if( debugSerial != NULL ) {
sschocke 5:48b7fd921bef 455 debugSerial->printf("Set Station SSID Reply: %s\r\n", reply);
sschocke 5:48b7fd921bef 456 }
sschocke 5:48b7fd921bef 457 if( strstr(reply, "\r\nOK\r\n") != NULL ) { return true; }
sschocke 5:48b7fd921bef 458
sschocke 5:48b7fd921bef 459 return false;
sschocke 5:48b7fd921bef 460 }
sschocke 5:48b7fd921bef 461
sschocke 0:56c72c87d2f5 462 ESP8266_WebRequest::ESP8266_WebRequest(const char* packet, Serial* debug) {
sschocke 0:56c72c87d2f5 463 int sz = strlen(packet);
sschocke 0:56c72c87d2f5 464 data = (char *)malloc(sz+1);
sschocke 0:56c72c87d2f5 465 memcpy(data, packet, sz+1);
sschocke 0:56c72c87d2f5 466 debugSerial = debug;
sschocke 0:56c72c87d2f5 467 }
sschocke 0:56c72c87d2f5 468
sschocke 0:56c72c87d2f5 469 void ESP8266_WebRequest::Read(void) {
sschocke 0:56c72c87d2f5 470 int bytesRecv, ipdLen, linkID;
sschocke 0:56c72c87d2f5 471 int numMatched = sscanf(data,"+IPD,%d,%d:%n", &linkID, &bytesRecv, &ipdLen);
sschocke 0:56c72c87d2f5 472 if( numMatched != 2 ) {
sschocke 0:56c72c87d2f5 473 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 474 debugSerial->printf("IPD ERROR : Matched %d, LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", numMatched, linkID, bytesRecv, ipdLen);
sschocke 0:56c72c87d2f5 475 }
sschocke 0:56c72c87d2f5 476 return;
sschocke 0:56c72c87d2f5 477 }
sschocke 0:56c72c87d2f5 478
sschocke 0:56c72c87d2f5 479 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 480 debugSerial->printf("IPD Data: LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", linkID, bytesRecv, ipdLen);
sschocke 0:56c72c87d2f5 481 }
sschocke 0:56c72c87d2f5 482 if( strstr(data, "HTTP") != NULL ) {
sschocke 0:56c72c87d2f5 483 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 484 debugSerial->printf("Got HTTP Request\r\n");
sschocke 0:56c72c87d2f5 485 }
sschocke 0:56c72c87d2f5 486 char* httpPacket = data + ipdLen;
sschocke 0:56c72c87d2f5 487 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 488 debugSerial->printf("HTTP Packet: %s\r\n", httpPacket);
sschocke 0:56c72c87d2f5 489 }
sschocke 0:56c72c87d2f5 490 int numMatched = sscanf(httpPacket, "%s %s HTTP/%*c.%*c", httpMethod, httpURI);
sschocke 0:56c72c87d2f5 491 if( numMatched != 2 ) {
sschocke 0:56c72c87d2f5 492 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 493 debugSerial->printf("HTTP ERROR : Matched %d, Method=%s, URI=%s\r\n", numMatched, httpMethod, httpURI);
sschocke 0:56c72c87d2f5 494 }
sschocke 0:56c72c87d2f5 495 return;
sschocke 0:56c72c87d2f5 496 }
sschocke 0:56c72c87d2f5 497
sschocke 0:56c72c87d2f5 498 LinkID = linkID;
sschocke 0:56c72c87d2f5 499 Method = httpMethod;
sschocke 0:56c72c87d2f5 500 URI = httpURI;
sschocke 0:56c72c87d2f5 501 int pos = URI.find('?');
sschocke 0:56c72c87d2f5 502 if(pos != string::npos ) {
sschocke 0:56c72c87d2f5 503 string params = URI.substr(pos+1);
sschocke 0:56c72c87d2f5 504 URI = URI.substr(0,pos);
sschocke 0:56c72c87d2f5 505 pos = params.find('=');
sschocke 0:56c72c87d2f5 506 while( pos != string::npos ) {
sschocke 0:56c72c87d2f5 507 string name = params.substr(0,pos);
sschocke 0:56c72c87d2f5 508 string value = params.substr(pos+1);
sschocke 0:56c72c87d2f5 509 pos = params.find('&');
sschocke 0:56c72c87d2f5 510 if( pos == string::npos )
sschocke 0:56c72c87d2f5 511 {
sschocke 0:56c72c87d2f5 512 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 513 debugSerial->printf("HTTP GET Parameter %s=%s\r\n", name.c_str(), value.c_str());
sschocke 0:56c72c87d2f5 514 }
sschocke 0:56c72c87d2f5 515 Parameters[name] = value;
sschocke 0:56c72c87d2f5 516 break;
sschocke 0:56c72c87d2f5 517 }
sschocke 0:56c72c87d2f5 518
sschocke 0:56c72c87d2f5 519 value = value.substr(0,value.find('&'));
sschocke 0:56c72c87d2f5 520 params = params.substr(pos+1);
sschocke 0:56c72c87d2f5 521 pos = params.find('=');
sschocke 0:56c72c87d2f5 522 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 523 debugSerial->printf("HTTP GET Parameter %s=%s\r\n", name.c_str(), value.c_str());
sschocke 0:56c72c87d2f5 524 }
sschocke 0:56c72c87d2f5 525 Parameters[name] = value;
sschocke 0:56c72c87d2f5 526 }
sschocke 0:56c72c87d2f5 527 }
sschocke 0:56c72c87d2f5 528 if( debugSerial != NULL ) {
sschocke 0:56c72c87d2f5 529 debugSerial->printf("HTTP %s %s\r\n", httpMethod, httpURI);
sschocke 0:56c72c87d2f5 530 }
sschocke 0:56c72c87d2f5 531 }
sschocke 0:56c72c87d2f5 532 }
sschocke 0:56c72c87d2f5 533
sschocke 0:56c72c87d2f5 534 ESP8266_WebRequest::~ESP8266_WebRequest()
sschocke 0:56c72c87d2f5 535 {
sschocke 0:56c72c87d2f5 536 free(data);
sschocke 0:56c72c87d2f5 537 }