munet Network Library for muwerk Scheduler
A muwerk network library supporting WiFi, NTP, OTA and MQTT for ESP8266 and ESP32 and serial links for all platforms
Loading...
Searching...
No Matches
web.h
1// web.h
2#pragma once
3
4#if defined(__ESP__)
5
6#include "ustd_platform.h"
7
8#ifdef __ESP32__
9#include <WiFiClient.h>
10#include <WebServer.h>
11#include <ESPmDNS.h>
12#else
13#include <WiFiClient.h>
14#include <ESP8266WebServer.h>
15#include <ESP8266mDNS.h>
16#endif
17
18#include "ustd_array.h"
19#include "ustd_map.h"
20#include "scheduler.h"
21
22namespace ustd {
23class Web {
24 public:
25 Scheduler *pSched;
26 int tID;
27 bool netUp = false;
28 bool webUp = false;
29 String webServer;
30#ifdef __ESP32__
31 WebServer *pWebServer;
32#else
33 ESP8266WebServer *pWebServer;
34#endif
35
36 Web() {
37 webServer = "";
38 }
39
40 ~Web() {
41 }
42
43 void begin(Scheduler *_pSched) {
44 pSched = _pSched;
45#ifdef __USE_SPIFFS_FS__
46 SPIFFS.begin();
47#else
48 LittleFS.begin();
49#endif
50
51#ifdef __ESP32__
52 pWebServer = new WebServer(80);
53#else
54 pWebServer = new ESP8266WebServer(80);
55#endif
56 netUp = false;
57
58 auto ft = [=]() { this->loop(); };
59 tID = pSched->add(ft, "web");
60
61 auto fnall = [=](String topic, String msg, String originator) {
62 this->subsMsg(topic, msg, originator);
63 };
64 pSched->subscribe(tID, "net/network", fnall);
65
66 pSched->publish("net/network/get");
67 // pSched->publish("net/services/webserver/get");
68 }
69
70 void handleRoot() {
71 handleFileSystem();
72 }
73 String getContentType(String fileName) {
74 if (fileName.endsWith(".html"))
75 return "text/html";
76 if (fileName.endsWith(".css"))
77 return "text/css";
78 if (fileName.endsWith(".png"))
79 return "image/png";
80 if (fileName.endsWith(".js"))
81 return "application/javascript";
82 if (fileName.endsWith(".ico"))
83 return "image/x-icon";
84 return "text/plain";
85 }
86
87 void handleFileSystem() {
88 String fileName = pWebServer->uri();
89 if (fileName == "/")
90 fileName = "/index.html";
91 String contentType = getContentType(fileName);
92#ifdef __USE_SPIFFS_FS__
93 if (SPIFFS.exists(fileName)) { // If the file exists
94 fs::File f = SPIFFS.open(fileName, "r"); // Open it
95#else
96 if (LittleFS.exists(fileName)) { // If the file exists
97 fs::File f = LittleFS.open(fileName, "r"); // Open it
98#endif
99 /*size_t sent = */ pWebServer->streamFile(f, contentType); // And send it to the client
100 f.close(); // Then close the file again
101 return;
102 } else {
103 handleNotFound();
104 }
105 }
106
107 void handleNotFound() {
108 String message = "File Not Found\n\n";
109 message += "URI: ";
110 message += pWebServer->uri();
111 message += "\nMethod: ";
112 message += (pWebServer->method() == HTTP_GET) ? "GET" : "POST";
113 message += "\nArguments: ";
114 message += pWebServer->args();
115 message += "\n";
116 for (uint8_t i = 0; i < pWebServer->args(); i++) {
117 message += " " + pWebServer->argName(i) + ": " + pWebServer->arg(i) + "\n";
118 }
119 pWebServer->send(404, "text/plain", message);
120 }
121
122 void initHandles() {
123 auto frt = [=]() { this->handleRoot(); };
124 pWebServer->on("/", frt);
125
126 pWebServer->on("/inline",
127 [&]() { pWebServer->send(200, "text/plain", "this works as well"); });
128
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);
135 });
136
137 auto fnf = [=]() { this->handleFileSystem(); };
138 pWebServer->onNotFound(fnf);
139 }
140
141 void loop() {
142 if (netUp) {
143 pWebServer->handleClient();
144#ifndef __ESP32__
145 MDNS.update();
146#endif
147 }
148 }
149
150 void subsMsg(String topic, String msg, String originator) {
151
152 if (topic == "net/network") {
153 JSONVar jsonMsg = JSON.parse(msg);
154 String state = (const char *)jsonMsg["state"]; // root["state"];
155 if (state == "connected") {
156 if (!webUp) {
157 netUp = true;
158 MDNS.begin("esp8266");
159 pWebServer->begin();
160 initHandles();
161 webUp = true;
162 }
163 } else {
164 netUp = false;
165 }
166 }
167 }
168}; // Web
169
170} // namespace ustd
171
172#endif // defined(__ESP__)
The muwerk namespace.
Definition: mqtt.h:21