Clinton Lee Taylor / Mbed 2 deprecated WiFiLamp

Dependencies:   ESP8266_WebServer mbed

Fork of WiFiLamp by Sebastian Schocke

ESP8266_WebServer/ESP8266_WebServer.cpp

Committer:
sschocke
Date:
2014-12-28
Revision:
11:3ab606a42227
Parent:
9:319aeb6e0123
Child:
13:1e8f27da036a
Child:
14:416fff90cd0f

File content as of revision 11:3ab606a42227:

#include "ESP8266_WebServer.h"
#include <string>
#include <map>

ESP8266_WebServer::ESP8266_WebServer(Serial *espUART) {
    serial = espUART;
    rxptr = buffer;
    debugSerial = NULL;
}

void ESP8266_WebServer::rxint(void) {
    char c = serial->getc();
    if( debugSerial != NULL ) {
        debugSerial->putc(c);
    }
    *rxptr = c;
    rxptr++;
    *rxptr = 0;    
}

void ESP8266_WebServer::readBuffer(void) {
    strncpy(reply, buffer, 1024);
    rxptr = buffer;
    *rxptr = 0;
}

short ESP8266_WebServer::data_waiting(void)
{
    char* ok = strstr(buffer, "OK\r\n");
    char* error = strstr(buffer, "ERROR\r\n");
    char* nochange = strstr(buffer, "no change\r\n");
    
    if( (ok != NULL) || (error != NULL ) || (nochange != NULL ) )
    {
        return 1;
    }

    return 0;
}

short ESP8266_WebServer::string_waiting(const char* str)
{
    char* pr = strstr(buffer, str);
    char* error = strstr(buffer, "ERROR\r\n");
    
    if( (pr != NULL) || (error != NULL ) )
    {
        return 1;
    }

    return 0;
}

void ESP8266_WebServer::Initialize(void) {
    readBuffer();
    while( string_waiting("\r\nready\r\n") == 0 ) {
        wait_ms(10);
    }
    readBuffer();
    
    if( debugSerial != NULL ) {
        debugSerial->printf("Done\r\nSetting operating mode...");
    }
    serial->printf("AT+CWMODE=3\r\n");
    while( data_waiting() == 0 ) {
        wait_ms(10);
    }
    readBuffer();
    
    if( debugSerial != NULL ) {
        debugSerial->printf("Done\r\nAccept Multiple connections...");
    }
    serial->printf("AT+CIPMUX=1\r\n");
    while( data_waiting() == 0 ) {
        wait_ms(10);
    }
    readBuffer();
    
    if( debugSerial != NULL ) {
        debugSerial->printf("Done\r\nStarting Web Server...");
    }
    
    serial->printf("AT+CIPSERVER=1,80\r\n");
    while( data_waiting() == 0 ) {
        wait_ms(10);
    }
    readBuffer();
}

ESP8266_WebRequest* ESP8266_WebServer::GetRequest(void)
{
    if( (string_waiting("+IPD") == 1) && (string_waiting("\r\nOK\r\n") == 1) ) {
        if( debugSerial != NULL ) {
            debugSerial->printf("\r\nGot Data\r\n");
        }
        readBuffer();

        char* ipdPacket = strstr(reply, "+IPD");
        int bytesRecv, ipdLen, linkID;
        int numMatched = sscanf(ipdPacket,"+IPD,%d,%d:%n", &linkID, &bytesRecv, &ipdLen);
        if( numMatched != 2 ) {
            if( debugSerial != NULL ) {
                debugSerial->printf("IPD ERROR : Matched %d, LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", numMatched, linkID, bytesRecv, ipdLen);
            }
            return false;
        }

        if( debugSerial != NULL ) {
            debugSerial->printf("IPD Data: LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", linkID, bytesRecv, ipdLen);
        }
        if( strstr(ipdPacket, "HTTP") != NULL ) {
            if( debugSerial != NULL ) {
                debugSerial->printf("Got HTTP Request\r\n");
            }
            char* httpPacket = ipdPacket + ipdLen;
            if( debugSerial != NULL ) {
                debugSerial->printf("HTTP Packet: %s\r\n", httpPacket);
            }
            ESP8266_WebRequest* request = new ESP8266_WebRequest(linkID, httpPacket, debugSerial); 
            
            return request;
        }
    }
    
    return NULL;
}

void ESP8266_WebServer::sendResponse(int linkID, int numBytes) {
    if( debugSerial != NULL ) {
        debugSerial->printf("HTTP Reply Packet(%d bytes): %s\r\n", numBytes, response);
    }
    serial->printf("AT+CIPSEND=%d,%d\r\n", linkID, numBytes);
    wait_ms(500);
    if( (string_waiting("\r\n>") == 1) ) {
        for( int i=0; i<numBytes; i++ ) {
            serial->putc(response[i]);
        }
    }
    while( string_waiting("\r\nSEND OK\r\n") == 0 ) {
        wait_ms(10);
    }
}

void ESP8266_WebServer::SendError(int linkID, std::string error) {
    SendError(linkID, error.c_str());
}
void ESP8266_WebServer::SendError(int linkID, const char* error) {
    sprintf(response, "HTTP/1.1 %s\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s", error, strlen(error), error);
    sendResponse(linkID, strlen(response));
}
void ESP8266_WebServer::Send404Error(int linkID) {
    SendError(linkID, "404 Not Found");
}

void ESP8266_WebServer::SendReply(int linkID, std::string reply, const char* mimeType) {
    SendReply(linkID, reply.c_str(), reply.length(), mimeType);
}

void ESP8266_WebServer::SendReply(int linkID, const char* reply, int replySize, const char* mimeType) {
    sprintf(response, "HTTP/1.1 200 OK\r\nContent-Type:%s\r\nContent-Length: %d\r\n\r\n%s", mimeType, replySize, reply);
    sendResponse(linkID, strlen(response));
}

ESP8266_WebRequest::ESP8266_WebRequest(int linkID, const char* httpPacket, Serial* debugSerial) {
    int numMatched = sscanf(httpPacket, "%s %s HTTP/%*c.%*c", httpMethod, httpURI);
    if( numMatched != 2 ) {
        if( debugSerial != NULL ) {
            debugSerial->printf("HTTP ERROR : Matched %d, Method=%s, URI=%s\r\n", numMatched, httpMethod, httpURI);
        }
        return;
    }
    if( debugSerial != NULL ) {
        debugSerial->printf("HTTP %s %s\r\n", httpMethod, httpURI);
    }

    LinkID = linkID;
    Method = httpMethod;
    URI = httpURI;
    int pos = URI.find('?');
    if(pos != string::npos ) {
        string params = URI.substr(pos+1);
        URI = URI.substr(0,pos);
        if( debugSerial != NULL ) {
            debugSerial->printf("HTTP %s %s\r\n", URI.c_str(), params.c_str());
        }
        pos = params.find('=');
        while( pos != string::npos ) {
            string name = params.substr(0,pos);
            string value = params.substr(pos+1);
            pos = params.find('&');
            if( pos == string::npos )
            {
                if( debugSerial != NULL ) {
                    debugSerial->printf("HTTP GET Parameter %s=%s\r\n", name.c_str(), value.c_str());
                }
                Parameters[name] = value;
                break;
            }
            
            value = value.substr(0,value.find('&'));
            params = params.substr(pos+1);
            pos = params.find('=');
            if( debugSerial != NULL ) {
                debugSerial->printf("HTTP GET Parameter %s=%s\r\n", name.c_str(), value.c_str());
            }
            Parameters[name] = value;
        }
    }
}