Clinton Lee Taylor / Mbed 2 deprecated WiFiLamp

Dependencies:   ESP8266_WebServer mbed

Fork of WiFiLamp by Sebastian Schocke

Committer:
sschocke
Date:
Sun Dec 28 16:04:22 2014 +0000
Revision:
11:3ab606a42227
Parent:
9:319aeb6e0123
Child:
13:1e8f27da036a
Child:
14:416fff90cd0f
WebServer API Updates for multiple content types

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sschocke 5:42c6f9d916bc 1 #include "ESP8266_WebServer.h"
sschocke 7:f15c81074400 2 #include <string>
sschocke 9:319aeb6e0123 3 #include <map>
sschocke 7:f15c81074400 4
sschocke 7:f15c81074400 5 ESP8266_WebServer::ESP8266_WebServer(Serial *espUART) {
sschocke 7:f15c81074400 6 serial = espUART;
sschocke 7:f15c81074400 7 rxptr = buffer;
sschocke 7:f15c81074400 8 debugSerial = NULL;
sschocke 7:f15c81074400 9 }
sschocke 7:f15c81074400 10
sschocke 7:f15c81074400 11 void ESP8266_WebServer::rxint(void) {
sschocke 7:f15c81074400 12 char c = serial->getc();
sschocke 7:f15c81074400 13 if( debugSerial != NULL ) {
sschocke 7:f15c81074400 14 debugSerial->putc(c);
sschocke 7:f15c81074400 15 }
sschocke 7:f15c81074400 16 *rxptr = c;
sschocke 7:f15c81074400 17 rxptr++;
sschocke 7:f15c81074400 18 *rxptr = 0;
sschocke 7:f15c81074400 19 }
sschocke 7:f15c81074400 20
sschocke 7:f15c81074400 21 void ESP8266_WebServer::readBuffer(void) {
sschocke 7:f15c81074400 22 strncpy(reply, buffer, 1024);
sschocke 7:f15c81074400 23 rxptr = buffer;
sschocke 7:f15c81074400 24 *rxptr = 0;
sschocke 7:f15c81074400 25 }
sschocke 7:f15c81074400 26
sschocke 7:f15c81074400 27 short ESP8266_WebServer::data_waiting(void)
sschocke 7:f15c81074400 28 {
sschocke 7:f15c81074400 29 char* ok = strstr(buffer, "OK\r\n");
sschocke 7:f15c81074400 30 char* error = strstr(buffer, "ERROR\r\n");
sschocke 7:f15c81074400 31 char* nochange = strstr(buffer, "no change\r\n");
sschocke 7:f15c81074400 32
sschocke 7:f15c81074400 33 if( (ok != NULL) || (error != NULL ) || (nochange != NULL ) )
sschocke 7:f15c81074400 34 {
sschocke 7:f15c81074400 35 return 1;
sschocke 7:f15c81074400 36 }
sschocke 7:f15c81074400 37
sschocke 7:f15c81074400 38 return 0;
sschocke 7:f15c81074400 39 }
sschocke 7:f15c81074400 40
sschocke 7:f15c81074400 41 short ESP8266_WebServer::string_waiting(const char* str)
sschocke 7:f15c81074400 42 {
sschocke 7:f15c81074400 43 char* pr = strstr(buffer, str);
sschocke 7:f15c81074400 44 char* error = strstr(buffer, "ERROR\r\n");
sschocke 7:f15c81074400 45
sschocke 7:f15c81074400 46 if( (pr != NULL) || (error != NULL ) )
sschocke 7:f15c81074400 47 {
sschocke 7:f15c81074400 48 return 1;
sschocke 7:f15c81074400 49 }
sschocke 7:f15c81074400 50
sschocke 7:f15c81074400 51 return 0;
sschocke 7:f15c81074400 52 }
sschocke 5:42c6f9d916bc 53
sschocke 7:f15c81074400 54 void ESP8266_WebServer::Initialize(void) {
sschocke 7:f15c81074400 55 readBuffer();
sschocke 7:f15c81074400 56 while( string_waiting("\r\nready\r\n") == 0 ) {
sschocke 7:f15c81074400 57 wait_ms(10);
sschocke 7:f15c81074400 58 }
sschocke 7:f15c81074400 59 readBuffer();
sschocke 7:f15c81074400 60
sschocke 7:f15c81074400 61 if( debugSerial != NULL ) {
sschocke 7:f15c81074400 62 debugSerial->printf("Done\r\nSetting operating mode...");
sschocke 7:f15c81074400 63 }
sschocke 7:f15c81074400 64 serial->printf("AT+CWMODE=3\r\n");
sschocke 7:f15c81074400 65 while( data_waiting() == 0 ) {
sschocke 7:f15c81074400 66 wait_ms(10);
sschocke 7:f15c81074400 67 }
sschocke 7:f15c81074400 68 readBuffer();
sschocke 7:f15c81074400 69
sschocke 7:f15c81074400 70 if( debugSerial != NULL ) {
sschocke 7:f15c81074400 71 debugSerial->printf("Done\r\nAccept Multiple connections...");
sschocke 7:f15c81074400 72 }
sschocke 7:f15c81074400 73 serial->printf("AT+CIPMUX=1\r\n");
sschocke 7:f15c81074400 74 while( data_waiting() == 0 ) {
sschocke 7:f15c81074400 75 wait_ms(10);
sschocke 7:f15c81074400 76 }
sschocke 7:f15c81074400 77 readBuffer();
sschocke 7:f15c81074400 78
sschocke 7:f15c81074400 79 if( debugSerial != NULL ) {
sschocke 7:f15c81074400 80 debugSerial->printf("Done\r\nStarting Web Server...");
sschocke 7:f15c81074400 81 }
sschocke 7:f15c81074400 82
sschocke 7:f15c81074400 83 serial->printf("AT+CIPSERVER=1,80\r\n");
sschocke 7:f15c81074400 84 while( data_waiting() == 0 ) {
sschocke 7:f15c81074400 85 wait_ms(10);
sschocke 7:f15c81074400 86 }
sschocke 7:f15c81074400 87 readBuffer();
sschocke 7:f15c81074400 88 }
sschocke 7:f15c81074400 89
sschocke 9:319aeb6e0123 90 ESP8266_WebRequest* ESP8266_WebServer::GetRequest(void)
sschocke 5:42c6f9d916bc 91 {
sschocke 7:f15c81074400 92 if( (string_waiting("+IPD") == 1) && (string_waiting("\r\nOK\r\n") == 1) ) {
sschocke 7:f15c81074400 93 if( debugSerial != NULL ) {
sschocke 7:f15c81074400 94 debugSerial->printf("\r\nGot Data\r\n");
sschocke 7:f15c81074400 95 }
sschocke 7:f15c81074400 96 readBuffer();
sschocke 7:f15c81074400 97
sschocke 7:f15c81074400 98 char* ipdPacket = strstr(reply, "+IPD");
sschocke 9:319aeb6e0123 99 int bytesRecv, ipdLen, linkID;
sschocke 9:319aeb6e0123 100 int numMatched = sscanf(ipdPacket,"+IPD,%d,%d:%n", &linkID, &bytesRecv, &ipdLen);
sschocke 7:f15c81074400 101 if( numMatched != 2 ) {
sschocke 7:f15c81074400 102 if( debugSerial != NULL ) {
sschocke 9:319aeb6e0123 103 debugSerial->printf("IPD ERROR : Matched %d, LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", numMatched, linkID, bytesRecv, ipdLen);
sschocke 7:f15c81074400 104 }
sschocke 7:f15c81074400 105 return false;
sschocke 7:f15c81074400 106 }
sschocke 7:f15c81074400 107
sschocke 7:f15c81074400 108 if( debugSerial != NULL ) {
sschocke 9:319aeb6e0123 109 debugSerial->printf("IPD Data: LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", linkID, bytesRecv, ipdLen);
sschocke 7:f15c81074400 110 }
sschocke 7:f15c81074400 111 if( strstr(ipdPacket, "HTTP") != NULL ) {
sschocke 7:f15c81074400 112 if( debugSerial != NULL ) {
sschocke 7:f15c81074400 113 debugSerial->printf("Got HTTP Request\r\n");
sschocke 7:f15c81074400 114 }
sschocke 7:f15c81074400 115 char* httpPacket = ipdPacket + ipdLen;
sschocke 7:f15c81074400 116 if( debugSerial != NULL ) {
sschocke 7:f15c81074400 117 debugSerial->printf("HTTP Packet: %s\r\n", httpPacket);
sschocke 7:f15c81074400 118 }
sschocke 9:319aeb6e0123 119 ESP8266_WebRequest* request = new ESP8266_WebRequest(linkID, httpPacket, debugSerial);
sschocke 7:f15c81074400 120
sschocke 9:319aeb6e0123 121 return request;
sschocke 7:f15c81074400 122 }
sschocke 7:f15c81074400 123 }
sschocke 7:f15c81074400 124
sschocke 9:319aeb6e0123 125 return NULL;
sschocke 7:f15c81074400 126 }
sschocke 7:f15c81074400 127
sschocke 11:3ab606a42227 128 void ESP8266_WebServer::sendResponse(int linkID, int numBytes) {
sschocke 7:f15c81074400 129 if( debugSerial != NULL ) {
sschocke 11:3ab606a42227 130 debugSerial->printf("HTTP Reply Packet(%d bytes): %s\r\n", numBytes, response);
sschocke 7:f15c81074400 131 }
sschocke 11:3ab606a42227 132 serial->printf("AT+CIPSEND=%d,%d\r\n", linkID, numBytes);
sschocke 7:f15c81074400 133 wait_ms(500);
sschocke 7:f15c81074400 134 if( (string_waiting("\r\n>") == 1) ) {
sschocke 11:3ab606a42227 135 for( int i=0; i<numBytes; i++ ) {
sschocke 11:3ab606a42227 136 serial->putc(response[i]);
sschocke 11:3ab606a42227 137 }
sschocke 7:f15c81074400 138 }
sschocke 7:f15c81074400 139 while( string_waiting("\r\nSEND OK\r\n") == 0 ) {
sschocke 7:f15c81074400 140 wait_ms(10);
sschocke 7:f15c81074400 141 }
sschocke 7:f15c81074400 142 }
sschocke 7:f15c81074400 143
sschocke 11:3ab606a42227 144 void ESP8266_WebServer::SendError(int linkID, std::string error) {
sschocke 11:3ab606a42227 145 SendError(linkID, error.c_str());
sschocke 11:3ab606a42227 146 }
sschocke 11:3ab606a42227 147 void ESP8266_WebServer::SendError(int linkID, const char* error) {
sschocke 11:3ab606a42227 148 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 11:3ab606a42227 149 sendResponse(linkID, strlen(response));
sschocke 11:3ab606a42227 150 }
sschocke 11:3ab606a42227 151 void ESP8266_WebServer::Send404Error(int linkID) {
sschocke 11:3ab606a42227 152 SendError(linkID, "404 Not Found");
sschocke 7:f15c81074400 153 }
sschocke 7:f15c81074400 154
sschocke 11:3ab606a42227 155 void ESP8266_WebServer::SendReply(int linkID, std::string reply, const char* mimeType) {
sschocke 11:3ab606a42227 156 SendReply(linkID, reply.c_str(), reply.length(), mimeType);
sschocke 11:3ab606a42227 157 }
sschocke 11:3ab606a42227 158
sschocke 11:3ab606a42227 159 void ESP8266_WebServer::SendReply(int linkID, const char* reply, int replySize, const char* mimeType) {
sschocke 11:3ab606a42227 160 sprintf(response, "HTTP/1.1 200 OK\r\nContent-Type:%s\r\nContent-Length: %d\r\n\r\n%s", mimeType, replySize, reply);
sschocke 11:3ab606a42227 161 sendResponse(linkID, strlen(response));
sschocke 7:f15c81074400 162 }
sschocke 9:319aeb6e0123 163
sschocke 9:319aeb6e0123 164 ESP8266_WebRequest::ESP8266_WebRequest(int linkID, const char* httpPacket, Serial* debugSerial) {
sschocke 9:319aeb6e0123 165 int numMatched = sscanf(httpPacket, "%s %s HTTP/%*c.%*c", httpMethod, httpURI);
sschocke 9:319aeb6e0123 166 if( numMatched != 2 ) {
sschocke 9:319aeb6e0123 167 if( debugSerial != NULL ) {
sschocke 9:319aeb6e0123 168 debugSerial->printf("HTTP ERROR : Matched %d, Method=%s, URI=%s\r\n", numMatched, httpMethod, httpURI);
sschocke 9:319aeb6e0123 169 }
sschocke 9:319aeb6e0123 170 return;
sschocke 9:319aeb6e0123 171 }
sschocke 9:319aeb6e0123 172 if( debugSerial != NULL ) {
sschocke 9:319aeb6e0123 173 debugSerial->printf("HTTP %s %s\r\n", httpMethod, httpURI);
sschocke 9:319aeb6e0123 174 }
sschocke 9:319aeb6e0123 175
sschocke 9:319aeb6e0123 176 LinkID = linkID;
sschocke 9:319aeb6e0123 177 Method = httpMethod;
sschocke 9:319aeb6e0123 178 URI = httpURI;
sschocke 9:319aeb6e0123 179 int pos = URI.find('?');
sschocke 9:319aeb6e0123 180 if(pos != string::npos ) {
sschocke 9:319aeb6e0123 181 string params = URI.substr(pos+1);
sschocke 9:319aeb6e0123 182 URI = URI.substr(0,pos);
sschocke 9:319aeb6e0123 183 if( debugSerial != NULL ) {
sschocke 9:319aeb6e0123 184 debugSerial->printf("HTTP %s %s\r\n", URI.c_str(), params.c_str());
sschocke 9:319aeb6e0123 185 }
sschocke 9:319aeb6e0123 186 pos = params.find('=');
sschocke 9:319aeb6e0123 187 while( pos != string::npos ) {
sschocke 9:319aeb6e0123 188 string name = params.substr(0,pos);
sschocke 9:319aeb6e0123 189 string value = params.substr(pos+1);
sschocke 9:319aeb6e0123 190 pos = params.find('&');
sschocke 9:319aeb6e0123 191 if( pos == string::npos )
sschocke 9:319aeb6e0123 192 {
sschocke 9:319aeb6e0123 193 if( debugSerial != NULL ) {
sschocke 9:319aeb6e0123 194 debugSerial->printf("HTTP GET Parameter %s=%s\r\n", name.c_str(), value.c_str());
sschocke 9:319aeb6e0123 195 }
sschocke 9:319aeb6e0123 196 Parameters[name] = value;
sschocke 9:319aeb6e0123 197 break;
sschocke 9:319aeb6e0123 198 }
sschocke 9:319aeb6e0123 199
sschocke 9:319aeb6e0123 200 value = value.substr(0,value.find('&'));
sschocke 9:319aeb6e0123 201 params = params.substr(pos+1);
sschocke 9:319aeb6e0123 202 pos = params.find('=');
sschocke 9:319aeb6e0123 203 if( debugSerial != NULL ) {
sschocke 9:319aeb6e0123 204 debugSerial->printf("HTTP GET Parameter %s=%s\r\n", name.c_str(), value.c_str());
sschocke 9:319aeb6e0123 205 }
sschocke 9:319aeb6e0123 206 Parameters[name] = value;
sschocke 9:319aeb6e0123 207 }
sschocke 9:319aeb6e0123 208 }
sschocke 9:319aeb6e0123 209 }