From 10ef58dccf50d440abde979d6946eba5c81d11b3 Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Mon, 11 May 2020 13:18:52 +0900 Subject: [PATCH 1/2] Preserve WiFi mode #210 --- src/AutoConnect.cpp | 16 +++++++++++----- src/AutoConnect.h | 8 ++++---- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/AutoConnect.cpp b/src/AutoConnect.cpp index de896ac..b7f382d 100644 --- a/src/AutoConnect.cpp +++ b/src/AutoConnect.cpp @@ -79,18 +79,24 @@ bool AutoConnect::begin(const char* ssid, const char* passphrase, unsigned long // Overwrite for the current timeout value. _connectTimeout = timeout; - if (_apConfig.preserveAPMode && !_apConfig.autoRise) { + if (_apConfig.preserveWiFiMode && !_apConfig.autoRise) { // Captive portal will not be started on connection failure. Enable Station mode // without disabling any current soft AP. - cs = WiFi.enableSTA(true); - AC_DBG(PSTR("Enable WIFI_STA: %s\n"), cs ? "Ok" : "Failed"); + AC_DBG("WiFi mode %d maintained, STA ", WiFi.getMode()); + if (WiFi.enableSTA(true)) { + AC_DBG_DUMB("enabled"); + } + else { + AC_DBG_DUMB("unavailable"); + } } else { // Start WiFi connection with station mode. WiFi.softAPdisconnect(true); - cs = WiFi.mode(WIFI_STA); + if (!WiFi.mode(WIFI_STA)) { + AC_DBG("Unable start WIFI_STA"); + } delay(100); - AC_DBG(PSTR("Switch to WIFI_STA: %s\n"), cs ? "Ok" : "Failed"); } // Set host name diff --git a/src/AutoConnect.h b/src/AutoConnect.h index c996ca6..ae8f6d4 100644 --- a/src/AutoConnect.h +++ b/src/AutoConnect.h @@ -105,7 +105,7 @@ class AutoConnectConfig { autoReconnect(false), immediateStart(false), retainPortal(false), - preserveAPMode(false), + preserveWiFiMode(false), portalTimeout(AUTOCONNECT_CAPTIVEPORTAL_TIMEOUT), menuItems(AC_MENUITEM_CONFIGNEW | AC_MENUITEM_OPENSSIDS | AC_MENUITEM_DISCONNECT | AC_MENUITEM_RESET | AC_MENUITEM_UPDATE | AC_MENUITEM_HOME), ticker(false), @@ -142,7 +142,7 @@ class AutoConnectConfig { autoReconnect(false), immediateStart(false), retainPortal(false), - preserveAPMode(false), + preserveWiFiMode(false), portalTimeout(portalTimeout), menuItems(AC_MENUITEM_CONFIGNEW | AC_MENUITEM_OPENSSIDS | AC_MENUITEM_DISCONNECT | AC_MENUITEM_RESET | AC_MENUITEM_UPDATE | AC_MENUITEM_HOME), ticker(false), @@ -179,7 +179,7 @@ class AutoConnectConfig { autoReconnect = o.autoReconnect; immediateStart = o.immediateStart; retainPortal = o.retainPortal; - preserveAPMode = o.preserveAPMode; + preserveWiFiMode = o.preserveWiFiMode; portalTimeout = o.portalTimeout; menuItems = o.menuItems; ticker = o.ticker; @@ -216,7 +216,7 @@ class AutoConnectConfig { bool autoReconnect; /**< Automatic reconnect with past SSID */ bool immediateStart; /**< Skips WiFi.begin(), start portal immediately */ bool retainPortal; /**< Even if the captive portal times out, it maintains the portal state. */ - bool preserveAPMode; /**< Keep existing AP WiFi mode if captive portal won't be started. */ + bool preserveWiFiMode; /**< Keep existing WiFi mode if captive portal won't be started. */ unsigned long portalTimeout; /**< Timeout value for stay in the captive portal */ uint16_t menuItems; /**< A compound value of the menu items to be attached */ bool ticker; /**< Drives LED flicker according to WiFi connection status. */ From b5106af7e07520e9571b87b9894874ef98e7daa8 Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Tue, 12 May 2020 17:13:37 +0900 Subject: [PATCH 2/2] #issuecomment-626827941 --- src/AutoConnect.cpp | 16 +++++----------- src/AutoConnect.h | 8 ++++---- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/AutoConnect.cpp b/src/AutoConnect.cpp index b7f382d..a279fe4 100644 --- a/src/AutoConnect.cpp +++ b/src/AutoConnect.cpp @@ -79,23 +79,17 @@ bool AutoConnect::begin(const char* ssid, const char* passphrase, unsigned long // Overwrite for the current timeout value. _connectTimeout = timeout; - if (_apConfig.preserveWiFiMode && !_apConfig.autoRise) { + if (_apConfig.preserveAPMode && !_apConfig.autoRise) { // Captive portal will not be started on connection failure. Enable Station mode // without disabling any current soft AP. - AC_DBG("WiFi mode %d maintained, STA ", WiFi.getMode()); - if (WiFi.enableSTA(true)) { - AC_DBG_DUMB("enabled"); - } - else { - AC_DBG_DUMB("unavailable"); - } + cs = WiFi.enableSTA(true); + AC_DBG("WiFi mode %d maintained, STA %s\n", WiFi.getMode(), cs ? "enabled" : "unavailable"); } else { // Start WiFi connection with station mode. WiFi.softAPdisconnect(true); - if (!WiFi.mode(WIFI_STA)) { - AC_DBG("Unable start WIFI_STA"); - } + if (!WiFi.mode(WIFI_STA)) + AC_DBG("Unable start WIFI_STA\n"); delay(100); } diff --git a/src/AutoConnect.h b/src/AutoConnect.h index ae8f6d4..c996ca6 100644 --- a/src/AutoConnect.h +++ b/src/AutoConnect.h @@ -105,7 +105,7 @@ class AutoConnectConfig { autoReconnect(false), immediateStart(false), retainPortal(false), - preserveWiFiMode(false), + preserveAPMode(false), portalTimeout(AUTOCONNECT_CAPTIVEPORTAL_TIMEOUT), menuItems(AC_MENUITEM_CONFIGNEW | AC_MENUITEM_OPENSSIDS | AC_MENUITEM_DISCONNECT | AC_MENUITEM_RESET | AC_MENUITEM_UPDATE | AC_MENUITEM_HOME), ticker(false), @@ -142,7 +142,7 @@ class AutoConnectConfig { autoReconnect(false), immediateStart(false), retainPortal(false), - preserveWiFiMode(false), + preserveAPMode(false), portalTimeout(portalTimeout), menuItems(AC_MENUITEM_CONFIGNEW | AC_MENUITEM_OPENSSIDS | AC_MENUITEM_DISCONNECT | AC_MENUITEM_RESET | AC_MENUITEM_UPDATE | AC_MENUITEM_HOME), ticker(false), @@ -179,7 +179,7 @@ class AutoConnectConfig { autoReconnect = o.autoReconnect; immediateStart = o.immediateStart; retainPortal = o.retainPortal; - preserveWiFiMode = o.preserveWiFiMode; + preserveAPMode = o.preserveAPMode; portalTimeout = o.portalTimeout; menuItems = o.menuItems; ticker = o.ticker; @@ -216,7 +216,7 @@ class AutoConnectConfig { bool autoReconnect; /**< Automatic reconnect with past SSID */ bool immediateStart; /**< Skips WiFi.begin(), start portal immediately */ bool retainPortal; /**< Even if the captive portal times out, it maintains the portal state. */ - bool preserveWiFiMode; /**< Keep existing WiFi mode if captive portal won't be started. */ + bool preserveAPMode; /**< Keep existing AP WiFi mode if captive portal won't be started. */ unsigned long portalTimeout; /**< Timeout value for stay in the captive portal */ uint16_t menuItems; /**< A compound value of the menu items to be attached */ bool ticker; /**< Drives LED flicker according to WiFi connection status. */