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-18
Revision:
7:f15c81074400
Parent:
5:42c6f9d916bc
Child:
9:319aeb6e0123

File content as of revision 7:f15c81074400:

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

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();
}

bool 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;
        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);
            }

            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 false;
            }
            if( debugSerial != NULL ) {
                debugSerial->printf("HTTP %s %s\r\n", httpMethod, httpURI);
            }

            Method = httpMethod;
            URI = httpURI;
            
            return true;
        }
    }
    
    return false;
}

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

void ESP8266_WebServer::Send404Reply(int linkID) {
    sprintf(response, "HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\nContent-Length: 13\r\n\r\n404 Not Found");
    sendResponse(linkID);
}

void ESP8266_WebServer::SendHTMLReply(int linkID, std::string reply) {
    sprintf(response, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s", reply.length(), reply.c_str());
    sendResponse(linkID);
}