From 9fa610c7bfc28708e7322a0f4ebff8b7f3e709c2 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Tue, 5 Aug 2025 20:40:30 +0200 Subject: [PATCH 1/2] add support for web server --- gateway/include/devices.h | 4 +++- gateway/include/huzzah.h | 3 +++ gateway/include/webserver.h | 45 +++++++++++++++++++++++++++++++++++++ gateway/platformio.ini | 1 + 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 gateway/include/webserver.h diff --git a/gateway/include/devices.h b/gateway/include/devices.h index fad2dd5..66097ea 100644 --- a/gateway/include/devices.h +++ b/gateway/include/devices.h @@ -113,6 +113,8 @@ struct EasyHomeSwitch : Switch { } }; +auto switchHomebox = new PollinSwitch{"00011", 4, "homebox"}; + Command* commands[] = { HaESP::restartButton() .asDevice(gatewayDevice) @@ -140,7 +142,7 @@ Command* commands[] = { new PollinSwitch{"00001", 2, "Fire Tv", "Living room"}, new PollinSwitch{"00001", 3, "Diningroom player", "Dining room"}, new PollinSwitch{"00001", 4}, - new PollinSwitch{"00011", 4, "homebox"}, + switchHomebox, new PollinSwitch{"11111", 4, "Train", "Playroom"} }; diff --git a/gateway/include/huzzah.h b/gateway/include/huzzah.h index 33e3b52..bc79158 100644 --- a/gateway/include/huzzah.h +++ b/gateway/include/huzzah.h @@ -11,6 +11,7 @@ Scheduler ts; #include "mqtt.h" #include "ota.h" #include "wifi.h" +#include "webserver.h" namespace Board { @@ -46,10 +47,12 @@ namespace Board { [] { Ota::tLoop.enable(); Mqtt::tReConnect.enable(); + WebServer::setup(); }, [] { Ota::tLoop.disable(); Mqtt::tReConnect.disable(); + WebServer::stop(); } ); Ota::setup( diff --git a/gateway/include/webserver.h b/gateway/include/webserver.h new file mode 100644 index 0000000..2d86a1f --- /dev/null +++ b/gateway/include/webserver.h @@ -0,0 +1,45 @@ +#ifdef ESP32 +#include +#elif defined(ESP8266) +#include +#endif +#include +#include "devices.h" + +AsyncWebServer server(80); + +namespace WebServer { + void notFound(AsyncWebServerRequest *request) { + request->send(404, "text/plain", "Not found"); + } + + void setup() { + + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { + request->send(200, "text/plain", "Hello, world"); + }); + + server.on("/switch/homebox", HTTP_GET, [](AsyncWebServerRequest *request) { + if (request->hasParam("state")) { + AsyncWebParameter* switchState = request->getParam("state"); + switchHomebox->onCommand(switchState->value().c_str()); + request->send(200, "text/plain", switchState->value().c_str()); + } else { + request->send(200, "text/plain", "No parameters provided"); + } + }); + + server.on("/restart", HTTP_GET, [](AsyncWebServerRequest *request) { + request->send(200, "text/plain"); + ESP.restart(); + }); + + server.onNotFound(notFound); + + server.begin(); + } + + void stop() { + server.end(); + } +} diff --git a/gateway/platformio.ini b/gateway/platformio.ini index 6f8eb3f..2a9e9d4 100644 --- a/gateway/platformio.ini +++ b/gateway/platformio.ini @@ -35,6 +35,7 @@ lib_deps = arkhipenko/TaskScheduler@^3.8.5 https://git.hodos.ro/libraries/ha-mqtt.git@^1.7.0 https://git.hodos.ro/libraries/wifi.git@^2.0.0 + esphome/ESPAsyncWebServer-esphome@^3.4.0 upload_port = 192.168.6.161 upload_protocol = espota upload_flags = From e80b4090e6795629cd6f4fb7b8c05e999778c51f Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sat, 6 Sep 2025 22:05:47 +0200 Subject: [PATCH 2/2] use web server from library --- gateway/include/devices.h | 4 +--- gateway/include/huzzah.h | 1 + gateway/include/webserver.h | 45 ------------------------------------- gateway/platformio.ini | 3 ++- 4 files changed, 4 insertions(+), 49 deletions(-) delete mode 100644 gateway/include/webserver.h diff --git a/gateway/include/devices.h b/gateway/include/devices.h index 66097ea..fad2dd5 100644 --- a/gateway/include/devices.h +++ b/gateway/include/devices.h @@ -113,8 +113,6 @@ struct EasyHomeSwitch : Switch { } }; -auto switchHomebox = new PollinSwitch{"00011", 4, "homebox"}; - Command* commands[] = { HaESP::restartButton() .asDevice(gatewayDevice) @@ -142,7 +140,7 @@ Command* commands[] = { new PollinSwitch{"00001", 2, "Fire Tv", "Living room"}, new PollinSwitch{"00001", 3, "Diningroom player", "Dining room"}, new PollinSwitch{"00001", 4}, - switchHomebox, + new PollinSwitch{"00011", 4, "homebox"}, new PollinSwitch{"11111", 4, "Train", "Playroom"} }; diff --git a/gateway/include/huzzah.h b/gateway/include/huzzah.h index bc79158..60b31ad 100644 --- a/gateway/include/huzzah.h +++ b/gateway/include/huzzah.h @@ -60,6 +60,7 @@ namespace Board { Mqtt::publishCleanupConfig(); delay(2000); Mqtt::disconnect(); + WebServer::stop(); }); tReadCommand.enable(); diff --git a/gateway/include/webserver.h b/gateway/include/webserver.h deleted file mode 100644 index 2d86a1f..0000000 --- a/gateway/include/webserver.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifdef ESP32 -#include -#elif defined(ESP8266) -#include -#endif -#include -#include "devices.h" - -AsyncWebServer server(80); - -namespace WebServer { - void notFound(AsyncWebServerRequest *request) { - request->send(404, "text/plain", "Not found"); - } - - void setup() { - - server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { - request->send(200, "text/plain", "Hello, world"); - }); - - server.on("/switch/homebox", HTTP_GET, [](AsyncWebServerRequest *request) { - if (request->hasParam("state")) { - AsyncWebParameter* switchState = request->getParam("state"); - switchHomebox->onCommand(switchState->value().c_str()); - request->send(200, "text/plain", switchState->value().c_str()); - } else { - request->send(200, "text/plain", "No parameters provided"); - } - }); - - server.on("/restart", HTTP_GET, [](AsyncWebServerRequest *request) { - request->send(200, "text/plain"); - ESP.restart(); - }); - - server.onNotFound(notFound); - - server.begin(); - } - - void stop() { - server.end(); - } -} diff --git a/gateway/platformio.ini b/gateway/platformio.ini index 2a9e9d4..9280216 100644 --- a/gateway/platformio.ini +++ b/gateway/platformio.ini @@ -33,7 +33,8 @@ framework = arduino lib_deps = ${env.lib_deps} arkhipenko/TaskScheduler@^3.8.5 - https://git.hodos.ro/libraries/ha-mqtt.git@^1.7.0 + https://git.hodos.ro/libraries/ha-mqtt.git@^1.8.0 + https://git.hodos.ro/libraries/ha-webserver.git@^0.1.0 https://git.hodos.ro/libraries/wifi.git@^2.0.0 esphome/ESPAsyncWebServer-esphome@^3.4.0 upload_port = 192.168.6.161