WiFi WebServer for ESP8266
Fork of ESP8266_WebServer by
ESP8266_WebServer.cpp@7:f6172ba3e807, 2015-01-05 (annotated)
- Committer:
- sschocke
- Date:
- Mon Jan 05 19:22:25 2015 +0000
- Revision:
- 7:f6172ba3e807
- Parent:
- 6:34d93ea4d519
- Child:
- 8:5573f6f70a82
Fixed bug with initial startup sequence not always starting up with char echo disabled
Who changed what in which revision?
User | Revision | Line number | New 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 | 7:f6172ba3e807 | 97 | void ESP8266_WebServer::ResetModule(void) { |
sschocke | 0:56c72c87d2f5 | 98 | readBuffer(); |
sschocke | 0:56c72c87d2f5 | 99 | serial->printf("ATE0\r\n"); |
sschocke | 0:56c72c87d2f5 | 100 | while( data_waiting() == 0 ) { |
sschocke | 0:56c72c87d2f5 | 101 | wait_ms(10); |
sschocke | 0:56c72c87d2f5 | 102 | } |
sschocke | 0:56c72c87d2f5 | 103 | readBuffer(); |
sschocke | 0:56c72c87d2f5 | 104 | |
sschocke | 7:f6172ba3e807 | 105 | readBuffer(); |
sschocke | 7:f6172ba3e807 | 106 | serial->printf("AT+RST\r\n"); |
sschocke | 7:f6172ba3e807 | 107 | wait_ms(1000); |
sschocke | 7:f6172ba3e807 | 108 | while( string_waiting("\r\nready\r\n") == 0 ) { |
sschocke | 7:f6172ba3e807 | 109 | wait_ms(100); |
sschocke | 0:56c72c87d2f5 | 110 | } |
sschocke | 7:f6172ba3e807 | 111 | |
sschocke | 7:f6172ba3e807 | 112 | readBuffer(); |
sschocke | 7:f6172ba3e807 | 113 | serial->printf("ATE0\r\n"); |
sschocke | 0:56c72c87d2f5 | 114 | while( data_waiting() == 0 ) { |
sschocke | 0:56c72c87d2f5 | 115 | wait_ms(10); |
sschocke | 0:56c72c87d2f5 | 116 | } |
sschocke | 0:56c72c87d2f5 | 117 | readBuffer(); |
sschocke | 7:f6172ba3e807 | 118 | } |
sschocke | 0:56c72c87d2f5 | 119 | |
sschocke | 7:f6172ba3e807 | 120 | void ESP8266_WebServer::Initialize(void) { |
sschocke | 0:56c72c87d2f5 | 121 | if( debugSerial != NULL ) { |
sschocke | 0:56c72c87d2f5 | 122 | debugSerial->printf("Done\r\nAccept Multiple connections..."); |
sschocke | 0:56c72c87d2f5 | 123 | } |
sschocke | 0:56c72c87d2f5 | 124 | serial->printf("AT+CIPMUX=1\r\n"); |
sschocke | 0:56c72c87d2f5 | 125 | while( data_waiting() == 0 ) { |
sschocke | 0:56c72c87d2f5 | 126 | wait_ms(10); |
sschocke | 0:56c72c87d2f5 | 127 | } |
sschocke | 0:56c72c87d2f5 | 128 | readBuffer(); |
sschocke | 0:56c72c87d2f5 | 129 | |
sschocke | 0:56c72c87d2f5 | 130 | if( debugSerial != NULL ) { |
sschocke | 0:56c72c87d2f5 | 131 | debugSerial->printf("Done\r\nStarting Web Server..."); |
sschocke | 0:56c72c87d2f5 | 132 | } |
sschocke | 0:56c72c87d2f5 | 133 | |
sschocke | 0:56c72c87d2f5 | 134 | serial->printf("AT+CIPSERVER=1,80\r\n"); |
sschocke | 0:56c72c87d2f5 | 135 | while( data_waiting() == 0 ) { |
sschocke | 0:56c72c87d2f5 | 136 | wait_ms(10); |
sschocke | 0:56c72c87d2f5 | 137 | } |
sschocke | 0:56c72c87d2f5 | 138 | readBuffer(); |
sschocke | 0:56c72c87d2f5 | 139 | } |
sschocke | 0:56c72c87d2f5 | 140 | |
sschocke | 0:56c72c87d2f5 | 141 | void ESP8266_WebServer::queueRequest(void) { |
sschocke | 0:56c72c87d2f5 | 142 | if( strstr(reqBuffer, "HTTP") != NULL ) { |
sschocke | 0:56c72c87d2f5 | 143 | ESP8266_WebRequest* request = new ESP8266_WebRequest(reqBuffer, debugSerial); |
sschocke | 0:56c72c87d2f5 | 144 | |
sschocke | 0:56c72c87d2f5 | 145 | incoming.push(request); |
sschocke | 0:56c72c87d2f5 | 146 | } |
sschocke | 0:56c72c87d2f5 | 147 | } |
sschocke | 0:56c72c87d2f5 | 148 | |
sschocke | 0:56c72c87d2f5 | 149 | ESP8266_WebRequest* ESP8266_WebServer::GetRequest(void) |
sschocke | 0:56c72c87d2f5 | 150 | { |
sschocke | 0:56c72c87d2f5 | 151 | if( incoming.empty() == false ) { |
sschocke | 0:56c72c87d2f5 | 152 | ESP8266_WebRequest* temp = incoming.front(); |
sschocke | 0:56c72c87d2f5 | 153 | incoming.pop(); |
sschocke | 0:56c72c87d2f5 | 154 | temp->Read(); |
sschocke | 0:56c72c87d2f5 | 155 | |
sschocke | 0:56c72c87d2f5 | 156 | return temp; |
sschocke | 0:56c72c87d2f5 | 157 | } |
sschocke | 0:56c72c87d2f5 | 158 | |
sschocke | 0:56c72c87d2f5 | 159 | return NULL; |
sschocke | 0:56c72c87d2f5 | 160 | } |
sschocke | 0:56c72c87d2f5 | 161 | |
sschocke | 0:56c72c87d2f5 | 162 | void ESP8266_WebServer::sendResponse(int linkID, int numBytes) { |
sschocke | 0:56c72c87d2f5 | 163 | bool sent = false; |
sschocke | 0:56c72c87d2f5 | 164 | |
sschocke | 0:56c72c87d2f5 | 165 | readBuffer(); |
sschocke | 0:56c72c87d2f5 | 166 | if( debugSerial != NULL ) { |
sschocke | 0:56c72c87d2f5 | 167 | debugSerial->printf("HTTP Reply Packet(%d bytes)\r\n", numBytes); |
sschocke | 0:56c72c87d2f5 | 168 | } |
sschocke | 0:56c72c87d2f5 | 169 | while( sent == false ) { |
sschocke | 0:56c72c87d2f5 | 170 | while( reqMode == true ) { wait_ms(1); } |
sschocke | 0:56c72c87d2f5 | 171 | serial->printf("AT+CIPSEND=%d,%d\r\n", linkID, numBytes); |
sschocke | 0:56c72c87d2f5 | 172 | wait_ms(100); |
sschocke | 0:56c72c87d2f5 | 173 | if( (string_waiting(">") == 1) ) { |
sschocke | 0:56c72c87d2f5 | 174 | char* error = strstr(buffer, "ERROR\r\n"); |
sschocke | 0:56c72c87d2f5 | 175 | if( error != NULL ) { continue; } |
sschocke | 0:56c72c87d2f5 | 176 | for( int i=0; i<numBytes; i++ ) { |
sschocke | 0:56c72c87d2f5 | 177 | serial->putc(response[i]); |
sschocke | 0:56c72c87d2f5 | 178 | } |
sschocke | 0:56c72c87d2f5 | 179 | while( string_waiting("\r\nSEND OK\r\n") == 0 ) { |
sschocke | 0:56c72c87d2f5 | 180 | wait_ms(10); |
sschocke | 0:56c72c87d2f5 | 181 | } |
sschocke | 0:56c72c87d2f5 | 182 | error = strstr(buffer, "ERROR\r\n"); |
sschocke | 0:56c72c87d2f5 | 183 | if( error != NULL ) { continue; } |
sschocke | 0:56c72c87d2f5 | 184 | sent = true; |
sschocke | 0:56c72c87d2f5 | 185 | } |
sschocke | 0:56c72c87d2f5 | 186 | } |
sschocke | 0:56c72c87d2f5 | 187 | readBuffer(); |
sschocke | 0:56c72c87d2f5 | 188 | } |
sschocke | 0:56c72c87d2f5 | 189 | |
sschocke | 0:56c72c87d2f5 | 190 | void ESP8266_WebServer::SendError(int linkID, std::string error) { |
sschocke | 0:56c72c87d2f5 | 191 | SendError(linkID, error.c_str()); |
sschocke | 0:56c72c87d2f5 | 192 | } |
sschocke | 0:56c72c87d2f5 | 193 | void ESP8266_WebServer::SendError(int linkID, const char* error) { |
sschocke | 0:56c72c87d2f5 | 194 | 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 | 195 | sendResponse(linkID, strlen(response)); |
sschocke | 0:56c72c87d2f5 | 196 | } |
sschocke | 0:56c72c87d2f5 | 197 | void ESP8266_WebServer::Send404Error(int linkID) { |
sschocke | 0:56c72c87d2f5 | 198 | SendError(linkID, "404 Not Found"); |
sschocke | 0:56c72c87d2f5 | 199 | } |
sschocke | 0:56c72c87d2f5 | 200 | |
sschocke | 0:56c72c87d2f5 | 201 | void ESP8266_WebServer::SendReply(int linkID, std::string reply, const char* mimeType) { |
sschocke | 3:4ef7db7a95a7 | 202 | SendReply(linkID, reply.c_str(), reply.length(), mimeType, 60); |
sschocke | 0:56c72c87d2f5 | 203 | } |
sschocke | 5:48b7fd921bef | 204 | void ESP8266_WebServer::SendReply(int linkID, std::string reply, const char* mimeType, int maxAge) { |
sschocke | 5:48b7fd921bef | 205 | SendReply(linkID, reply.c_str(), reply.length(), mimeType, maxAge); |
sschocke | 5:48b7fd921bef | 206 | } |
sschocke | 0:56c72c87d2f5 | 207 | |
sschocke | 2:6079554681d6 | 208 | void ESP8266_WebServer::SendReply(int linkID, char const* reply, int replySize, const char* mimeType) { |
sschocke | 3:4ef7db7a95a7 | 209 | SendReply(linkID, reply, replySize, mimeType, 600); |
sschocke | 3:4ef7db7a95a7 | 210 | } |
sschocke | 3:4ef7db7a95a7 | 211 | void ESP8266_WebServer::SendReply(int linkID, char const* reply, const char* mimeType, int maxAge) { |
sschocke | 3:4ef7db7a95a7 | 212 | SendReply(linkID, reply, strlen(reply), mimeType, maxAge); |
sschocke | 3:4ef7db7a95a7 | 213 | } |
sschocke | 3:4ef7db7a95a7 | 214 | void ESP8266_WebServer::SendReply(int linkID, char const* reply, int replySize, const char* mimeType, int maxAge) { |
sschocke | 3:4ef7db7a95a7 | 215 | 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 | 216 | sendResponse(linkID, strlen(response)); |
sschocke | 2:6079554681d6 | 217 | char const* sendPtr = reply; |
sschocke | 2:6079554681d6 | 218 | int bytesSent = 0; |
sschocke | 2:6079554681d6 | 219 | while( bytesSent < replySize ) { |
sschocke | 2:6079554681d6 | 220 | int bytesToSend = replySize - bytesSent; |
sschocke | 2:6079554681d6 | 221 | if( bytesToSend > sizeof(response) ) { |
sschocke | 2:6079554681d6 | 222 | bytesToSend = sizeof(response); |
sschocke | 2:6079554681d6 | 223 | } |
sschocke | 2:6079554681d6 | 224 | |
sschocke | 2:6079554681d6 | 225 | memcpy(response, sendPtr, bytesToSend); |
sschocke | 2:6079554681d6 | 226 | sendResponse(linkID, bytesToSend); |
sschocke | 2:6079554681d6 | 227 | sendPtr += bytesToSend; |
sschocke | 2:6079554681d6 | 228 | bytesSent += bytesToSend; |
sschocke | 2:6079554681d6 | 229 | } |
sschocke | 0:56c72c87d2f5 | 230 | } |
sschocke | 0:56c72c87d2f5 | 231 | |
sschocke | 1:1c4333ce7448 | 232 | void ESP8266_WebServer::SendFile(int linkID, FileHandle* file, const char* mimeType) { |
sschocke | 3:4ef7db7a95a7 | 233 | SendFile(linkID, file, mimeType, 86400); |
sschocke | 3:4ef7db7a95a7 | 234 | } |
sschocke | 3:4ef7db7a95a7 | 235 | void ESP8266_WebServer::SendFile(int linkID, FileHandle* file, const char* mimeType, int maxAge) { |
sschocke | 3:4ef7db7a95a7 | 236 | 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 | 237 | sendResponse(linkID, strlen(response)); |
sschocke | 1:1c4333ce7448 | 238 | int numBytes = file->read(response, sizeof(response)); |
sschocke | 1:1c4333ce7448 | 239 | while( numBytes > 0) { |
sschocke | 1:1c4333ce7448 | 240 | sendResponse(linkID, numBytes); |
sschocke | 1:1c4333ce7448 | 241 | numBytes = file->read(response, sizeof(response)); |
sschocke | 1:1c4333ce7448 | 242 | } |
sschocke | 1:1c4333ce7448 | 243 | } |
sschocke | 1:1c4333ce7448 | 244 | |
leet | 4:759de84e790b | 245 | int ESP8266_WebServer::SendStream(int linkID, char const* reply, int StreamSize, int WindowSize, const char* mimeType, int maxAge) { |
leet | 4:759de84e790b | 246 | 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 | 247 | sendResponse(linkID, strlen(response)); |
leet | 4:759de84e790b | 248 | return SendStream(linkID, reply, WindowSize); |
leet | 4:759de84e790b | 249 | } |
leet | 4:759de84e790b | 250 | |
sschocke | 5:48b7fd921bef | 251 | std::string ESP8266_WebServer::GetStationMAC(void) { |
sschocke | 5:48b7fd921bef | 252 | while( reqMode == true ) { wait_ms(1); } |
sschocke | 5:48b7fd921bef | 253 | readBuffer(); |
sschocke | 5:48b7fd921bef | 254 | serial->printf("AT+CIPSTAMAC?\r\n"); |
sschocke | 5:48b7fd921bef | 255 | while( data_waiting() == 0 ) { |
sschocke | 5:48b7fd921bef | 256 | wait_ms(10); |
sschocke | 5:48b7fd921bef | 257 | } |
sschocke | 5:48b7fd921bef | 258 | readBuffer(); |
sschocke | 5:48b7fd921bef | 259 | if( debugSerial != NULL ) { |
sschocke | 5:48b7fd921bef | 260 | debugSerial->printf("Station MAC Reply: %s\r\n", reply); |
sschocke | 5:48b7fd921bef | 261 | } |
sschocke | 5:48b7fd921bef | 262 | |
sschocke | 5:48b7fd921bef | 263 | std::string mac = std::string(reply); |
sschocke | 5:48b7fd921bef | 264 | mac = mac.substr(1, 17); |
sschocke | 5:48b7fd921bef | 265 | |
sschocke | 5:48b7fd921bef | 266 | return mac; |
sschocke | 5:48b7fd921bef | 267 | } |
sschocke | 5:48b7fd921bef | 268 | std::string ESP8266_WebServer::GetAPMAC(void) { |
sschocke | 5:48b7fd921bef | 269 | while( reqMode == true ) { wait_ms(1); } |
sschocke | 5:48b7fd921bef | 270 | readBuffer(); |
sschocke | 5:48b7fd921bef | 271 | serial->printf("AT+CIPAPMAC?\r\n"); |
sschocke | 5:48b7fd921bef | 272 | while( data_waiting() == 0 ) { |
sschocke | 5:48b7fd921bef | 273 | wait_ms(10); |
sschocke | 5:48b7fd921bef | 274 | } |
sschocke | 5:48b7fd921bef | 275 | readBuffer(); |
sschocke | 5:48b7fd921bef | 276 | if( debugSerial != NULL ) { |
sschocke | 5:48b7fd921bef | 277 | debugSerial->printf("SoftAP MAC Reply: %s\r\n", reply); |
sschocke | 5:48b7fd921bef | 278 | } |
sschocke | 5:48b7fd921bef | 279 | |
sschocke | 5:48b7fd921bef | 280 | std::string mac = std::string(reply); |
sschocke | 5:48b7fd921bef | 281 | mac = mac.substr(1, 17); |
sschocke | 5:48b7fd921bef | 282 | |
sschocke | 5:48b7fd921bef | 283 | return mac; |
sschocke | 5:48b7fd921bef | 284 | } |
sschocke | 5:48b7fd921bef | 285 | std::string ESP8266_WebServer::GetStationIP(void) { |
sschocke | 5:48b7fd921bef | 286 | while( reqMode == true ) { wait_ms(1); } |
sschocke | 5:48b7fd921bef | 287 | readBuffer(); |
sschocke | 5:48b7fd921bef | 288 | serial->printf("AT+CIPSTA?\r\n"); |
sschocke | 5:48b7fd921bef | 289 | while( data_waiting() == 0 ) { |
sschocke | 5:48b7fd921bef | 290 | wait_ms(10); |
sschocke | 5:48b7fd921bef | 291 | } |
sschocke | 5:48b7fd921bef | 292 | readBuffer(); |
sschocke | 5:48b7fd921bef | 293 | if( debugSerial != NULL ) { |
sschocke | 5:48b7fd921bef | 294 | debugSerial->printf("Station IP Reply: %s\r\n", reply); |
sschocke | 5:48b7fd921bef | 295 | } |
sschocke | 5:48b7fd921bef | 296 | |
sschocke | 5:48b7fd921bef | 297 | std::string ip = std::string(reply); |
sschocke | 5:48b7fd921bef | 298 | ip = ip.substr(1, ip.find('"', 1) - 1); |
sschocke | 5:48b7fd921bef | 299 | |
sschocke | 5:48b7fd921bef | 300 | return ip; |
sschocke | 5:48b7fd921bef | 301 | } |
sschocke | 5:48b7fd921bef | 302 | std::string ESP8266_WebServer::GetAPIP(void) { |
sschocke | 5:48b7fd921bef | 303 | while( reqMode == true ) { wait_ms(1); } |
sschocke | 5:48b7fd921bef | 304 | readBuffer(); |
sschocke | 5:48b7fd921bef | 305 | serial->printf("AT+CIPAP?\r\n"); |
sschocke | 5:48b7fd921bef | 306 | while( data_waiting() == 0 ) { |
sschocke | 5:48b7fd921bef | 307 | wait_ms(10); |
sschocke | 5:48b7fd921bef | 308 | } |
sschocke | 5:48b7fd921bef | 309 | readBuffer(); |
sschocke | 5:48b7fd921bef | 310 | if( debugSerial != NULL ) { |
sschocke | 5:48b7fd921bef | 311 | debugSerial->printf("SoftAP IP Reply: %s\r\n", reply); |
sschocke | 5:48b7fd921bef | 312 | } |
sschocke | 5:48b7fd921bef | 313 | |
sschocke | 5:48b7fd921bef | 314 | std::string ip = std::string(reply); |
sschocke | 5:48b7fd921bef | 315 | ip = ip.substr(1, ip.find('"', 1) - 1); |
sschocke | 5:48b7fd921bef | 316 | |
sschocke | 5:48b7fd921bef | 317 | return ip; |
sschocke | 5:48b7fd921bef | 318 | } |
sschocke | 5:48b7fd921bef | 319 | int ESP8266_WebServer::GetOperatingMode(void) { |
sschocke | 5:48b7fd921bef | 320 | while( reqMode == true ) { wait_ms(1); } |
sschocke | 5:48b7fd921bef | 321 | readBuffer(); |
sschocke | 5:48b7fd921bef | 322 | serial->printf("AT+CWMODE?\r\n"); |
sschocke | 5:48b7fd921bef | 323 | while( data_waiting() == 0 ) { |
sschocke | 5:48b7fd921bef | 324 | wait_ms(10); |
sschocke | 5:48b7fd921bef | 325 | } |
sschocke | 5:48b7fd921bef | 326 | readBuffer(); |
sschocke | 5:48b7fd921bef | 327 | if( debugSerial != NULL ) { |
sschocke | 5:48b7fd921bef | 328 | debugSerial->printf("Operating Mode Reply: %s\r\n", reply); |
sschocke | 5:48b7fd921bef | 329 | } |
sschocke | 5:48b7fd921bef | 330 | |
sschocke | 5:48b7fd921bef | 331 | return atoi(reply); |
sschocke | 5:48b7fd921bef | 332 | } |
sschocke | 5:48b7fd921bef | 333 | std::string ESP8266_WebServer::GetStationSSID(void) { |
sschocke | 5:48b7fd921bef | 334 | while( reqMode == true ) { wait_ms(1); } |
sschocke | 5:48b7fd921bef | 335 | readBuffer(); |
sschocke | 5:48b7fd921bef | 336 | serial->printf("AT+CWJAP?\r\n"); |
sschocke | 5:48b7fd921bef | 337 | while( data_waiting() == 0 ) { |
sschocke | 5:48b7fd921bef | 338 | wait_ms(10); |
sschocke | 5:48b7fd921bef | 339 | } |
sschocke | 5:48b7fd921bef | 340 | readBuffer(); |
sschocke | 5:48b7fd921bef | 341 | if( debugSerial != NULL ) { |
sschocke | 5:48b7fd921bef | 342 | debugSerial->printf("Station SSID Reply: %s\r\n", reply); |
sschocke | 5:48b7fd921bef | 343 | } |
sschocke | 5:48b7fd921bef | 344 | |
sschocke | 5:48b7fd921bef | 345 | std::string ssid = std::string(reply); |
sschocke | 5:48b7fd921bef | 346 | if( strstr(reply, "No AP\r\n") != NULL ) { return "(None)"; } |
sschocke | 5:48b7fd921bef | 347 | ssid = ssid.substr(1, ssid.find('"', 1) - 1); |
sschocke | 5:48b7fd921bef | 348 | |
sschocke | 5:48b7fd921bef | 349 | return ssid; |
sschocke | 5:48b7fd921bef | 350 | } |
sschocke | 5:48b7fd921bef | 351 | std::list<std::string> ESP8266_WebServer::ListAvailableSSID(void) { |
sschocke | 5:48b7fd921bef | 352 | std::list<std::string> apList; |
sschocke | 5:48b7fd921bef | 353 | |
sschocke | 5:48b7fd921bef | 354 | while( reqMode == true ) { wait_ms(1); } |
sschocke | 5:48b7fd921bef | 355 | readBuffer(); |
sschocke | 5:48b7fd921bef | 356 | serial->printf("AT+CWLAP\r\n"); |
sschocke | 5:48b7fd921bef | 357 | while( data_waiting() == 0 ) { |
sschocke | 5:48b7fd921bef | 358 | wait_ms(10); |
sschocke | 5:48b7fd921bef | 359 | } |
sschocke | 5:48b7fd921bef | 360 | readBuffer(); |
sschocke | 5:48b7fd921bef | 361 | if( debugSerial != NULL ) { |
sschocke | 5:48b7fd921bef | 362 | debugSerial->printf("SSID List Reply: %s\r\n", reply); |
sschocke | 5:48b7fd921bef | 363 | } |
sschocke | 5:48b7fd921bef | 364 | |
sschocke | 5:48b7fd921bef | 365 | char ssid[65]; |
sschocke | 5:48b7fd921bef | 366 | char apmac[20]; |
sschocke | 5:48b7fd921bef | 367 | int ecn; |
sschocke | 5:48b7fd921bef | 368 | int rssi; |
sschocke | 5:48b7fd921bef | 369 | int tokenLength; |
sschocke | 5:48b7fd921bef | 370 | char* token = strstr(reply, "("); |
sschocke | 5:48b7fd921bef | 371 | while( token != NULL ) { |
sschocke | 5:48b7fd921bef | 372 | int numMatched = sscanf(token, "(%d,\"%[^\"]\",%d,\"%[0123456789abcdef:]\",%*d)\r\n%n", &ecn, ssid, &rssi, apmac, &tokenLength); |
sschocke | 5:48b7fd921bef | 373 | if( numMatched < 4 ) { |
sschocke | 5:48b7fd921bef | 374 | if( debugSerial != NULL ) { |
sschocke | 5:48b7fd921bef | 375 | debugSerial->printf("SSID List Token Error: NumMatched=%d, SSID=%s\r\n", numMatched, ssid); |
sschocke | 5:48b7fd921bef | 376 | } |
sschocke | 5:48b7fd921bef | 377 | return apList; |
sschocke | 5:48b7fd921bef | 378 | } |
sschocke | 5:48b7fd921bef | 379 | if( debugSerial != NULL ) { |
sschocke | 5:48b7fd921bef | 380 | 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 | 381 | } |
sschocke | 5:48b7fd921bef | 382 | apList.push_back(std::string(ssid)); |
sschocke | 5:48b7fd921bef | 383 | token += tokenLength; |
sschocke | 5:48b7fd921bef | 384 | token = strstr(token, "("); |
sschocke | 5:48b7fd921bef | 385 | } |
sschocke | 5:48b7fd921bef | 386 | |
sschocke | 5:48b7fd921bef | 387 | return apList; |
sschocke | 5:48b7fd921bef | 388 | } |
sschocke | 5:48b7fd921bef | 389 | std::string ESP8266_WebServer::GetFirmwareVersion(void) { |
sschocke | 5:48b7fd921bef | 390 | while( reqMode == true ) { wait_ms(1); } |
sschocke | 5:48b7fd921bef | 391 | readBuffer(); |
sschocke | 5:48b7fd921bef | 392 | serial->printf("AT+GMR\r\n"); |
sschocke | 5:48b7fd921bef | 393 | while( data_waiting() == 0 ) { |
sschocke | 5:48b7fd921bef | 394 | wait_ms(10); |
sschocke | 5:48b7fd921bef | 395 | } |
sschocke | 5:48b7fd921bef | 396 | readBuffer(); |
sschocke | 5:48b7fd921bef | 397 | if( debugSerial != NULL ) { |
sschocke | 5:48b7fd921bef | 398 | debugSerial->printf("Firmware Version Reply: %s\r\n", reply); |
sschocke | 5:48b7fd921bef | 399 | } |
sschocke | 5:48b7fd921bef | 400 | |
sschocke | 5:48b7fd921bef | 401 | std::string ver = std::string(reply); |
sschocke | 5:48b7fd921bef | 402 | ver = ver.substr(0, ver.find('\r')); |
sschocke | 5:48b7fd921bef | 403 | |
sschocke | 5:48b7fd921bef | 404 | return ver; |
sschocke | 5:48b7fd921bef | 405 | } |
sschocke | 5:48b7fd921bef | 406 | |
leet | 4:759de84e790b | 407 | int ESP8266_WebServer::SendStream(int linkID, char const* reply, int WindowSize) { |
leet | 4:759de84e790b | 408 | char const* sendPtr = reply; |
leet | 4:759de84e790b | 409 | int bytesSent = 0; |
leet | 4:759de84e790b | 410 | while( bytesSent < WindowSize ) { |
leet | 4:759de84e790b | 411 | int bytesToSend = WindowSize - bytesSent; |
leet | 4:759de84e790b | 412 | if( bytesToSend > sizeof(response) ) { |
leet | 4:759de84e790b | 413 | bytesToSend = sizeof(response); |
leet | 4:759de84e790b | 414 | } |
leet | 4:759de84e790b | 415 | |
leet | 4:759de84e790b | 416 | memcpy(response, sendPtr, bytesToSend); |
leet | 4:759de84e790b | 417 | sendResponse(linkID, bytesToSend); |
leet | 4:759de84e790b | 418 | sendPtr += bytesToSend; |
leet | 4:759de84e790b | 419 | bytesSent += bytesToSend; |
leet | 4:759de84e790b | 420 | } |
leet | 4:759de84e790b | 421 | |
leet | 4:759de84e790b | 422 | return bytesSent; |
leet | 4:759de84e790b | 423 | } |
leet | 4:759de84e790b | 424 | |
sschocke | 5:48b7fd921bef | 425 | bool ESP8266_WebServer::SetOperatingMode(int mode) { |
sschocke | 5:48b7fd921bef | 426 | if( debugSerial != NULL ) { |
sschocke | 5:48b7fd921bef | 427 | debugSerial->printf("Set Operating Mode to %s(%d)\r\n", opModes[mode], mode); |
sschocke | 5:48b7fd921bef | 428 | } |
sschocke | 6:34d93ea4d519 | 429 | |
sschocke | 5:48b7fd921bef | 430 | while( reqMode == true ) { wait_ms(1); } |
sschocke | 5:48b7fd921bef | 431 | readBuffer(); |
sschocke | 5:48b7fd921bef | 432 | serial->printf("AT+CWMODE=%d\r\n", mode); |
sschocke | 5:48b7fd921bef | 433 | while( data_waiting() == 0 ) { |
sschocke | 5:48b7fd921bef | 434 | wait_ms(10); |
sschocke | 5:48b7fd921bef | 435 | } |
sschocke | 5:48b7fd921bef | 436 | readBuffer(); |
sschocke | 5:48b7fd921bef | 437 | if( debugSerial != NULL ) { |
sschocke | 5:48b7fd921bef | 438 | debugSerial->printf("Set Operating Mode Reply: %s\r\n", reply); |
sschocke | 5:48b7fd921bef | 439 | } |
sschocke | 5:48b7fd921bef | 440 | if( strstr(reply, "\r\nOK\r\n") != NULL ) { return true; } |
sschocke | 5:48b7fd921bef | 441 | return false; |
sschocke | 5:48b7fd921bef | 442 | } |
sschocke | 5:48b7fd921bef | 443 | |
sschocke | 5:48b7fd921bef | 444 | bool ESP8266_WebServer::SetStationSSID(std::string newAP, std::string password) { |
sschocke | 5:48b7fd921bef | 445 | if( debugSerial != NULL ) { |
sschocke | 5:48b7fd921bef | 446 | debugSerial->printf("Set Station SSID to %s, Password=%s\r\n", newAP.c_str(), password.c_str()); |
sschocke | 5:48b7fd921bef | 447 | } |
sschocke | 5:48b7fd921bef | 448 | while( reqMode == true ) { wait_ms(1); } |
sschocke | 5:48b7fd921bef | 449 | readBuffer(); |
sschocke | 5:48b7fd921bef | 450 | serial->printf("AT+CWJAP=\"%s\",\"%s\"\r\n", newAP.c_str(), password.c_str()); |
sschocke | 5:48b7fd921bef | 451 | while( data_waiting() == 0 ) { |
sschocke | 5:48b7fd921bef | 452 | wait_ms(10); |
sschocke | 5:48b7fd921bef | 453 | } |
sschocke | 5:48b7fd921bef | 454 | readBuffer(); |
sschocke | 5:48b7fd921bef | 455 | if( debugSerial != NULL ) { |
sschocke | 5:48b7fd921bef | 456 | debugSerial->printf("Set Station SSID Reply: %s\r\n", reply); |
sschocke | 5:48b7fd921bef | 457 | } |
sschocke | 5:48b7fd921bef | 458 | if( strstr(reply, "\r\nOK\r\n") != NULL ) { return true; } |
sschocke | 5:48b7fd921bef | 459 | |
sschocke | 5:48b7fd921bef | 460 | return false; |
sschocke | 5:48b7fd921bef | 461 | } |
sschocke | 5:48b7fd921bef | 462 | |
sschocke | 0:56c72c87d2f5 | 463 | ESP8266_WebRequest::ESP8266_WebRequest(const char* packet, Serial* debug) { |
sschocke | 0:56c72c87d2f5 | 464 | int sz = strlen(packet); |
sschocke | 0:56c72c87d2f5 | 465 | data = (char *)malloc(sz+1); |
sschocke | 0:56c72c87d2f5 | 466 | memcpy(data, packet, sz+1); |
sschocke | 0:56c72c87d2f5 | 467 | debugSerial = debug; |
sschocke | 0:56c72c87d2f5 | 468 | } |
sschocke | 0:56c72c87d2f5 | 469 | |
sschocke | 0:56c72c87d2f5 | 470 | void ESP8266_WebRequest::Read(void) { |
sschocke | 0:56c72c87d2f5 | 471 | int bytesRecv, ipdLen, linkID; |
sschocke | 0:56c72c87d2f5 | 472 | int numMatched = sscanf(data,"+IPD,%d,%d:%n", &linkID, &bytesRecv, &ipdLen); |
sschocke | 0:56c72c87d2f5 | 473 | if( numMatched != 2 ) { |
sschocke | 0:56c72c87d2f5 | 474 | if( debugSerial != NULL ) { |
sschocke | 0:56c72c87d2f5 | 475 | debugSerial->printf("IPD ERROR : Matched %d, LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", numMatched, linkID, bytesRecv, ipdLen); |
sschocke | 0:56c72c87d2f5 | 476 | } |
sschocke | 0:56c72c87d2f5 | 477 | return; |
sschocke | 0:56c72c87d2f5 | 478 | } |
sschocke | 0:56c72c87d2f5 | 479 | |
sschocke | 0:56c72c87d2f5 | 480 | if( debugSerial != NULL ) { |
sschocke | 0:56c72c87d2f5 | 481 | debugSerial->printf("IPD Data: LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", linkID, bytesRecv, ipdLen); |
sschocke | 0:56c72c87d2f5 | 482 | } |
sschocke | 0:56c72c87d2f5 | 483 | if( strstr(data, "HTTP") != NULL ) { |
sschocke | 0:56c72c87d2f5 | 484 | if( debugSerial != NULL ) { |
sschocke | 0:56c72c87d2f5 | 485 | debugSerial->printf("Got HTTP Request\r\n"); |
sschocke | 0:56c72c87d2f5 | 486 | } |
sschocke | 0:56c72c87d2f5 | 487 | char* httpPacket = data + ipdLen; |
sschocke | 0:56c72c87d2f5 | 488 | if( debugSerial != NULL ) { |
sschocke | 0:56c72c87d2f5 | 489 | debugSerial->printf("HTTP Packet: %s\r\n", httpPacket); |
sschocke | 0:56c72c87d2f5 | 490 | } |
sschocke | 0:56c72c87d2f5 | 491 | int numMatched = sscanf(httpPacket, "%s %s HTTP/%*c.%*c", httpMethod, httpURI); |
sschocke | 0:56c72c87d2f5 | 492 | if( numMatched != 2 ) { |
sschocke | 0:56c72c87d2f5 | 493 | if( debugSerial != NULL ) { |
sschocke | 0:56c72c87d2f5 | 494 | debugSerial->printf("HTTP ERROR : Matched %d, Method=%s, URI=%s\r\n", numMatched, httpMethod, httpURI); |
sschocke | 0:56c72c87d2f5 | 495 | } |
sschocke | 0:56c72c87d2f5 | 496 | return; |
sschocke | 0:56c72c87d2f5 | 497 | } |
sschocke | 0:56c72c87d2f5 | 498 | |
sschocke | 0:56c72c87d2f5 | 499 | LinkID = linkID; |
sschocke | 0:56c72c87d2f5 | 500 | Method = httpMethod; |
sschocke | 0:56c72c87d2f5 | 501 | URI = httpURI; |
sschocke | 0:56c72c87d2f5 | 502 | int pos = URI.find('?'); |
sschocke | 0:56c72c87d2f5 | 503 | if(pos != string::npos ) { |
sschocke | 0:56c72c87d2f5 | 504 | string params = URI.substr(pos+1); |
sschocke | 0:56c72c87d2f5 | 505 | URI = URI.substr(0,pos); |
sschocke | 0:56c72c87d2f5 | 506 | pos = params.find('='); |
sschocke | 0:56c72c87d2f5 | 507 | while( pos != string::npos ) { |
sschocke | 0:56c72c87d2f5 | 508 | string name = params.substr(0,pos); |
sschocke | 0:56c72c87d2f5 | 509 | string value = params.substr(pos+1); |
sschocke | 0:56c72c87d2f5 | 510 | pos = params.find('&'); |
sschocke | 0:56c72c87d2f5 | 511 | if( pos == string::npos ) |
sschocke | 0:56c72c87d2f5 | 512 | { |
sschocke | 0:56c72c87d2f5 | 513 | if( debugSerial != NULL ) { |
sschocke | 0:56c72c87d2f5 | 514 | debugSerial->printf("HTTP GET Parameter %s=%s\r\n", name.c_str(), value.c_str()); |
sschocke | 0:56c72c87d2f5 | 515 | } |
sschocke | 0:56c72c87d2f5 | 516 | Parameters[name] = value; |
sschocke | 0:56c72c87d2f5 | 517 | break; |
sschocke | 0:56c72c87d2f5 | 518 | } |
sschocke | 0:56c72c87d2f5 | 519 | |
sschocke | 0:56c72c87d2f5 | 520 | value = value.substr(0,value.find('&')); |
sschocke | 0:56c72c87d2f5 | 521 | params = params.substr(pos+1); |
sschocke | 0:56c72c87d2f5 | 522 | pos = params.find('='); |
sschocke | 0:56c72c87d2f5 | 523 | if( debugSerial != NULL ) { |
sschocke | 0:56c72c87d2f5 | 524 | debugSerial->printf("HTTP GET Parameter %s=%s\r\n", name.c_str(), value.c_str()); |
sschocke | 0:56c72c87d2f5 | 525 | } |
sschocke | 0:56c72c87d2f5 | 526 | Parameters[name] = value; |
sschocke | 0:56c72c87d2f5 | 527 | } |
sschocke | 0:56c72c87d2f5 | 528 | } |
sschocke | 0:56c72c87d2f5 | 529 | if( debugSerial != NULL ) { |
sschocke | 0:56c72c87d2f5 | 530 | debugSerial->printf("HTTP %s %s\r\n", httpMethod, httpURI); |
sschocke | 0:56c72c87d2f5 | 531 | } |
sschocke | 0:56c72c87d2f5 | 532 | } |
sschocke | 0:56c72c87d2f5 | 533 | } |
sschocke | 0:56c72c87d2f5 | 534 | |
sschocke | 0:56c72c87d2f5 | 535 | ESP8266_WebRequest::~ESP8266_WebRequest() |
sschocke | 0:56c72c87d2f5 | 536 | { |
sschocke | 0:56c72c87d2f5 | 537 | free(data); |
sschocke | 0:56c72c87d2f5 | 538 | } |