From bec4cc7358e9969807718aa9139a4b9397cf4146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Fri, 10 Jan 2025 12:14:02 +0100 Subject: [PATCH] proper reboot --- src/core/http.cpp | 7 ++++++- src/core/http.h | 3 ++- src/core/log.cpp | 23 ++++++++++++++++++++--- src/core/wifi.cpp | 3 ++- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/core/http.cpp b/src/core/http.cpp index 6dc0925..1ea1ffd 100644 --- a/src/core/http.cpp +++ b/src/core/http.cpp @@ -122,7 +122,12 @@ void httpLoop() { ws.cleanupClients(); } -void websocketSend(const char *message) { +void httpStop() { + ws.closeAll(); + ws.enable(false); + server.end(); +} + void websocketSendAll(const char *message) { if (websocketStarted) { ws.textAll(message); diff --git a/src/core/http.h b/src/core/http.h index fc51b15..366a509 100644 --- a/src/core/http.h +++ b/src/core/http.h @@ -5,7 +5,8 @@ void httpSetup(); void httpLoop(); -void websocketSend(const char *message); +void httpStop(); + void websocketSendAll(const char *message); #endif diff --git a/src/core/log.cpp b/src/core/log.cpp index 92236e3..ccdf75a 100644 --- a/src/core/log.cpp +++ b/src/core/log.cpp @@ -7,6 +7,8 @@ #include "clock.h" #include "filesystem.h" +#include "http.h" +#include "wifi.h" void doLog(LogLevel level, const char *format, va_list args); @@ -18,6 +20,8 @@ void logSetup() { info("Startup"); } +auto rebootRequested = false; + void execute(const char *cmd) { if (strcmp(cmd, "help") == 0) { info("HELP"); @@ -38,9 +42,7 @@ void execute(const char *cmd) { info(" %-10s %s", "dns:", WiFi.dnsIP().toString().c_str()); } else if (strcmp(cmd, "reboot") == 0) { info("REBOOT"); - delay(1000); - yield(); - ESP.restart(); + rebootRequested = true; } else if (strcmp(cmd, "reconnect") == 0) { info("RECONNECT"); WiFi.disconnect(); @@ -57,7 +59,22 @@ void execute(const char *cmd) { } } +void doReboot() { + httpStop(); + wifiOff(); + LittleFS.end(); + yield(); + + delay(1000); + yield(); + + ESP.restart(); +} + void logLoop() { + if (rebootRequested) { + doReboot(); + } static char buffer[64]; static auto write = buffer; if (Serial.available() > 0) { diff --git a/src/core/wifi.cpp b/src/core/wifi.cpp index 200593e..d964f9e 100644 --- a/src/core/wifi.cpp +++ b/src/core/wifi.cpp @@ -57,6 +57,7 @@ void wifiSetupOTA() { void wifiOff() { info("wifi disabled"); wifiEnabled = false; + WiFi.disconnect(); } void wifiLoop() { @@ -64,7 +65,7 @@ void wifiLoop() { if (wifiConnected != currentState) { wifiConnected = currentState; if (wifiConnected) { - info("wifi connected: %s", WiFi.localIP().toString().c_str()); + info("wifi connected as %s", WiFi.localIP().toString().c_str()); wifiSetupOTA(); } else { warn("wifi disconnected");