fix: stop watchdogs firing on unsigned wrap of stale timestamps
The MQTT task captured now at the top of its iteration, then a publish in the same iteration stored a newer s_lastPublishOkMs. now - last then underflowed to ~49 days and tripped the 1 h reboot watchdog (seen as 'No successful publish for 4294967s, rebooting' right after a publish). - sinceMs(): elapsed time from a fresh millis(), clamped at 0 - Use it for the MQTT force-reconnect, reboot and BLE stale watchdogs - Use it for data_age_s and ble_age_s so the payload cannot wrap either - Log the elapsed value on each watchdog action
This commit is contained in:
parent
0c8390eee1
commit
5629e05d97
28
src/main.cpp
28
src/main.cpp
|
|
@ -135,6 +135,14 @@ static uint32_t s_lastDiscoveryAttemptMs = 0;
|
|||
|
||||
// ── Helpers ──────────────────────────────────────────────────────────────────
|
||||
|
||||
// Elapsed ms since t, using a fresh millis(). Timestamps are set on the other
|
||||
// core / later in the same iteration, so they can be newer than a cached
|
||||
// `now`; plain unsigned subtraction would wrap to ~49 days and trip watchdogs.
|
||||
static uint32_t sinceMs(uint32_t t) {
|
||||
uint32_t n = millis();
|
||||
return n >= t ? n - t : 0;
|
||||
}
|
||||
|
||||
static bool hexToBin(const char* hex, uint8_t* out, size_t outLen) {
|
||||
if (strlen(hex) != outLen * 2) return false;
|
||||
for (size_t i = 0; i < outLen; i++) {
|
||||
|
|
@ -353,7 +361,7 @@ static bool publishData(const MpptData& d, uint32_t now) {
|
|||
doc["external_load_w"] = serialized(String(d.externalLoad * d.batteryVoltage, 1));
|
||||
// Seconds since the last decoded BLE packet. Grows when BLE stops
|
||||
// delivering, which distinguishes a BLE problem from an MQTT problem.
|
||||
doc["data_age_s"] = s_bleSeen ? (now - s_lastBleMs) / 1000UL : 0;
|
||||
doc["data_age_s"] = s_bleSeen ? sinceMs(s_lastBleMs) / 1000UL : 0;
|
||||
|
||||
char buf[320];
|
||||
serializeJson(doc, buf);
|
||||
|
|
@ -370,7 +378,7 @@ static bool publishData(const MpptData& d, uint32_t now) {
|
|||
// ── Diagnostics ──────────────────────────────────────────────────────────────
|
||||
|
||||
static void publishDiagnostics(uint32_t now) {
|
||||
uint32_t bleAgeS = s_bleSeen ? (now - s_lastBleMs) / 1000UL : 999999UL;
|
||||
uint32_t bleAgeS = s_bleSeen ? sinceMs(s_lastBleMs) / 1000UL : 999999UL;
|
||||
|
||||
char buf[448];
|
||||
snprintf(buf, sizeof(buf),
|
||||
|
|
@ -456,19 +464,19 @@ void mqttTask(void* param) {
|
|||
// MQTT thinks it is connected but nothing gets out -> half-open socket.
|
||||
// Keep the actual last-success timestamp untouched so the reboot
|
||||
// watchdog below still fires if reconnecting does not help.
|
||||
if (mqttUp && now - s_lastPublishOkMs >= MQTT_WATCHDOG_MS &&
|
||||
now - lastForceReconnectMs >= MQTT_WATCHDOG_MS) {
|
||||
if (mqttUp && sinceMs(s_lastPublishOkMs) >= MQTT_WATCHDOG_MS &&
|
||||
sinceMs(lastForceReconnectMs) >= MQTT_WATCHDOG_MS) {
|
||||
Serial.printf("[MQTT] No successful publish for %lus, forcing reconnect\n",
|
||||
(unsigned long)((now - s_lastPublishOkMs) / 1000UL));
|
||||
(unsigned long)(sinceMs(s_lastPublishOkMs) / 1000UL));
|
||||
s_mqtt.disconnect();
|
||||
lastForceReconnectMs = now;
|
||||
lastForceReconnectMs = millis();
|
||||
lastDataMs = 0; // publish the newest snapshot right after reconnect
|
||||
}
|
||||
|
||||
// Total comms failure for a long time -> the ESP32 itself is wedged.
|
||||
if (now - s_lastPublishOkMs >= REBOOT_WATCHDOG_MS) {
|
||||
if (sinceMs(s_lastPublishOkMs) >= REBOOT_WATCHDOG_MS) {
|
||||
Serial.printf("[WD] No successful publish for %lus, rebooting\n",
|
||||
(unsigned long)((now - s_lastPublishOkMs) / 1000UL));
|
||||
(unsigned long)(sinceMs(s_lastPublishOkMs) / 1000UL));
|
||||
Serial.flush();
|
||||
ESP.restart();
|
||||
}
|
||||
|
|
@ -606,9 +614,9 @@ void loop() {
|
|||
|
||||
// BLE stopped delivering while MQTT/WiFi stay healthy. Only applies once BLE
|
||||
// has worked, so a wrong MAC or an absent MPPT never causes a reboot loop.
|
||||
if (s_bleSeen && now - s_lastBleMs >= BLE_STALE_REBOOT_MS) {
|
||||
if (s_bleSeen && sinceMs(s_lastBleMs) >= BLE_STALE_REBOOT_MS) {
|
||||
Serial.printf("[BLE] No valid packet for %lus, rebooting\n",
|
||||
(unsigned long)((now - s_lastBleMs) / 1000UL));
|
||||
(unsigned long)(sinceMs(s_lastBleMs) / 1000UL));
|
||||
Serial.flush();
|
||||
ESP.restart();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue