diff --git a/src/patrix/Patrix.cpp b/src/patrix/Patrix.cpp index 199f364..8382aaf 100644 --- a/src/patrix/Patrix.cpp +++ b/src/patrix/Patrix.cpp @@ -1,6 +1,7 @@ #include #include "Patrix.h" #include "http.h" +#include "system.h" void setup() { Log.info("Startup."); @@ -13,6 +14,7 @@ void setup() { } void loop() { + systemLoop(); wifiLoop(); mqttLoop(); if (isBootDelayComplete()) { @@ -21,6 +23,5 @@ void loop() { httpSetup(); } patrixLoop(); - httpLoop(); } } diff --git a/src/patrix/http.cpp b/src/patrix/http.cpp index 69320ad..c1af68a 100644 --- a/src/patrix/http.cpp +++ b/src/patrix/http.cpp @@ -1,12 +1,10 @@ #include "http.h" -#include "mqtt.h" +#include "system.h" #include AsyncWebServer server(80); -bool httpDoRestart = false; - void httpNotFound(AsyncWebServerRequest *request) { if (request->method() == HTTP_OPTIONS) { request->send(200); @@ -16,7 +14,7 @@ void httpNotFound(AsyncWebServerRequest *request) { } void httpRestart([[maybe_unused]] AsyncWebServerRequest *request) { - httpDoRestart = true; + systemRequestRestart(); request->send(200, "application/json", "true"); } @@ -30,10 +28,6 @@ void httpSetup() { server.begin(); } -void httpLoop() { - if (httpDoRestart) { - Log.info("Restarting..."); - delay(500); - EspClass::restart(); - } -} +void httpStop() { + server.end(); +} \ No newline at end of file diff --git a/src/patrix/http.h b/src/patrix/http.h index 9225913..761b7d9 100644 --- a/src/patrix/http.h +++ b/src/patrix/http.h @@ -7,6 +7,6 @@ extern AsyncWebServer server; void httpSetup(); -void httpLoop(); +void httpStop(); #endif diff --git a/src/patrix/mqtt.cpp b/src/patrix/mqtt.cpp index 4ce732b..87095a1 100644 --- a/src/patrix/mqtt.cpp +++ b/src/patrix/mqtt.cpp @@ -3,6 +3,7 @@ #include #include "PubSubClient.h" +#include "system.h" #include WiFiClient client; @@ -29,19 +30,15 @@ void mqttCallback(char *topic, const uint8_t *payload, unsigned int length) { Log.info("HELP"); Log.info(" %s", "help"); Log.info(" %s", "info"); - Log.info(" %s", "reboot"); + Log.info(" %s", "restart"); } else if (strcmp(message, "info") == 0) { Log.info("INFO"); Log.info(" %-10s %s", "SSID:", WiFi.SSID().c_str()); Log.info(" %-10s %s", "IP:", WiFi.localIP().toString().c_str()); Log.info(" %-10s %d", "RSSI:", WiFi.RSSI()); Log.info(" %-10s %s", "uptime:", uptimeString().c_str()); - } else if (strcmp(message, "reboot") == 0) { - Log.info("rebooting..."); - delay(500); - mqtt.disconnect(); - delay(500); - EspClass::restart(); + } else if (strcmp(message, "restart") == 0) { + systemRequestRestart(); } } @@ -62,6 +59,10 @@ void mqttLoop() { } } +void mqttDisconnect() { + mqtt.disconnect(); +} + void mqttPublishValue(const String& name, const int32_t value, const char *unit) { mqttPublishValue(name, String(value), unit); } diff --git a/src/patrix/mqtt.h b/src/patrix/mqtt.h index 407c919..fa9b731 100644 --- a/src/patrix/mqtt.h +++ b/src/patrix/mqtt.h @@ -28,6 +28,8 @@ extern const String cmdTopic; void mqttLoop(); +void mqttDisconnect(); + void mqttPublishValue(const String& name, int32_t value, const char *unit); void mqttPublishValue(const String& name, int64_t value, const char *unit); diff --git a/src/patrix/system.cpp b/src/patrix/system.cpp new file mode 100644 index 0000000..2f67e4b --- /dev/null +++ b/src/patrix/system.cpp @@ -0,0 +1,24 @@ +#include "system.h" + +#include "mqtt.h" +#include "http.h" + +bool systemRestart = false; + +void systemRequestRestart() { + systemRestart = true; +} + +void systemLoop() { + if (systemRestart) { + Log.info("Restarting..."); + delay(250); + + httpStop(); + mqttDisconnect(); + wifiDisconnect(); + + delay(250); + EspClass::restart(); + } +} diff --git a/src/patrix/system.h b/src/patrix/system.h new file mode 100644 index 0000000..9801521 --- /dev/null +++ b/src/patrix/system.h @@ -0,0 +1,8 @@ +#ifndef HELLIGKEIT_SYSTEM_H +#define HELLIGKEIT_SYSTEM_H + +void systemRequestRestart(); + +void systemLoop(); + +#endif diff --git a/src/patrix/wifi.cpp b/src/patrix/wifi.cpp index 322e790..e3b580f 100644 --- a/src/patrix/wifi.cpp +++ b/src/patrix/wifi.cpp @@ -28,15 +28,20 @@ void wifiConnect() { ArduinoOTA.onError([](const ota_error_t error) { const char *name; switch (error) { - case OTA_AUTH_ERROR: name = "AUTH"; + case OTA_AUTH_ERROR: + name = "AUTH"; break; - case OTA_BEGIN_ERROR: name = "BEGIN"; + case OTA_BEGIN_ERROR: + name = "BEGIN"; break; - case OTA_CONNECT_ERROR: name = "CONNECT"; + case OTA_CONNECT_ERROR: + name = "CONNECT"; break; - case OTA_RECEIVE_ERROR: name = "RECEIVE"; + case OTA_RECEIVE_ERROR: + name = "RECEIVE"; break; - case OTA_END_ERROR: name = "END"; + case OTA_END_ERROR: + name = "END"; break; default: name = "[???]"; @@ -133,6 +138,10 @@ void wifiLoop() { timeLoop(); } +void wifiDisconnect() { + WiFi.disconnect(); +} + String getTimeString() { time_t now; time(&now); diff --git a/src/patrix/wifi.h b/src/patrix/wifi.h index b0c74d5..b29bb2c 100644 --- a/src/patrix/wifi.h +++ b/src/patrix/wifi.h @@ -7,6 +7,8 @@ void wifiConnect(); void wifiLoop(); +void wifiDisconnect(); + String getTimeString(); bool isWifiConnected();