Example program using the ESPAT library. Made for BULME Bertl 2017.

Dependencies:   mbed ESPAT

Demo Program für mein ESPAT Library für Bulme Bertl 2017. Startet einen WebServer mit zwei Buttons um die LED D10 zu steuern.

main.cpp

Committer:
EliasN
Date:
2019-03-29
Revision:
1:f1f79c0e0d15
Parent:
0:617bf6669ab4
Child:
2:db40a55d1b0b

File content as of revision 1:f1f79c0e0d15:

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

/*
Example program using the ESPAT library.
Made for BULME Bertl 2017 (LPC11U68)
Make sure to edit your wifi login data below
# Features:
 - Led D10 turns on when ready (wifi connected + server listening)
 - Navigate to the device's ip in your browser and use the buttons to control Led D11
 - Navigate to <device ip>/test for a test page that demonstrates that paths work
(C)2019 Elias Nestl
*/

ESPAT esp(P1_0, P0_20, 115200);

DigitalOut Led0 (P1_18); // D10
DigitalOut Led1 (P2_16); // D11

string htmlPage = "<html>"
"<head><title>Led toggler</title></head>"
"<body>"
"<button onclick=\"fetch('./ledOn')\">Led ON</button>"
"<button onclick=\"fetch('./ledOff')\">Led OFF</button>"
"</body></html>";

void handleReady() {
    Led0 = 0;
}

void handleRequest(int linkId, string path) {
    if (path == "/") {
        esp.httpReply(linkId, "200 OK", htmlPage);
    } else if (path == "/ledOn") {
        Led1 = 0;
        esp.httpReply(linkId, "200 OK", "success");
    } else if (path == "/ledOff") {
        Led1 = 1;
        esp.httpReply(linkId, "200 OK", "success");
    } else if (path == "/test") {
        esp.httpReply(linkId, "200 OK", "This is just a test");
    } else {
        esp.httpReply(linkId, "404 Not Found", "404 Not found!");
    }
}

int main() {
    Led0 = Led1 = 1;
    esp.resetEsp();
    esp.initWifiAP("ESP", "12345678");
    Led0 = 0;
    esp.initServer(handleRequest);
}