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:
2026-07-17 13:09:16 +03:00
parent 9cd6b142ef
commit 959cb77d81
14 changed files with 388 additions and 9 deletions
+62
View File
@@ -28,6 +28,7 @@ void Telemetry::begin() {
// (512B format buffer + MQTT publish String + AsyncMqttClient::publish()) on this
// task's own stack. 4096 overflowed the canary in production — see stack-audit notes.
xTaskCreatePinnedToCore(telemetryTask, "TelemetryTask", 8192, this, 2, &telemetryTaskHandle, 1);
registerTaskForStackMonitoring("TelemetryTask", telemetryTaskHandle);
LOG_INFO(TAG, "Telemetry initialized");
}
@@ -206,6 +207,64 @@ void Telemetry::logVibration(float vibration) {
LOG_INFO(TAG, "Vibration: %.2f", vibration);
}
// ════════════════════════════════════════════════════════════════════════════
// DIAGNOSTICS REPORT — local temperature sampling + stack high-water registry
// ════════════════════════════════════════════════════════════════════════════
void Telemetry::sampleDiagnosticsIfDue() {
uint32_t now = millis();
if (now - lastDiagnosticsSampleMs < DIAGNOSTICS_SAMPLE_INTERVAL_MS) return;
lastDiagnosticsSampleMs = now;
// ESP32's built-in die temperature sensor (temperatureRead(), Arduino-ESP32
// core). Coarse and reads die temp rather than ambient — useful as a "is
// this thing running hot" trend signal, not a calibrated instrument.
float tempC = temperatureRead();
temperatureStats.addSample(tempC);
}
// Fixed-size registry — matches this codebase's avoid-dynamic-allocation
// style elsewhere (e.g. strikeCounters[16]). 8 slots comfortably covers every
// task with a stored TaskHandle_t today (TelemetryTask, HealthMonitor,
// TimeKeeper, OutputManager, FirmwareValidator, Logging, BellEngine,
// InputManager) with room to spare; short-lived fire-and-forget tasks like
// mqttRecon/netRecon don't keep a handle at all and can't be monitored this way.
namespace {
struct StackMonitorEntry {
const char* name = nullptr;
TaskHandle_t handle = nullptr;
};
constexpr size_t MAX_STACK_MONITOR_ENTRIES = 8;
StackMonitorEntry stackMonitorRegistry[MAX_STACK_MONITOR_ENTRIES];
size_t stackMonitorCount = 0;
}
void Telemetry::registerTaskForStackMonitoring(const char* name, TaskHandle_t handle) {
if (handle == nullptr) return;
if (stackMonitorCount >= MAX_STACK_MONITOR_ENTRIES) {
LOG_WARNING(TAG, "Stack monitor registry full — '%s' not registered", name);
return;
}
stackMonitorRegistry[stackMonitorCount++] = { name, handle };
}
void Telemetry::writeStackHighWaterMarks(JsonObject& out) {
for (size_t i = 0; i < stackMonitorCount; i++) {
const auto& entry = stackMonitorRegistry[i];
UBaseType_t wordsFree = uxTaskGetStackHighWaterMark(entry.handle);
out[entry.name] = (uint32_t)(wordsFree * sizeof(StackType_t));
}
}
void Telemetry::writeTemperatureStats(JsonDocument& doc) const {
if (temperatureStats.count == 0) return;
JsonObject tempObj = doc["cpu_temp"].to<JsonObject>();
tempObj["avg"] = temperatureStats.average();
tempObj["min"] = temperatureStats.min;
tempObj["max"] = temperatureStats.max;
tempObj["samples"] = temperatureStats.count;
}
void Telemetry::checkBellLoads() {
if (!guardEnabled) {
// Guard disabled — still decay loads for accurate reporting, but never force-stop
@@ -292,6 +351,9 @@ void Telemetry::telemetryTask(void* parameter) {
telemetry->flushRuntimeToSD();
telemetry->lastRuntimeFlushMs = now;
}
// Diagnostics: local temperature sampling (own throttle, ~15s)
telemetry->sampleDiagnosticsIfDue();
}
vTaskDelay(pdMS_TO_TICKS(1000));