6#include "ustd_platform.h"
13#include <WiFiClient.h>
14#include <ESP8266WebServer.h>
15#include <ESP8266mDNS.h>
18#include "ustd_array.h"
31 WebServer *pWebServer;
33 ESP8266WebServer *pWebServer;
43 void begin(Scheduler *_pSched) {
45#ifdef __USE_SPIFFS_FS__
52 pWebServer =
new WebServer(80);
54 pWebServer =
new ESP8266WebServer(80);
58 auto ft = [=]() { this->loop(); };
59 tID = pSched->add(ft,
"web");
61 auto fnall = [=](String topic, String msg, String originator) {
62 this->subsMsg(topic, msg, originator);
64 pSched->subscribe(tID,
"net/network", fnall);
66 pSched->publish(
"net/network/get");
73 String getContentType(String fileName) {
74 if (fileName.endsWith(
".html"))
76 if (fileName.endsWith(
".css"))
78 if (fileName.endsWith(
".png"))
80 if (fileName.endsWith(
".js"))
81 return "application/javascript";
82 if (fileName.endsWith(
".ico"))
83 return "image/x-icon";
87 void handleFileSystem() {
88 String fileName = pWebServer->uri();
90 fileName =
"/index.html";
91 String contentType = getContentType(fileName);
92#ifdef __USE_SPIFFS_FS__
93 if (SPIFFS.exists(fileName)) {
94 fs::File f = SPIFFS.open(fileName,
"r");
96 if (LittleFS.exists(fileName)) {
97 fs::File f = LittleFS.open(fileName,
"r");
99 pWebServer->streamFile(f, contentType);
107 void handleNotFound() {
108 String message =
"File Not Found\n\n";
110 message += pWebServer->uri();
111 message +=
"\nMethod: ";
112 message += (pWebServer->method() == HTTP_GET) ?
"GET" :
"POST";
113 message +=
"\nArguments: ";
114 message += pWebServer->args();
116 for (uint8_t i = 0; i < pWebServer->args(); i++) {
117 message +=
" " + pWebServer->argName(i) +
": " + pWebServer->arg(i) +
"\n";
119 pWebServer->send(404,
"text/plain", message);
123 auto frt = [=]() { this->handleRoot(); };
124 pWebServer->on(
"/", frt);
126 pWebServer->on(
"/inline",
127 [&]() { pWebServer->send(200,
"text/plain",
"this works as well"); });
129 pWebServer->on(
"/result", [&]() {
130 String ssid = pWebServer->arg(
"ssid");
131 String hostname = pWebServer->arg(
"hostname");
132 String response =
"{\"ssid\": \"" + ssid +
"\", \"hostname\": \"" + hostname +
"\"}";
133 pWebServer->send(200,
"text/plain", response.c_str());
134 pSched->publish(
"webserver/data", response);
137 auto fnf = [=]() { this->handleFileSystem(); };
138 pWebServer->onNotFound(fnf);
143 pWebServer->handleClient();
150 void subsMsg(String topic, String msg, String originator) {
152 if (topic ==
"net/network") {
153 JSONVar jsonMsg = JSON.parse(msg);
154 String state = (
const char *)jsonMsg[
"state"];
155 if (state ==
"connected") {
158 MDNS.begin(
"esp8266");
The muwerk namespace.
Definition: mqtt.h:21