From d9be78d7382a6117efe04399191c2b246a731b11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Tue, 7 Jan 2025 09:18:18 +0100 Subject: [PATCH] http --- platformio.ini | 1 + src/patrix/http.cpp | 27 +++++++++++++++++++++++++++ src/patrix/http.h | 6 ++++++ src/patrix/system.cpp | 10 ++++++++++ src/patrix/system.h | 6 ++++++ src/patrix/wifi.cpp | 10 ++++++++++ src/patrix/wifi.h | 2 ++ 7 files changed, 62 insertions(+) create mode 100644 src/patrix/http.cpp create mode 100644 src/patrix/http.h create mode 100644 src/patrix/system.cpp create mode 100644 src/patrix/system.h diff --git a/platformio.ini b/platformio.ini index cf9380b..7ad557e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -10,6 +10,7 @@ lib_deps = milesburton/DallasTemperature https://github.com/adafruit/MAX6675-library paulstoffregen/OneWire bblanchon/ArduinoJson + https://github.com/me-no-dev/ESPAsyncWebServer.git upload_port = /dev/ttyUSB0 upload_speed = 921600 monitor_port = /dev/ttyUSB0 diff --git a/src/patrix/http.cpp b/src/patrix/http.cpp new file mode 100644 index 0000000..1b5a1e2 --- /dev/null +++ b/src/patrix/http.cpp @@ -0,0 +1,27 @@ +#include "http.h" + +#include + +#include "log.h" +#include "system.h" + +AsyncWebServer server(80); + +void httpIndex(AsyncWebServerRequest *request) { + request->send(200, "text/plain", R"(reboot)"); +} + +void httpReboot(AsyncWebServerRequest *request) { + info("Rebooting..."); + request->redirect("/"); + request->client()->close(); + yield(); + + restart(); +} + +void httpSetup() { + server.on("/", HTTP_GET, httpIndex); + server.on("/reboot", HTTP_GET, httpReboot); + server.begin(); +} diff --git a/src/patrix/http.h b/src/patrix/http.h new file mode 100644 index 0000000..abcba1f --- /dev/null +++ b/src/patrix/http.h @@ -0,0 +1,6 @@ +#ifndef HTTP_H +#define HTTP_H + +void httpSetup(); + +#endif diff --git a/src/patrix/system.cpp b/src/patrix/system.cpp new file mode 100644 index 0000000..e8a93d5 --- /dev/null +++ b/src/patrix/system.cpp @@ -0,0 +1,10 @@ +#include "system.h" + +#include + +#include "wifi.h" + +void restart() { + wifiOff(); + ESP.restart(); +} diff --git a/src/patrix/system.h b/src/patrix/system.h new file mode 100644 index 0000000..267e9a0 --- /dev/null +++ b/src/patrix/system.h @@ -0,0 +1,6 @@ +#ifndef SYSTEM_H +#define SYSTEM_H + +void restart(); + +#endif diff --git a/src/patrix/wifi.cpp b/src/patrix/wifi.cpp index 8d2cdb4..2823141 100644 --- a/src/patrix/wifi.cpp +++ b/src/patrix/wifi.cpp @@ -5,6 +5,8 @@ #define WIFI_TIMEOUT_MILLIS 10000 +auto wifiEnabled = true; + auto wifiConnected = false; auto wifiTryMillis = 0UL; @@ -52,6 +54,11 @@ void wifiSetupOTA() { ArduinoOTA.begin(); } +void wifiOff() { + info("wifi disabled"); + wifiEnabled = false; +} + void wifiLoop() { const auto currentState = WiFi.localIP() != 0; if (wifiConnected != currentState) { @@ -65,6 +72,9 @@ void wifiLoop() { WiFi.disconnect(); } } else if (!wifiConnected) { + if (!wifiEnabled) { + return; + } if (wifiTryMillis == 0 || millis() - wifiTryMillis >= WIFI_TIMEOUT_MILLIS) { wifiTryMillis = millis(); WiFiClass::hostname(wifiHost); diff --git a/src/patrix/wifi.h b/src/patrix/wifi.h index 37754af..074d5c9 100644 --- a/src/patrix/wifi.h +++ b/src/patrix/wifi.h @@ -1,6 +1,8 @@ #ifndef WIFI_H #define WIFI_H +void wifiOff(); + void wifiLoop(); bool isWiFiConnected();