centralized system restart

This commit is contained in:
Patrick Haßel 2025-02-17 23:40:20 +01:00
parent 933ce71db4
commit b9d96d3f02
9 changed files with 66 additions and 25 deletions

View File

@ -1,6 +1,7 @@
#include <LittleFS.h> #include <LittleFS.h>
#include "Patrix.h" #include "Patrix.h"
#include "http.h" #include "http.h"
#include "system.h"
void setup() { void setup() {
Log.info("Startup."); Log.info("Startup.");
@ -13,6 +14,7 @@ void setup() {
} }
void loop() { void loop() {
systemLoop();
wifiLoop(); wifiLoop();
mqttLoop(); mqttLoop();
if (isBootDelayComplete()) { if (isBootDelayComplete()) {
@ -21,6 +23,5 @@ void loop() {
httpSetup(); httpSetup();
} }
patrixLoop(); patrixLoop();
httpLoop();
} }
} }

View File

@ -1,12 +1,10 @@
#include "http.h" #include "http.h"
#include "mqtt.h" #include "system.h"
#include <LittleFS.h> #include <LittleFS.h>
AsyncWebServer server(80); AsyncWebServer server(80);
bool httpDoRestart = false;
void httpNotFound(AsyncWebServerRequest *request) { void httpNotFound(AsyncWebServerRequest *request) {
if (request->method() == HTTP_OPTIONS) { if (request->method() == HTTP_OPTIONS) {
request->send(200); request->send(200);
@ -16,7 +14,7 @@ void httpNotFound(AsyncWebServerRequest *request) {
} }
void httpRestart([[maybe_unused]] AsyncWebServerRequest *request) { void httpRestart([[maybe_unused]] AsyncWebServerRequest *request) {
httpDoRestart = true; systemRequestRestart();
request->send(200, "application/json", "true"); request->send(200, "application/json", "true");
} }
@ -30,10 +28,6 @@ void httpSetup() {
server.begin(); server.begin();
} }
void httpLoop() { void httpStop() {
if (httpDoRestart) { server.end();
Log.info("Restarting..."); }
delay(500);
EspClass::restart();
}
}

View File

@ -7,6 +7,6 @@ extern AsyncWebServer server;
void httpSetup(); void httpSetup();
void httpLoop(); void httpStop();
#endif #endif

View File

@ -3,6 +3,7 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include "PubSubClient.h" #include "PubSubClient.h"
#include "system.h"
#include <WiFiClient.h> #include <WiFiClient.h>
WiFiClient client; WiFiClient client;
@ -29,19 +30,15 @@ void mqttCallback(char *topic, const uint8_t *payload, unsigned int length) {
Log.info("HELP"); Log.info("HELP");
Log.info(" %s", "help"); Log.info(" %s", "help");
Log.info(" %s", "info"); Log.info(" %s", "info");
Log.info(" %s", "reboot"); Log.info(" %s", "restart");
} else if (strcmp(message, "info") == 0) { } else if (strcmp(message, "info") == 0) {
Log.info("INFO"); Log.info("INFO");
Log.info(" %-10s %s", "SSID:", WiFi.SSID().c_str()); Log.info(" %-10s %s", "SSID:", WiFi.SSID().c_str());
Log.info(" %-10s %s", "IP:", WiFi.localIP().toString().c_str()); Log.info(" %-10s %s", "IP:", WiFi.localIP().toString().c_str());
Log.info(" %-10s %d", "RSSI:", WiFi.RSSI()); Log.info(" %-10s %d", "RSSI:", WiFi.RSSI());
Log.info(" %-10s %s", "uptime:", uptimeString().c_str()); Log.info(" %-10s %s", "uptime:", uptimeString().c_str());
} else if (strcmp(message, "reboot") == 0) { } else if (strcmp(message, "restart") == 0) {
Log.info("rebooting..."); systemRequestRestart();
delay(500);
mqtt.disconnect();
delay(500);
EspClass::restart();
} }
} }
@ -62,6 +59,10 @@ void mqttLoop() {
} }
} }
void mqttDisconnect() {
mqtt.disconnect();
}
void mqttPublishValue(const String& name, const int32_t value, const char *unit) { void mqttPublishValue(const String& name, const int32_t value, const char *unit) {
mqttPublishValue(name, String(value), unit); mqttPublishValue(name, String(value), unit);
} }

View File

@ -28,6 +28,8 @@ extern const String cmdTopic;
void mqttLoop(); void mqttLoop();
void mqttDisconnect();
void mqttPublishValue(const String& name, int32_t value, const char *unit); void mqttPublishValue(const String& name, int32_t value, const char *unit);
void mqttPublishValue(const String& name, int64_t value, const char *unit); void mqttPublishValue(const String& name, int64_t value, const char *unit);

24
src/patrix/system.cpp Normal file
View File

@ -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();
}
}

8
src/patrix/system.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef HELLIGKEIT_SYSTEM_H
#define HELLIGKEIT_SYSTEM_H
void systemRequestRestart();
void systemLoop();
#endif

View File

@ -28,15 +28,20 @@ void wifiConnect() {
ArduinoOTA.onError([](const ota_error_t error) { ArduinoOTA.onError([](const ota_error_t error) {
const char *name; const char *name;
switch (error) { switch (error) {
case OTA_AUTH_ERROR: name = "AUTH"; case OTA_AUTH_ERROR:
name = "AUTH";
break; break;
case OTA_BEGIN_ERROR: name = "BEGIN"; case OTA_BEGIN_ERROR:
name = "BEGIN";
break; break;
case OTA_CONNECT_ERROR: name = "CONNECT"; case OTA_CONNECT_ERROR:
name = "CONNECT";
break; break;
case OTA_RECEIVE_ERROR: name = "RECEIVE"; case OTA_RECEIVE_ERROR:
name = "RECEIVE";
break; break;
case OTA_END_ERROR: name = "END"; case OTA_END_ERROR:
name = "END";
break; break;
default: default:
name = "[???]"; name = "[???]";
@ -133,6 +138,10 @@ void wifiLoop() {
timeLoop(); timeLoop();
} }
void wifiDisconnect() {
WiFi.disconnect();
}
String getTimeString() { String getTimeString() {
time_t now; time_t now;
time(&now); time(&now);

View File

@ -7,6 +7,8 @@ void wifiConnect();
void wifiLoop(); void wifiLoop();
void wifiDisconnect();
String getTimeString(); String getTimeString();
bool isWifiConnected(); bool isWifiConnected();