feat(MQTTDriver,SystemHandler): free_heap on heartbeat + ping RTT timestamp echo

Requested by the console-side agent building a device Health tab
(RSSI, ping latency, free heap, restart timeline graphs). Both changes
are additive and backward-compatible.

publishHeartbeat() now includes free_heap (ESP.getFreeHeap()) on the
existing 30s heartbeat, alongside rssi — previously free heap was only
visible once per boot via boot_report, a single point rather than a
trend.

handlePing() previously built a JsonDocument and never used it —
ping was a pure ack. It now echoes back an optional caller-supplied
"ts" (epoch-millis) unchanged, plus device_uptime_ms, so the caller
can compute round-trip latency as (time of receipt) - ts without
needing synchronized clocks. ts is optional on both request and
response, so existing callers doing a plain liveness check are
unaffected. Left the existing #if HAS_RTC scoping on SystemHandler.cpp
untouched, per the request — ping remains RTC-variant-only.

Verified: pio run on vesper-std-v1 and agnus-v1 (confirms the HAS_RTC
scoping around SystemHandler.cpp wasn't disturbed).

Documented in feature-catalog.md F-054, MQTTDriver/README.md, and
Communication/Handlers/README.md.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-16 12:32:51 +03:00
co-authored by Claude Sonnet 5
parent 957c283788
commit 8546604490
5 changed files with 43 additions and 4 deletions
+28
View File
@@ -63,6 +63,7 @@ Every feature — user-facing or significant internal mechanism — is listed he
| F-051 | Fix: TelemetryTask/HealthMonitor stack overflow — stack bump + deferred MQTT/SD logging queue | v2.0.3 | Stable |
| F-052 | Consolidated lifetime boot counter + coredump crash detail, published automatically over MQTT | v2.1.3 | Stable |
| 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 |
---
@@ -794,3 +795,30 @@ Same shape as the MQTT `boot_report`'s `crash` object (see F-052) — present on
Verified: `pio run` on both `vesper-std-v1` and `agnus-v1` (no-SD variant, where `getBootHistory()` correctly returns `false`/the command returns its error response).
Subsystem: `src/Telemetry/Telemetry.hpp`, `src/Telemetry/Telemetry.cpp`, `src/Communication/Handlers/TelemetryHandler/TelemetryHandler.hpp`, `src/Communication/Handlers/TelemetryHandler/TelemetryHandler.cpp`
---
### F-054 — Console Health tab support: `free_heap` on heartbeat + `ping` round-trip timestamp echo
Requested by the console-side agent building a device Health tab (RSSI, ping latency, free heap, and restart-timeline graphs). Two small, additive, backward-compatible changes — no existing field renamed or removed.
**1. `free_heap` added to the 30s MQTT heartbeat.** [MQTTDriver.cpp](../../src/Communication/Drivers/MQTTDriver/MQTTDriver.cpp)'s `publishHeartbeat()` now includes `payload["free_heap"] = ESP.getFreeHeap()` alongside the existing `rssi` sample. Before this, free heap was only visible once per boot (via F-052's `boot_report`) — a single data point per device lifetime segment, not a trend. Every device already publishes RSSI on this same 30s cadence; heap costs nothing extra to piggyback on the same message. Heartbeat payload is now: `device_id`, `firmware_version`, `timestamp`, `ip_address`, `gateway`, `uptime_ms`, `rssi`, `free_heap`.
**2. `ping` now echoes a caller-supplied timestamp for round-trip latency measurement.** [SystemHandler.cpp](../../src/Communication/Handlers/SystemHandler/SystemHandler.cpp)'s `handlePing()` previously built a `doc` and never used it — `ping` was a pure ack (`{"status":"SUCCESS","type":"pong"}`), no timing data. Now:
Request (contents optional):
```json
{ "v": 2, "cmd": "ping", "contents": { "ts": 1752600000123 } }
```
Response:
```json
{ "status": "SUCCESS", "type": "pong", "data": { "ts": 1752600000123, "device_uptime_ms": 184213 } }
```
- `ts` — caller's own epoch-millis timestamp at send time, echoed back **unchanged**. The device does not attempt to measure round-trip time itself — it has no reliable epoch clock without NTP, and the caller (backend) is already positioned to timestamp its own send and receipt. The device's only job is to echo promptly. Backend computes RTT as `(time of response receipt) − ts`.
- `ts` is optional on the request — if omitted, the response simply omits `ts` too. Fully backward compatible with any existing caller that just wants a liveness ack, exactly like before this change.
- `device_uptime_ms` (`millis()`) is included as a bonus — cheap, already-available, potentially useful for correlating a ping sample with a reboot — but is not required for RTT math.
- **Scope note:** `SystemHandler.cpp` (and therefore `ping`) is entirely wrapped in `#if HAS_RTC` — this command only exists on RTC-equipped board variants (`vesper-std`, not `agnus`/`agnus-mini`). This was a pre-existing constraint, left untouched by this change; non-RTC devices don't support ping-based latency until/unless that scoping is revisited separately.
Verified: `pio run` on `vesper-std-v1` (has `ping`) and `agnus-v1` (compiles clean with `SystemHandler.cpp` excluded via `#if HAS_RTC`, confirming the scoping wasn't disturbed).
Subsystem: `src/Communication/Drivers/MQTTDriver/MQTTDriver.cpp`, `src/Communication/Handlers/SystemHandler/SystemHandler.cpp`