DE: Ein sehr, sehr einfacher Webserver mithilfe eines ESP8266 auf dem die AT-Firmware läuft . Für Bulme Bertl. - EN: A very, very, basic web server that works using an ESP8266 running the AT-Firmware.

Dependents:   BULME_BERTL17_WebServer_ESPAT Bravo Team

Click here for example code!

DE:

Ein sehr, sehr einfacher Webserver mithilfe eines ESP8266 auf dem die AT-Firmware läuft. Getestet mit einem ESP-01 Modul am Bulme Bertl 2017 (LPC11U68).

EN:

A very, very, basic web server that works using an ESP8266 running the AT-Firmware. Tested with an ESP-01 Module and Bulme Bertl 2017 (LPC11U68).

ESPAT.cpp

Committer:
EliasN
Date:
2019-02-19
Revision:
1:aa115daaaa02
Parent:
0:afba75b3b390
Child:
2:61ed6c1c9bdd

File content as of revision 1:aa115daaaa02:

#include "ESPAT.h"
#include "mbed.h"
#include "string"

/*
Library for using an ESP8266 (e.g. ESP-01 board) with its AT command firmware as webserver
Version: 0.1.0
(C)2019 Elias Nestl
*/

ESPAT::ESPAT(PinName tx, PinName rx, string _wifiName, string _wifiPass, int baud) : espSerial(tx, rx, baud) {
    wifiName = _wifiName;
    wifiPass = _wifiPass;
}

void ESPAT::readStrUntil(string * str, char until) {
    while (true) {
        char c = espSerial.getc();
        if (c != until) {
            *str += c;
        } else {
            break;
        }
    }
}

void ESPAT::waitFor(char * text) {
    for (int i = 0; i < strlen(text); i++) {
        char c = espSerial.getc();
        if (c != text[i]) {
            i = 0;
        }
    }
}

void ESPAT::sendCommand(char * cmd, bool waitOk) {
    espSerial.printf("%s\r\n", cmd);
    if (waitOk) waitFor("OK");
}

void ESPAT::resetEsp() {
    sendCommand("AT+RST");
    waitFor("ready");
}

void ESPAT::initWifi() {
    sendCommand("AT+CWMODE_CUR=1");
    string wifiCmd = "AT+CWJAP_CUR=\"" + wifiName + "\",\"" + wifiPass + "\"";
    sendCommand((char*) wifiCmd.c_str());
}

void ESPAT::initServer(void (*requestHandler)(int, string)) {
    sendCommand("AT+CIPMUX=1");
    sendCommand("AT+CIPSERVER=1,80");
    while (true) {
        // Input looks like this: +IPD,0,372:GET / HTTP/1.1
        waitFor("+IPD,");
        // Parse linkId
        int linkId = 0;
        char c;
        while (true) {
            c = espSerial.getc();
            if (c == ',') break;
            linkId = linkId * 10 + (c - '0');
        }
        // Parse path
        waitFor(":GET ");
        string path;
        readStrUntil(&path, ' ');
        // Send request to handler
        requestHandler(linkId, path);
    }
}

void ESPAT::startWebServer(void (*readyCallback)(), void (*requestHandler)(int, string)) {
    resetEsp();
    initWifi();
    readyCallback();
    initServer(requestHandler);
}

void ESPAT::tcpReply(int linkId, string data) {
    espSerial.printf("AT+CIPSEND=%d,%d\r\n", linkId, data.length());
    wait(0.1); // Can't use waitFor here as it isn't fast enough
    for (int i = 0; i < data.length(); i++) {
        // Artificially slowing down this to prevent errors
        espSerial.printf("%c", data[i]);
    }
    waitFor("SEND OK");
    espSerial.printf("AT+CIPCLOSE=%d\r\n", linkId);
    waitFor("OK");
}

void ESPAT::httpReply(int linkId, string code, string payload) {
    string data = "HTTP/1.1 " + code + "\r\nContent-Type: text/html\r\n\r\n" + payload;
    tcpReply(linkId, data);
}