diff --git a/README.md b/README.md index 7e0316a..30cfa4e 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,7 @@ cp include/example.config.h include/config.h |---|---| | `solar/mppt/data` | JSON payload with all sensor values | | `solar/mppt/status` | `online` / `offline` (retained LWT) | +| `solar/mppt/diag` | Diagnostics: uptime, WiFi RSSI/status, heap, reconnect counts, BLE packet age | ```json { diff --git a/include/example.config.h b/include/example.config.h index 1b93f83..d56594b 100644 --- a/include/example.config.h +++ b/include/example.config.h @@ -15,6 +15,8 @@ // ── MQTT topics ─────────────────────────────────────────────────────────────── #define MQTT_TOPIC_DATA "solar/mppt/data" #define MQTT_TOPIC_STATUS "solar/mppt/status" +// Runtime diagnostics: uptime, RSSI, heap, reconnect counts, BLE age +#define MQTT_TOPIC_DIAG "solar/mppt/diag" // ── Victron SmartSolar MPPT ─────────────────────────────────────────────────── // MAC address of your Victron device (lowercase, colon-separated) @@ -31,3 +33,6 @@ // BLE scan window (seconds) #define BLE_SCAN_DURATION_S 5 + +// How often to publish the diagnostics payload (milliseconds) +#define DIAG_INTERVAL_MS 60000 diff --git a/src/main.cpp b/src/main.cpp index c999afa..269dec9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -19,6 +20,25 @@ static WiFiClient s_wifiClient; static PubSubClient s_mqtt(s_wifiClient); static bool s_discoveryPublished = false; +// ── Runtime diagnostics ────────────────────────────────────────────────────── +// Written from the BLE callback (core 1) / WiFi event task, read from core 0. + +static volatile uint32_t s_wifiDisconnects = 0; +static volatile uint32_t s_mqttConnects = 0; +static volatile uint32_t s_pubFailures = 0; +static volatile uint32_t s_bleValid = 0; +static volatile uint32_t s_bleIgnored = 0; +static volatile uint32_t s_lastBleMs = 0; +static volatile bool s_bleSeen = false; + +#ifndef MQTT_TOPIC_DIAG +#define MQTT_TOPIC_DIAG "solar/mppt/diag" +#endif + +#ifndef DIAG_INTERVAL_MS +#define DIAG_INTERVAL_MS 60000UL +#endif + // ── Helpers ────────────────────────────────────────────────────────────────── static bool hexToBin(const char* hex, uint8_t* out, size_t outLen) { @@ -39,6 +59,26 @@ static bool hexToBin(const char* hex, uint8_t* out, size_t outLen) { // ── WiFi ───────────────────────────────────────────────────────────────────── +static void onWiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { + switch (event) { + case ARDUINO_EVENT_WIFI_STA_CONNECTED: + Serial.println("[WiFi] Associated with AP"); + break; + case ARDUINO_EVENT_WIFI_STA_GOT_IP: + Serial.printf("[WiFi] Got IP: %s (RSSI %d dBm)\n", + WiFi.localIP().toString().c_str(), (int)WiFi.RSSI()); + break; + case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: + s_wifiDisconnects++; + Serial.printf("[WiFi] Disconnected, reason=%u (total %lu)\n", + (unsigned)info.wifi_sta_disconnected.reason, + (unsigned long)s_wifiDisconnects); + break; + default: + break; + } +} + static void wifiConnect() { Serial.printf("[WiFi] Connecting to %s", WIFI_SSID); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); @@ -70,6 +110,7 @@ static void mqttConnect() { } if (ok) { Serial.println(" connected"); + s_mqttConnects++; s_mqtt.publish(MQTT_TOPIC_STATUS, "online", true); s_discoveryPublished = false; } else { @@ -185,10 +226,37 @@ static void publishData(const MpptData& d) { char buf[256]; serializeJson(doc, buf); - s_mqtt.publish(MQTT_TOPIC_DATA, buf, false); + if (!s_mqtt.publish(MQTT_TOPIC_DATA, buf, false)) { + s_pubFailures++; + Serial.printf("[MQTT] Publish FAILED: %s\n", buf); + return; + } Serial.printf("[MQTT] Published: %s\n", buf); } +// ── Diagnostics ────────────────────────────────────────────────────────────── + +static void publishDiagnostics(uint32_t now) { + uint32_t bleAgeS = s_bleSeen ? (now - s_lastBleMs) / 1000UL : 999999UL; + + char buf[256]; + snprintf(buf, sizeof(buf), + "{\"uptime_s\":%lu,\"wifi_status\":%d,\"wifi_rssi\":%d," + "\"free_heap\":%u,\"min_free_heap\":%u," + "\"wifi_disconnects\":%lu,\"mqtt_connects\":%lu," + "\"publish_failures\":%lu,\"ble_valid\":%lu," + "\"ble_ignored\":%lu,\"ble_age_s\":%lu}", + (unsigned long)(now / 1000UL), (int)WiFi.status(), (int)WiFi.RSSI(), + (unsigned)ESP.getFreeHeap(), (unsigned)ESP.getMinFreeHeap(), + (unsigned long)s_wifiDisconnects, (unsigned long)s_mqttConnects, + (unsigned long)s_pubFailures, (unsigned long)s_bleValid, + (unsigned long)s_bleIgnored, (unsigned long)bleAgeS); + + bool ok = s_mqtt.publish(MQTT_TOPIC_DIAG, buf, false); + if (!ok) s_pubFailures++; + Serial.printf("[DIAG] %s %s\n", ok ? "OK" : "FAIL", buf); +} + // ── MQTT Task (runs on Core 0) ──────────────────────────────────────────────── void mqttTask(void* param) { @@ -202,7 +270,11 @@ void mqttTask(void* param) { wifiConnect(); mqttConnect(); + uint32_t lastDiagMs = millis(); + for (;;) { + uint32_t now = millis(); + if (WiFi.status() != WL_CONNECTED) wifiConnect(); if (!s_mqtt.connected()) mqttConnect(); @@ -215,6 +287,11 @@ void mqttTask(void* param) { publishData(s_latest); } + if (now - lastDiagMs >= DIAG_INTERVAL_MS) { + lastDiagMs = now; + publishDiagnostics(now); + } + delay(100); } } @@ -235,9 +312,14 @@ class VictronAdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks MpptData parsed = parseVictronMppt(s_encKey, payload, payloadLen); if (parsed.valid) { - s_latest = parsed; - s_newData = true; + s_latest = parsed; + s_lastBleMs = millis(); + s_bleSeen = true; + s_bleValid++; + s_newData = true; Serial.println("[BLE] Valid Victron packet decoded"); + } else { + s_bleIgnored++; } } }; @@ -269,13 +351,17 @@ void setup() { } Serial.println("[Boot] Config OK"); + WiFi.onEvent(onWiFiEvent); + xTaskCreatePinnedToCore(mqttTask, "mqttTask", 8192, NULL, 1, NULL, 0); bleInit(); } void loop() { Serial.println("[BLE] Scanning..."); - s_scan->start(BLE_SCAN_DURATION_S, false); + if (!s_scan->start(BLE_SCAN_DURATION_S, false)) { + Serial.println("[BLE] Scan start FAILED"); + } s_scan->clearResults(); delay(PUBLISH_INTERVAL_MS); } \ No newline at end of file