Automatic reboot when MQTT is lost (#1058)

* Fixing board type overwrite, zmartcharge default issues and disabling entsoe

* Fixed Zmartcharge configuration issue

* Option to auto reboot if MQTT connection is lost
This commit is contained in:
Gunnar Skjold
2025-11-06 18:26:40 +01:00
committed by GitHub
parent eefbc08222
commit ffd8d46f2e
14 changed files with 81 additions and 44 deletions

View File

@@ -24,22 +24,20 @@ class AmsMqttHandler {
public:
#if defined(AMS_REMOTE_DEBUG)
AmsMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf, AmsFirmwareUpdater* updater) {
#else
AmsMqttHandler(MqttConfig& mqttConfig, Stream* debugger, char* buf) {
#endif
this->mqttConfig = mqttConfig;
this->mqttConfigChanged = true;
this->debugger = debugger;
this->json = buf;
this->updater = updater;
mqtt.dropOverflow(true);
pubTopic = String(mqttConfig.publishTopic);
subTopic = String(mqttConfig.subscribeTopic);
if(subTopic.isEmpty()) subTopic = pubTopic+"/command";
};
#else
AmsMqttHandler(MqttConfig& mqttConfig, Stream* debugger, char* buf) {
this->mqttConfig = mqttConfig;
this->mqttConfigChanged = true;
this->debugger = debugger;
this->json = buf;
mqtt.dropOverflow(true);
};
#endif
void setCaVerification(bool);
void setConfig(MqttConfig& mqttConfig);
@@ -84,6 +82,10 @@ protected:
char* json;
uint16_t BufferSize = 2048;
uint64_t lastStateUpdate = 0;
uint64_t lastSuccessfulLoop = 0;
String pubTopic;
String subTopic;
AmsFirmwareUpdater* updater = NULL;
};

View File

@@ -8,6 +8,7 @@
#include "FirmwareVersion.h"
#include "AmsStorage.h"
#include "LittleFS.h"
#include "Uptime.h"
void AmsMqttHandler::setCaVerification(bool caVerification) {
this->caVerification = caVerification;
@@ -165,7 +166,20 @@ bool AmsMqttHandler::connected() {
}
bool AmsMqttHandler::loop() {
bool ret = mqtt.loop();
uint64_t now = millis64();
bool ret = mqtt.connected() && mqtt.loop();
if(ret) {
lastSuccessfulLoop = now;
} else if(mqttConfig.rebootMinutes > 0) {
if(now - lastSuccessfulLoop > (uint64_t) mqttConfig.rebootMinutes * 60000) {
// Reboot the device if the MQTT connection is lost for too long
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("MQTT connection lost for over %d minutes, rebooting device\n"), mqttConfig.rebootMinutes);
ESP.restart();
}
}
delay(10); // Needed to preserve power. After adding this, the voltage is super smooth on a HAN powered device
yield();
#if defined(ESP32)