diff --git a/lib/framework/NTPSettingsService.cpp b/lib/framework/NTPSettingsService.cpp index 588f5a1a5..960a45228 100644 --- a/lib/framework/NTPSettingsService.cpp +++ b/lib/framework/NTPSettingsService.cpp @@ -82,5 +82,4 @@ void NTPSettingsService::configureTime(AsyncWebServerRequest * request, JsonVari void NTPSettingsService::ntp_received(struct timeval * tv) { // emsesp::EMSESP::logger().info("NTP sync to %d sec", tv->tv_sec); emsesp::EMSESP::system_.ntp_connected(true); - emsesp::EMSESP::system_.send_info_mqtt("connected", true); // send info topic with NTP time } diff --git a/src/system.cpp b/src/system.cpp index ade342558..e44dac9e5 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -541,19 +541,26 @@ void System::loop() { led_monitor(); // check status and report back using the LED system_check(); // check system health + send_info_mqtt(); #endif } // send MQTT info topic appended with the version information as JSON, as a retained flag -void System::send_info_mqtt(const char * event_str, bool send_ntp) { - // use dynamic json because it is called from NTP-callback from lwip task with small stack - DynamicJsonDocument doc = DynamicJsonDocument(EMSESP_JSON_SIZE_MEDIUM); - doc["event"] = event_str; - doc["version"] = EMSESP_APP_VERSION; +void System::send_info_mqtt() { + static uint8_t _connection = 0; + uint8_t connection = (ethernet_connected() ? 1 : 0) + ((WiFi.status() == WL_CONNECTED) ? 2 : 0) + (ntp_connected_ ? 4 : 0) + (has_ipv6_ ? 8 : 0); + // check if connection status has changed + if (!Mqtt::connected() || connection == _connection) { + return; + } + _connection = connection; + StaticJsonDocument doc; + doc["event"] = "connected"; + doc["version"] = EMSESP_APP_VERSION; // if NTP is enabled send the boot_time in local time in ISO 8601 format (eg: 2022-11-15 20:46:38) // https://github.com/emsesp/EMS-ESP32/issues/751 - if (send_ntp) { + if (ntp_connected_) { char time_string[25]; time_t now = time(nullptr) - uuid::get_uptime_sec(); strftime(time_string, 25, "%FT%T%z", localtime(&now)); @@ -1200,6 +1207,9 @@ bool System::command_info(const char * value, const int8_t id, JsonObject & outp } #endif EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & settings) { + if (WiFi.status() == WL_CONNECTED && !settings.bssid.isEmpty()) { + node["BSSID"] = "set"; + } node["static ip config"] = settings.staticIPConfig; node["enable IPv6"] = settings.enableIPv6; node["low bandwidth"] = settings.bandwidth20; diff --git a/src/system.h b/src/system.h index a4cb6d5f6..9d8e756e4 100644 --- a/src/system.h +++ b/src/system.h @@ -79,7 +79,7 @@ class System { bool check_restore(); bool heartbeat_json(JsonObject & output); void send_heartbeat(); - void send_info_mqtt(const char * event_str, bool send_ntp = false); + void send_info_mqtt(); bool syslog_enabled() { return syslog_enabled_; @@ -181,6 +181,10 @@ class System { ethernet_connected_ = b; } + void has_ipv6(bool b) { + has_ipv6_ = b; + } + void ntp_connected(bool b); bool ntp_connected(); @@ -297,6 +301,7 @@ class System { bool upload_status_ = false; // true if we're in the middle of a OTA firmware upload bool ethernet_connected_ = false; + bool has_ipv6_ = false; bool ntp_connected_ = false; uint32_t ntp_last_check_ = 0; diff --git a/src/web/WebStatusService.cpp b/src/web/WebStatusService.cpp index 7ca471be9..c5f2d03bb 100644 --- a/src/web/WebStatusService.cpp +++ b/src/web/WebStatusService.cpp @@ -38,13 +38,12 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: EMSESP::logger().warning("WiFi disconnected. Reason code=%s", disconnectReason(info.wifi_sta_disconnected.reason)); // IDF 4.0 WiFi.disconnect(true); + EMSESP::system_.has_ipv6(false); break; case ARDUINO_EVENT_WIFI_STA_GOT_IP: EMSESP::logger().info("WiFi connected with IP=%s, hostname=%s", WiFi.localIP().toString().c_str(), WiFi.getHostname()); - // EMSESP::system_.syslog_init(); mDNS_start(); - EMSESP::system_.send_info_mqtt("connected"); break; case ARDUINO_EVENT_ETH_START: @@ -64,22 +63,21 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { // prevent double calls if (!EMSESP::system_.ethernet_connected()) { EMSESP::logger().info("Ethernet connected with IP=%s, speed %d Mbps", ETH.localIP().toString().c_str(), ETH.linkSpeed()); - // EMSESP::system_.send_heartbeat(); - // EMSESP::system_.syslog_init(); EMSESP::system_.ethernet_connected(true); mDNS_start(); - EMSESP::system_.send_info_mqtt("connected"); } break; case ARDUINO_EVENT_ETH_DISCONNECTED: EMSESP::logger().warning("Ethernet disconnected"); EMSESP::system_.ethernet_connected(false); + EMSESP::system_.has_ipv6(false); break; case ARDUINO_EVENT_ETH_STOP: EMSESP::logger().info("Ethernet stopped"); EMSESP::system_.ethernet_connected(false); + EMSESP::system_.has_ipv6(false); break; case ARDUINO_EVENT_WIFI_STA_CONNECTED: @@ -105,9 +103,8 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { } else { EMSESP::logger().info("WiFi connected with IPv6=%s, hostname=%s", WiFi.localIPv6().toString().c_str(), WiFi.getHostname()); } - // EMSESP::system_.syslog_init(); mDNS_start(); - EMSESP::system_.send_info_mqtt("connected"); + EMSESP::system_.has_ipv6(true); break; default: @@ -151,7 +148,7 @@ void WebStatusService::webStatusService(AsyncWebServerRequest * request) { if (EMSESP::sensor_enabled()) { statJson = statsJson.createNestedObject(); statJson["id"] = 3; - statJson["s"] = EMSESP::temperaturesensor_.reads(); + statJson["s"] = EMSESP::temperaturesensor_.reads() - EMSESP::temperaturesensor_.fails(); statJson["f"] = EMSESP::temperaturesensor_.fails(); statJson["q"] = EMSESP::temperaturesensor_.reads() == 0 ? 100 : 100 - (uint8_t)((100 * EMSESP::temperaturesensor_.fails()) / EMSESP::temperaturesensor_.reads()); @@ -159,7 +156,7 @@ void WebStatusService::webStatusService(AsyncWebServerRequest * request) { if (EMSESP::analog_enabled()) { statJson = statsJson.createNestedObject(); statJson["id"] = 4; - statJson["s"] = EMSESP::analogsensor_.reads(); + statJson["s"] = EMSESP::analogsensor_.reads() - EMSESP::analogsensor_.fails(); statJson["f"] = EMSESP::analogsensor_.fails(); statJson["q"] = EMSESP::analogsensor_.reads() == 0 ? 100 : 100 - (uint8_t)((100 * EMSESP::analogsensor_.fails()) / EMSESP::analogsensor_.reads()); }