From 043ea2986b073f26ec44b4cbe5b2cf7957aec54f Mon Sep 17 00:00:00 2001 From: s-hadinger <49731213+s-hadinger@users.noreply.github.com> Date: Thu, 18 May 2023 11:23:09 +0200 Subject: [PATCH] Berry `tasmota.wifi()` would wrongly report wifi as up (#18687) * Berry `tasmota.wifi()` would wrongly report wifi as up * Fix ESP8266 compile * Fix display of IP in wifiman * Improve wifiman IP handling for IPv6 --- CHANGELOG.md | 1 + tasmota/tasmota_support/support_wifi.ino | 21 ++++++++++++++++--- .../xdrv_01_9_webserver.ino | 5 +++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d16d119e4..769ffa136 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file. ### Fixed - Shutter bootloop using more than 4 shutters (#18673) - AIThinker webcam issues (#18652) +- Berry `tasmota.wifi()` would wrongly report wifi as up ### Removed diff --git a/tasmota/tasmota_support/support_wifi.ino b/tasmota/tasmota_support/support_wifi.ino index 0901ceed1..c518af1d2 100644 --- a/tasmota/tasmota_support/support_wifi.ino +++ b/tasmota/tasmota_support/support_wifi.ino @@ -503,6 +503,7 @@ void WifiSetState(uint8_t state) * - DNS reporting actual values used (not the Settings): * `DNSGetIP(n)`, `DNSGetIPStr(n)` with n=`0`/`1` (same dns for Wifi and Eth) \*****************************************************************************************************/ +bool WifiGetIP(IPAddress *ip, bool exclude_ap = false); // IPv4 for Wifi // Returns only IPv6 global address (no loopback and no link-local) bool WifiGetIPv4(IPAddress *ip) @@ -755,15 +756,29 @@ String IPForUrl(const IPAddress & ip) // Check to see if we have any routable IP address // IPv4 has always priority // Copy the value of the IP if pointer provided (optional) -bool WifiGetIP(IPAddress *ip) { - if ((uint32_t)WiFi.localIP() != 0) { +// `exclude_ap` allows to exlude AP IP address and focus only on local STA +bool WifiGetIP(IPAddress *ip, bool exclude_ap) { +#ifdef ESP32 + wifi_mode_t mode = WiFi.getMode(); + if ((mode == WIFI_MODE_STA || mode == WIFI_MODE_APSTA) && (uint32_t)WiFi.localIP() != 0) { if (ip != nullptr) { *ip = WiFi.localIP(); } return true; } - if ((uint32_t)WiFi.softAPIP() != 0) { + if (!exclude_ap && (mode == WIFI_MODE_AP || mode == WIFI_MODE_APSTA) && (uint32_t)WiFi.softAPIP() != 0) { if (ip != nullptr) { *ip = WiFi.softAPIP(); } return true; } +#else + WiFiMode_t mode = WiFi.getMode(); + if ((mode == WIFI_STA || mode == WIFI_AP_STA) && (uint32_t)WiFi.localIP() != 0) { + if (ip != nullptr) { *ip = WiFi.localIP(); } + return true; + } + if (!exclude_ap && (mode == WIFI_AP || mode == WIFI_AP_STA) && (uint32_t)WiFi.softAPIP() != 0) { + if (ip != nullptr) { *ip = WiFi.softAPIP(); } + return true; + } +#endif #ifdef USE_IPV6 IPAddress lip; if (WifiGetIPv6(&lip)) { diff --git a/tasmota/tasmota_xdrv_driver/xdrv_01_9_webserver.ino b/tasmota/tasmota_xdrv_driver/xdrv_01_9_webserver.ino index 7b241a9c7..84f5368fb 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_01_9_webserver.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_01_9_webserver.ino @@ -3701,11 +3701,12 @@ bool Xdrv01(uint32_t function) if (Wifi.wifi_test_counter) { Wifi.wifi_test_counter--; AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_WIFI D_TRYING_TO_CONNECT " %s"), SettingsText(SET_STASSID1)); - if (WifiHasIP()) { // Got IP - Connection Established + IPAddress local_ip; + if (WifiGetIP(&local_ip, true)) { // Got IP - Connection Established (exclude AP address) Wifi.wifi_test_AP_TIMEOUT = false; Wifi.wifi_test_counter = 0; Wifi.wifiTest = WIFI_TEST_FINISHED; - AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_WIFI D_CMND_SSID " %s: " D_CONNECTED " - " D_IP_ADDRESS " %s"), SettingsText(Wifi.wifi_Test_Save_SSID2 ? SET_STASSID2 : SET_STASSID1), WiFi.localIP().toString().c_str()); + AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_WIFI D_CMND_SSID " %s: " D_CONNECTED " - " D_IP_ADDRESS " %s"), SettingsText(Wifi.wifi_Test_Save_SSID2 ? SET_STASSID2 : SET_STASSID1), local_ip.toString().c_str()); // TasmotaGlobal.blinks = 255; // Signal wifi connection with blinks if (MAX_WIFI_OPTION != Wifi.old_wificonfig) { TasmotaGlobal.wifi_state_flag = Settings->sta_config = Wifi.old_wificonfig;