feat(Telemetry,Networking,OTAManager,CommunicationRouter): diagnostics report — CPU temp, WiFi reconnects, OTA state, stack high-water mark
Console requested four more Health-tab metrics beyond F-054's free_heap/ping work. None needed 30s heartbeat freshness, so this adds a separate low-frequency report instead of growing the heartbeat further: published every 5 min to status/info (diagnostics_report) and queryable on-demand via a new telemetry.get_diagnostics command. Only CPU temperature needs local sampling (15s, via a min/max/avg RunningStats accumulator on Telemetry) — a thermal spike could occur and subside entirely within a 5-minute gap. The other three fields are monotonic/self-tracking (WiFi reconnect counter, FreeRTOS's own stack high-water mark, rarely-changing OTA state) so a single read at report time loses no information. Verified: pio run on all four board variants.
This commit is contained in:
@@ -65,6 +65,7 @@ Every feature — user-facing or significant internal mechanism — is listed he
|
||||
| F-053 | `telemetry.get_boot_history` command + fixed boot-log counter drift + crash detail in SD history | v2.1.3 | Stable |
|
||||
| F-054 | Console Health tab support — `free_heap` on heartbeat + `ping` round-trip timestamp echo | v2.1.4 | Stable |
|
||||
| F-055 | Fix: heap-exhaustion abort() under rapid MQTT command bursts — free-heap guard on publish + command intake | v2.1.4 | Stable |
|
||||
| F-056 | Diagnostics report — CPU temperature, WiFi reconnects, OTA state, stack high-water mark, published automatically every 5 min + on-demand via `telemetry.get_diagnostics` | v2.1.5 | Stable |
|
||||
|
||||
---
|
||||
|
||||
@@ -854,3 +855,48 @@ xtensa-esp32-elf-addr2line.exe -pfiaC -e .pio/build/vesper-std-v1/firmware.elf 0
|
||||
Verified: `pio run` on both `vesper-std-v1` and `agnus-v1`.
|
||||
|
||||
Subsystem: `src/Communication/Drivers/MQTTDriver/MQTTDriver.hpp`, `src/Communication/Drivers/MQTTDriver/MQTTDriver.cpp`, `src/Communication/Transport/MQTTTransport/MQTTTransport.cpp`
|
||||
|
||||
---
|
||||
|
||||
### F-056 — Diagnostics report: CPU temperature, WiFi reconnects, OTA state, stack high-water mark
|
||||
|
||||
Second console-requested addition to the Health tab (after F-054's `free_heap`/`ping` work). The console wanted four more metrics — CPU temperature, WiFi reconnect count, firmware/OTA state, and per-task stack headroom — but none of them belonged on the 30s heartbeat: the heartbeat fires forever, per device, at fleet scale, and none of these four need 30-second freshness. Instead, this adds a new low-frequency **diagnostics report**, published every 5 minutes to `status/info` (same transport pattern as F-052's `boot_report`) and additionally queryable on-demand via a new `telemetry.get_diagnostics` command.
|
||||
|
||||
**Local sampling vs. batch send — why they differ per metric.** The console's initial ask was "read everything every 5 minutes." That's fine for three of the four metrics but wrong for temperature:
|
||||
|
||||
| Metric | Behaviour between reports | Sampling strategy |
|
||||
|---|---|---|
|
||||
| CPU temperature | Fluctuates continuously — a thermal spike can be short-lived (e.g. a burst of relay activity) and gone by the time a slow report fires | Sampled locally every 15s ([`Telemetry::sampleDiagnosticsIfDue()`](../../src/Telemetry/Telemetry.cpp), called from `TelemetryTask`'s existing 1s loop), reduced to a `RunningStats` min/max/avg accumulator (O(1) per sample, no per-sample storage), reset after each 5-minute report |
|
||||
| WiFi reconnects | Monotonic counter — only ever increases | Single read at report time. No information is lost by infrequent reads; the counter already remembers every increment |
|
||||
| Stack high-water mark | `uxTaskGetStackHighWaterMark()` is itself a running low-water-mark maintained by FreeRTOS since the task started — it only ever goes down | Single read at report time — reading more often cannot reveal a lower value than the task's own internal tracking already has |
|
||||
| OTA state | Changes only around an actual update attempt (rare) | Single read at report time |
|
||||
|
||||
Only temperature needed the local-sampling treatment; over-engineering a sampling buffer for three values that are already monotonic/self-tracking would have added complexity with no benefit.
|
||||
|
||||
**1. CPU temperature.** [Telemetry.hpp](../../src/Telemetry/Telemetry.hpp)/[.cpp](../../src/Telemetry/Telemetry.cpp) add a `RunningStats` accumulator (`sum`/`min`/`max`/`count`, `addSample()`/`average()`/`reset()`) and `sampleDiagnosticsIfDue()`, which reads `temperatureRead()` (ESP32 Arduino core's built-in die-temperature sensor) every `DIAGNOSTICS_SAMPLE_INTERVAL_MS` (15s) and folds it into the accumulator. **Caveat carried into the wire format's docs:** the ESP32's internal sensor reads die temperature, not ambient, and is not a calibrated instrument — useful as a coarse "is this thing running hot" trend, not a precise reading. `logTemperature(float)` (the pre-existing stub noted in Telemetry's own README as "no persistent storage or analysis yet") is untouched — this is a new, separate mechanism, not an implementation of that stub.
|
||||
|
||||
**2. Stack high-water mark.** New static registry in `Telemetry` (`registerTaskForStackMonitoring(name, handle)` / `writeStackHighWaterMarks(JsonObject&)`), backed by a fixed 8-slot array (this codebase's established style — see `strikeCounters[16]` — avoids dynamic allocation). Only tasks that already keep a stored `TaskHandle_t` can be monitored this way; short-lived fire-and-forget tasks created with `xTaskCreate(..., NULL)` (`mqttRecon`, `mqttHB`, `mqttConnect`, `netRecon`, `PlayerCtrl`, `MelodyDL`, `ntpSync`) don't keep a handle and can't be queried. Registered at `begin()` time: `TelemetryTask` itself ([Telemetry.cpp](../../src/Telemetry/Telemetry.cpp)) and `HealthMonitor`'s monitoring task ([HealthMonitor.cpp](../../src/HealthMonitor/HealthMonitor.cpp)) — the two tasks F-051's stack-overflow incident specifically named. Reports bytes free (`uxTaskGetStackHighWaterMark()`'s words-remaining × `sizeof(StackType_t)`), not words, so the console doesn't need to know the platform's word size.
|
||||
|
||||
**3. WiFi reconnect count + last disconnect reason.** [Networking.hpp](../../src/Networking/Networking.hpp)/[.cpp](../../src/Networking/Networking.cpp) add `_reconnectCount` (incremented in `onWiFiConnected()`, guarded by `_bootSequenceComplete` so the initial boot-time connection isn't counted as a "reconnect" — only connects *after* that first one are) and `_lastDisconnectReason`/`_lastDisconnectUptimeMs`, captured in `networkEventHandler()`'s `ARDUINO_EVENT_WIFI_STA_DISCONNECTED` case via the event's `info.wifi_sta_disconnected.reason` field and Arduino-ESP32's own `WiFi.disconnectReasonName()` helper (already shipped in this SDK — no new lookup table needed). Both are in-memory only, reset on reboot by design: a fresh boot legitimately starts a new "how flaky has this session been" count, distinct from the lifetime boot counter (F-052) which already answers "how many times has this device restarted, ever."
|
||||
|
||||
**4. OTA / firmware state.** [OTAManager.hpp](../../src/OTAManager/OTAManager.hpp)/[.cpp](../../src/OTAManager/OTAManager.cpp) add `_lastCheckUptimeMs` (set at the top of `checkForUpdates(channel)`, the single call site both the scheduled and emergency check paths funnel through) and `errorCodeToString(ErrorCode)`. `OTAManager::getStatus()`, `getLastError()`, `getAvailableVersion()`, and `isUpdateAvailable()` already existed but were never wired into any command response before this — `firmware.status` (F-049) only surfaced `FirmwareValidator`'s rollback-safety data, a separate concern (see F-052's boot-counter table for why these two subsystems are kept deliberately distinct).
|
||||
|
||||
**Publishing.** [CommunicationRouter.cpp](../../src/Communication/CommunicationRouter/CommunicationRouter.cpp)'s `reportDiagnosticsIfDue()` — checked from the existing `loop()` (a `millis()`-diff check every main-loop iteration, same lightweight style as `Telemetry`'s own `RUNTIME_FLUSH_INTERVAL_MS` check, not a new FreeRTOS timer) — assembles all four into one payload and calls the existing `publishInfo("diagnostics_report", payload)`:
|
||||
```json
|
||||
{
|
||||
"type": "diagnostics_report",
|
||||
"payload": {
|
||||
"cpu_temp": { "avg": 47.1, "min": 44.8, "max": 52.8, "samples": 20 },
|
||||
"wifi_reconnects": { "lifetime_count": 2, "last_reason": "AUTH_EXPIRE", "last_at_uptime_ms": 3600000 },
|
||||
"ota": { "current_version": "2.7.3", "update_available": false, "last_check_uptime_ms": 180000, "last_error": "NONE" },
|
||||
"stack_high_water": { "TelemetryTask": 1024, "HealthMonitor": 890 }
|
||||
}
|
||||
}
|
||||
```
|
||||
`cpu_temp` is omitted entirely if no samples were taken (e.g. the very first report shortly after boot, before 15s has elapsed).
|
||||
|
||||
**On-demand access.** [TelemetryHandler.hpp](../../src/Communication/Handlers/TelemetryHandler/TelemetryHandler.hpp)/[.cpp](../../src/Communication/Handlers/TelemetryHandler/TelemetryHandler.cpp) gain `telemetry.get_diagnostics`, returning the identical shape (unwrapped, per the standard `CommandResult::ok(type, doc)` response envelope — no `"payload"` nesting, unlike the MQTT `status/info` publish). `TelemetryHandler`'s constructor now also takes `OTAManager&` and `Networking&` (previously `Telemetry&` only) — its single construction call site in `CommunicationRouter::registerHandlers()` was updated accordingly. Critically, the on-demand path does **not** reset the temperature accumulator (`Telemetry::writeTemperatureStats()` peeks without consuming) — it's a read, not a report cycle, so a console polling this command doesn't rob the next scheduled 5-minute report of its sampling window.
|
||||
|
||||
**Scope note (RTC/SD independence):** unlike F-054's `ping` addition, none of these four metrics depend on `#if HAS_RTC` or SD-card presence — `temperatureRead()`, `uxTaskGetStackHighWaterMark()`, WiFi events, and `OTAManager` are all available on every board variant. Verified via `pio run` on all four environments (`vesper-std-v1`, `agnus-v1`, `agnus-mini-v1`, `bespoke-rs485-v1`).
|
||||
|
||||
Subsystem: `src/Telemetry/Telemetry.hpp`, `src/Telemetry/Telemetry.cpp`, `src/Networking/Networking.hpp`, `src/Networking/Networking.cpp`, `src/OTAManager/OTAManager.hpp`, `src/OTAManager/OTAManager.cpp`, `src/HealthMonitor/HealthMonitor.cpp`, `src/Communication/CommunicationRouter/CommunicationRouter.hpp`, `src/Communication/CommunicationRouter/CommunicationRouter.cpp`, `src/Communication/Handlers/TelemetryHandler/TelemetryHandler.hpp`, `src/Communication/Handlers/TelemetryHandler/TelemetryHandler.cpp`
|
||||
|
||||
Reference in New Issue
Block a user