diff --git a/src/main.cpp b/src/main.cpp index c7d0264..118bb7f 100644 --- a/src/main.cpp +++ b/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(); }