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`
@@ -416,6 +416,10 @@ void MQTTDriver::publishHeartbeat() {
// WiFi signal strength
payload["rssi"] = WiFi.RSSI();
// Free heap (bytes) — lets the console chart heap trend over time instead of
// only seeing one sample per boot (via boot_report)
payload["free_heap"] = ESP.getFreeHeap();
// Serialize to string
String heartbeatMessage;
serializeJson(doc, heartbeatMessage);
@@ -62,7 +62,7 @@ Reconnect attempts are reset to 0 on a successful connection or a new `onNetwork
## Heartbeat
Once connected, a heartbeat is published immediately and then every 30 seconds to `vesper/{uid}/status/heartbeat` with `retain = true`. Heartbeat payload includes `device_id`, `firmware_version`, `ip_address`, `gateway`, `uptime_ms`, `rssi`, and an uptime string. The heartbeat timer is stopped on disconnect and restarted on reconnect.
Once connected, a heartbeat is published immediately and then every 30 seconds to `vesper/{uid}/status/heartbeat` with `retain = true`. Heartbeat payload includes `device_id`, `firmware_version`, `ip_address`, `gateway`, `uptime_ms`, `rssi`, `free_heap` (bytes, via `ESP.getFreeHeap()` — added so a console can chart heap trend over time from the same 30s cadence as RSSI, rather than only seeing one heap sample per boot via `boot_report`), and an uptime string. The heartbeat timer is stopped on disconnect and restarted on reconnect.
---
+2 -2
View File
@@ -81,13 +81,13 @@ Full control over the Timekeeper clock subsystem and its persisted configuration
## SystemHandler
General device management. `identify` is WebSocket-only. `system.restart` defers the actual reboot to a FreeRTOS task so the reply reaches the client first.
General device management. `identify` is WebSocket-only. `system.restart` defers the actual reboot to a FreeRTOS task so the reply reaches the client first. `ping`/`identify`/all `system.*` commands here are wrapped in `#if HAS_RTC` — this handler does not exist on non-RTC variants (e.g. `agnus_mini`).
**Commands:**
| Command | Key contents | Description |
|---------|-------------|-------------|
| `ping` | — | Returns `{ "status": "SUCCESS", "type": "pong" }`. |
| `ping` | `{ "ts": 1752600000123 }` (optional) | Echoes `ts` back unchanged plus `device_uptime_ms` (`millis()`), so the caller can compute round-trip time as `(time of response receipt) − ts` without needing synchronized clocks. Returns `{ "status": "SUCCESS", "type": "pong", "data": { "ts": 1752600000123, "device_uptime_ms": 184213 } }`. `ts` is omitted from the response if the caller didn't send one — fully backward compatible with callers that just want a liveness ack. |
| `identify` | `{ "device_type": "master"\|"secondary" }` | WebSocket only. Classifies the calling client in `ClientManager`. |
| `system.status` | — | Player status, time elapsed, projected run time, 16 strike counters. |
| `system.get_time` | — | Local and UTC timestamps, broken-down time fields, `rtc_available`. |
@@ -54,7 +54,14 @@ CommandResult SystemHandler::execute(const CommandEnvelope& envelope) {
CommandResult SystemHandler::handlePing(const CommandEnvelope& e) {
JsonDocument doc;
doc["type"] = "pong";
return CommandResult::ok("pong");
// Echo caller's timestamp back unchanged so the caller can compute RTT
// as (time_received - contents.ts) without needing symmetric clocks.
// uint64_t: epoch-millis (e.g. 1752600000123) overflows uint32_t.
if (e.contents["ts"].is<uint64_t>()) {
doc["ts"] = e.contents["ts"];
}
doc["device_uptime_ms"] = millis();
return CommandResult::ok("pong", doc);
}
CommandResult SystemHandler::handleIdentify(const CommandEnvelope& e) {