From 0e82271eb17ad5d343b997268bd1e9a6f3e8f1b5 Mon Sep 17 00:00:00 2001 From: agessaman Date: Tue, 31 Mar 2026 14:45:47 -0700 Subject: [PATCH] Implement WiFi disconnect diagnostics in MQTTBridge and CommonCLI. Add functionality to retrieve and format last WiFi disconnect reasons for improved troubleshooting. Update CommonCLI to handle diagnostic commands and enhance error reporting in MQTTBridge, ensuring better visibility into connection issues. --- src/helpers/CommonCLI.cpp | 17 ++++ src/helpers/bridges/MQTTBridge.cpp | 126 ++++++++++++++++++++++++++++- src/helpers/bridges/MQTTBridge.h | 11 +++ variants/rak3112/platformio.ini | 83 +++++++++++++++++++ 4 files changed, 233 insertions(+), 4 deletions(-) diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index 070be834f..2c092ad53 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -902,6 +902,8 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else { strcpy(reply, "> (not set — custom slots use username/password auth)"); } + } else if (memcmp(subcmd, "diag", 4) == 0) { + MQTTBridge::formatSlotDiagReply(reply, 160, slot); } else { sprintf(reply, "??: %s", config); } @@ -918,6 +920,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch case WL_CONNECT_FAILED: status_str = "connect_failed"; break; case WL_CONNECTION_LOST: status_str = "connection_lost"; break; case WL_DISCONNECTED: status_str = "disconnected"; break; + case 255: status_str = "not_started"; break; // WL_NO_SHIELD default: status_str = "unknown"; break; } if (status == WL_CONNECTED) { @@ -945,7 +948,21 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } #endif } else { +#ifdef WITH_MQTT_BRIDGE + uint8_t reason = MQTTBridge::getLastWifiDisconnectReason(); + if (reason != 0) { + const char* desc = MQTTBridge::wifiReasonStr(reason); + if (desc) { + sprintf(reply, "> %s: %s (reason: %d)", status_str, desc, reason); + } else { + sprintf(reply, "> %s: reason %d", status_str, reason); + } + } else { + sprintf(reply, "> %s (code: %d)", status_str, status); + } +#else sprintf(reply, "> %s (code: %d)", status_str, status); +#endif } } else if (memcmp(config, "wifi.powersave", 14) == 0) { uint8_t ps = _prefs->wifi_power_save; diff --git a/src/helpers/bridges/MQTTBridge.cpp b/src/helpers/bridges/MQTTBridge.cpp index 1e56468c2..fad10f6da 100644 --- a/src/helpers/bridges/MQTTBridge.cpp +++ b/src/helpers/bridges/MQTTBridge.cpp @@ -98,6 +98,10 @@ static void psram_free(void* ptr) { // Time (millis()) when WiFi was last seen connected; 0 when disconnected. Used for get wifi.status uptime. static unsigned long s_wifi_connected_at = 0; +// Last WiFi disconnect reason (from ESP-IDF event). Used for get wifi.status diagnostics. +static uint8_t s_wifi_disconnect_reason = 0; +static unsigned long s_wifi_disconnect_time = 0; + #ifdef MQTT_MEMORY_DEBUG // #region agent log static void agentLogHeap(const char* location, const char* message, const char* hypothesisId, @@ -169,6 +173,107 @@ void MQTTBridge::formatMqttStatusReply(char* buf, size_t bufsize, const NodePref snprintf(buf + pos, bufsize - pos, ", q:%d", q); } +uint8_t MQTTBridge::getLastWifiDisconnectReason() { return s_wifi_disconnect_reason; } +unsigned long MQTTBridge::getLastWifiDisconnectTime() { return s_wifi_disconnect_time; } + +const char* MQTTBridge::wifiReasonStr(uint8_t reason) { + switch (reason) { + case 2: return "auth expired"; + case 4: return "assoc timeout"; + case 8: return "AP disconnected"; + case 15: return "wrong password"; + case 39: return "SSID not found"; + case 200: return "signal lost"; + case 201: return "security mismatch"; + case 202: return "auth mode rejected"; + default: return nullptr; + } +} + +const char* MQTTBridge::tlsErrorStr(int32_t err) { + switch (err) { + case 0x8001: return "DNS failed"; + case 0x8002: return "socket error"; + case 0x8004: return "connect refused"; + case 0x8006: return "TLS timeout"; + case 0x800B: return "cert verify failed"; + case 0x8010: return "mbedTLS error"; + default: return nullptr; + } +} + +void MQTTBridge::formatSlotDiagReply(char* buf, size_t bufsize, int slot_index) { + if (!buf || bufsize == 0) return; + if (!s_mqtt_bridge_instance || !s_mqtt_bridge_instance->_initialized) { + snprintf(buf, bufsize, "> mqtt%d: bridge not running", slot_index + 1); + return; + } + if (slot_index < 0 || slot_index >= MAX_MQTT_SLOTS) { + snprintf(buf, bufsize, "> invalid slot"); + return; + } + + MQTTBridge* b = s_mqtt_bridge_instance; + const MQTTSlot& slot = b->_slots[slot_index]; + + // Determine state string + const char* state; + if (!slot.enabled && !slot.preset && slot.host[0] == '\0') { + snprintf(buf, bufsize, "> mqtt%d: not configured", slot_index + 1); + return; + } else if (!slot.enabled) { + state = "inactive"; + } else if (!slot.client) { + state = "no client"; + } else if (slot.connected) { + state = "ok"; + } else if (slot.circuit_breaker_tripped) { + state = "fail"; + } else { + state = "disc"; + } + + int pos = snprintf(buf, bufsize, "> mqtt%d: %s", slot_index + 1, state); + + // If connected with no errors, we're done + if (slot.connected && slot.last_error_time == 0) { + snprintf(buf + pos, bufsize - pos, ", no errors"); + return; + } + + // Show last error if we have one + if (slot.last_error_time > 0) { + // TLS error with human-friendly description + if (slot.last_tls_err != 0) { + const char* desc = tlsErrorStr(slot.last_tls_err); + if (desc) { + pos += snprintf(buf + pos, bufsize - pos, ", %s (0x%04X)", desc, (unsigned)slot.last_tls_err); + } else { + pos += snprintf(buf + pos, bufsize - pos, ", tls:0x%04X", (unsigned)slot.last_tls_err); + } + } + // mbedTLS stack error (shown as negative hex per convention) + if (slot.last_tls_stack_err != 0) { + pos += snprintf(buf + pos, bufsize - pos, ", mbedtls:-0x%04X", (unsigned)(-slot.last_tls_stack_err)); + } + // Socket errno + if (slot.last_sock_errno != 0) { + pos += snprintf(buf + pos, bufsize - pos, ", sock:%d", slot.last_sock_errno); + } + // Time ago + unsigned long ago_sec = (millis() - slot.last_error_time) / 1000; + if (ago_sec < 60) { + snprintf(buf + pos, bufsize - pos, ", %lus ago", ago_sec); + } else if (ago_sec < 3600) { + snprintf(buf + pos, bufsize - pos, ", %lum ago", ago_sec / 60); + } else { + snprintf(buf + pos, bufsize - pos, ", %luh ago", ago_sec / 3600); + } + } else if (!slot.connected) { + snprintf(buf + pos, bufsize - pos, ", no error info"); + } +} + // --------------------------------------------------------------------------- // Constructor // --------------------------------------------------------------------------- @@ -577,6 +682,11 @@ void MQTTBridge::initializeWiFiInTask() { _ntp_sync_pending = true; } break; + case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: + s_wifi_disconnect_reason = info.wifi_sta_disconnected.reason; + s_wifi_disconnect_time = millis(); + MQTT_DEBUG_PRINTLN("WiFi disconnected: reason %d", s_wifi_disconnect_reason); + break; default: break; } @@ -830,11 +940,11 @@ void MQTTBridge::setupSlot(int index) { slot.client = new PsychicMqttClient(); slot.client->setAutoReconnect(false); // We handle reconnect with our own backoff logic - if (slot.preset && slot.preset->keepalive > 0) { - slot.client->setKeepAlive(slot.preset->keepalive); - } bool uses_jwt = (slot.preset && slot.preset->auth_type == MQTT_AUTH_JWT) || slot.audience[0] != '\0'; - optimizeMqttClientConfig(slot.client, uses_jwt); + optimizeMqttClientConfig(slot.client, uses_jwt); // sets 45s keepalive default + if (slot.preset && slot.preset->keepalive > 0) { + slot.client->setKeepAlive(slot.preset->keepalive); // preset overrides default + } // Callbacks (capture index by value) slot.client->onConnect([this, index](bool sessionPresent) { @@ -843,6 +953,10 @@ void MQTTBridge::setupSlot(int index) { _slots[index].reconnect_backoff = 0; _slots[index].max_backoff_failures = 0; _slots[index].circuit_breaker_tripped = false; + _slots[index].last_tls_err = 0; + _slots[index].last_tls_stack_err = 0; + _slots[index].last_sock_errno = 0; + _slots[index].last_error_time = 0; updateCachedConnectionStatus(); publishStatusToSlot(index); }); @@ -852,6 +966,10 @@ void MQTTBridge::setupSlot(int index) { updateCachedConnectionStatus(); }); slot.client->onError([this, index](esp_mqtt_error_codes error) { + _slots[index].last_tls_err = error.esp_tls_last_esp_err; + _slots[index].last_tls_stack_err = error.esp_tls_stack_err; + _slots[index].last_sock_errno = error.esp_transport_sock_errno; + _slots[index].last_error_time = millis(); if (error.esp_tls_last_esp_err != 0 || error.esp_tls_stack_err != 0 || error.esp_transport_sock_errno != 0) { MQTT_DEBUG_PRINTLN("MQTT%d error: tls=%d, tls_stack=%d, sock=%d, type=%d", index + 1, error.esp_tls_last_esp_err, error.esp_tls_stack_err, diff --git a/src/helpers/bridges/MQTTBridge.h b/src/helpers/bridges/MQTTBridge.h index 19e4e238b..d08af590f 100644 --- a/src/helpers/bridges/MQTTBridge.h +++ b/src/helpers/bridges/MQTTBridge.h @@ -84,6 +84,12 @@ private: bool circuit_breaker_tripped; // true = stop reconnecting until reconfigured unsigned long last_reconnect_attempt; unsigned long last_log_time; // Throttle disconnect log messages + + // Last error (stored for CLI diagnostics — serial-free debugging) + int32_t last_tls_err; // esp_tls_last_esp_err (0 = no error) + int32_t last_tls_stack_err; // mbedTLS stack error + int last_sock_errno; // socket errno + unsigned long last_error_time; // millis() of last error }; static const size_t AUTH_TOKEN_SIZE = 768; @@ -317,6 +323,11 @@ public: static unsigned long getWifiConnectedAtMillis(); static void formatMqttStatusReply(char* buf, size_t bufsize, const NodePrefs* prefs); + static void formatSlotDiagReply(char* buf, size_t bufsize, int slot_index); + static uint8_t getLastWifiDisconnectReason(); + static unsigned long getLastWifiDisconnectTime(); + static const char* wifiReasonStr(uint8_t reason); + static const char* tlsErrorStr(int32_t err); void setStatsSources(mesh::Dispatcher* dispatcher, mesh::Radio* radio, mesh::MainBoard* board, mesh::MillisecondClock* ms); diff --git a/variants/rak3112/platformio.ini b/variants/rak3112/platformio.ini index b4165bb2b..98a60068d 100644 --- a/variants/rak3112/platformio.ini +++ b/variants/rak3112/platformio.ini @@ -98,6 +98,52 @@ lib_deps = ${rak3112.lib_deps} ${esp32_ota.lib_deps} +[env:RAK_3112_repeater_observer_mqtt] +extends = rak3112 +extra_scripts = + ${esp32_base.extra_scripts} + pre:scripts/generate_cert_bundle.py +board_ssl_cert_source = adafruit-full +board_build.embed_files = src/certs/x509_crt_bundle.bin +build_flags = + ${rak3112.build_flags} + -D ADVERT_NAME='"MQTT Observer"' + -D ADVERT_LAT=0.0 + -D ADVERT_LON=0.0 + -D ADMIN_PASSWORD='"password"' + -D MAX_NEIGHBOURS=50 + -D WITH_MQTT_BRIDGE=1 + -D MAX_MQTT_BROKERS=3 + -D MQTT_MAX_PACKET_SIZE=1024 + -D MQTT_DEBUG=1 + -D MESH_PACKET_LOGGING=1 + -D MESH_DEBUG=1 + -D CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=y + -D ESP32_CPU_FREQ=160 + -D MQTT_WIFI_TX_POWER=WIFI_POWER_19_5dBm + -D WITH_SNMP=1 +# -D WIFI_SSID='"ssid"' +# -D WIFI_PWD='"password"' +# -D MQTT_SERVER='"your-mqtt-broker.com"' +# -D MQTT_PORT=1883 +# -D MQTT_USERNAME='"your-username"' +# -D MQTT_PASSWORD='"your-password"' +build_src_filter = ${rak3112.build_src_filter} + + + + + + + + + +<../examples/simple_repeater> +lib_deps = + ${rak3112.lib_deps} + ${esp32_ota.lib_deps} + elims/PsychicMqttClient@^0.2.4 + bblanchon/ArduinoJson + arduino-libraries/NTPClient + JChristensen/Timezone + 0neblock/SNMP_Agent + paulstoffregen/Time@1.6.1 + [env:RAK_3112_room_server] extends = rak3112 build_flags = @@ -115,6 +161,43 @@ lib_deps = ${rak3112.lib_deps} ${esp32_ota.lib_deps} +[env:RAK_3112_room_server_observer_mqtt] +extends = rak3112 +extra_scripts = + ${esp32_base.extra_scripts} + pre:scripts/generate_cert_bundle.py +board_ssl_cert_source = adafruit-full +board_build.embed_files = src/certs/x509_crt_bundle.bin +build_flags = + ${rak3112.build_flags} + -D ADVERT_NAME='"RAK3112 Room Observer"' + -D ADVERT_LAT=0.0 + -D ADVERT_LON=0.0 + -D ADMIN_PASSWORD='"password"' + -D ROOM_PASSWORD='"hello"' + -D WITH_MQTT_BRIDGE=1 + -D MAX_MQTT_BROKERS=3 + -D MQTT_MAX_PACKET_SIZE=1024 + -D MQTT_DEBUG=1 +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 + -D CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=y + -D ESP32_CPU_FREQ=160 + -D MQTT_WIFI_TX_POWER=WIFI_POWER_19_5dBm +build_src_filter = ${rak3112.build_src_filter} + + + + + + + +<../examples/simple_room_server> +lib_deps = + ${rak3112.lib_deps} + ${esp32_ota.lib_deps} + elims/PsychicMqttClient@^0.2.4 + bblanchon/ArduinoJson + arduino-libraries/NTPClient + JChristensen/Timezone + paulstoffregen/Time@1.6.1 + [env:RAK_3112_terminal_chat] extends = rak3112 build_flags =