From 4ee0b1219ea4d72c0b1da13363ecdebfdeb19b17 Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Tue, 4 Dec 2018 20:40:10 +0900 Subject: [PATCH] Supports AutoConnectAux --- src/AutoConnect.cpp | 32 +++++++++++++++++++++++--------- src/AutoConnect.h | 6 +++--- src/AutoConnectDefs.h | 3 +++ 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/AutoConnect.cpp b/src/AutoConnect.cpp index a0f996c..9862304 100644 --- a/src/AutoConnect.cpp +++ b/src/AutoConnect.cpp @@ -47,6 +47,7 @@ AutoConnect::AutoConnect(WebServerClass& webServer) { void AutoConnect::_initialize() { _rfConnect = false; _rfReset = false; + _responsePage = nullptr; _currentPageElement = nullptr; _menuTitle = String(AUTOCONNECT_MENU_TITLE); _portalTimeout = AUTOCONNECT_TIMEOUT; @@ -88,9 +89,10 @@ bool AutoConnect::begin(const char* ssid, const char* passphrase, unsigned long _portalTimeout = timeout; // Start WiFi connection with station mode. - WiFi.softAPdisconnect(false); + WiFi.softAPdisconnect(true); WiFi.enableAP(false); - delay(100); + _disconnectWiFi(false); + WiFi.setAutoReconnect(false); WiFi.mode(WIFI_STA); delay(100); @@ -143,9 +145,6 @@ bool AutoConnect::begin(const char* ssid, const char* passphrase, unsigned long } _currentHostIP = WiFi.localIP(); - // It doesn't matter the connection status for launching the Web server. - _startWebServer(); - // Rushing into the portal. if (!cs) { @@ -155,8 +154,6 @@ bool AutoConnect::begin(const char* ssid, const char* passphrase, unsigned long // Change WiFi working mode, Enable AP with STA WiFi.setAutoConnect(false); _disconnectWiFi(true); - WiFi.mode(WIFI_AP_STA); - delay(300); // Connection unsuccessful, launch the captive portal. if (!(_apConfig.apip == IPAddress(0, 0, 0, 0) || _apConfig.gateway == IPAddress(0, 0, 0, 0) || _apConfig.netmask == IPAddress(0, 0, 0, 0))) { @@ -173,6 +170,14 @@ bool AutoConnect::begin(const char* ssid, const char* passphrase, unsigned long // Fork to the exit routine that starts captive portal. cs = _onDetectExit ? _onDetectExit(_currentHostIP) : true; + // Activate the AP mode with configured softAP and start the access point. + WiFi.mode(WIFI_AP_STA); + while (WiFi.getMode() != WIFI_AP_STA) + yield(); + + // Start Web server when TCP connection is enabled. + _startWebServer(); + // Start captive portal without cancellation by DetectExit. if (cs) { // Prepare for redirecting captive portal detection. @@ -195,6 +200,11 @@ bool AutoConnect::begin(const char* ssid, const char* passphrase, unsigned long } } } + + // It doesn't matter the connection status for launching the Web server. + if (!_responsePage) + _startWebServer(); + return cs; } @@ -312,6 +322,7 @@ void AutoConnect::_startWebServer() { _webServer->onNotFound(std::bind(&AutoConnect::_handleNotFound, this)); // here, Prepare PageBuilders for captive portal _responsePage = new PageBuilder(); +// _responsePage->chunked(PB_ByteStream); _responsePage->chunked(PB_ByteStream); _responsePage->exitCanHandle(std::bind(&AutoConnect::_classifyHandle, this, std::placeholders::_1, std::placeholders::_2)); _responsePage->insert(*_webServer); @@ -362,7 +373,8 @@ void AutoConnect::handleRequest() { // An attempt to establish a new AP. AC_DBG("Request for %s\n", (const char*)_credential.ssid); - WiFi.begin((const char*)_credential.ssid, (const char*)_credential.password); +// WiFi.begin((const char*)_credential.ssid, (const char*)_credential.password); + WiFi.begin((const char*)_credential.ssid, (const char*)_credential.password, _apConfig.channel); if (_waitForConnect(_portalTimeout) == WL_CONNECTED) { if (WiFi.BSSID() != NULL) { memcpy(_credential.bssid, WiFi.BSSID(), sizeof(station_config::bssid)); @@ -686,7 +698,7 @@ wl_status_t AutoConnect::_waitForConnect(unsigned long timeout) { } #ifdef AC_DEBUG AC_DEBUG_PORT.print('.'); -#endif // AC_DEBUG +#endif // !AC_DEBUG delay(300); } AC_DBG("%s IP:%s\n", wifiStatus == WL_CONNECTED ? "established" : "time out", WiFi.localIP().toString().c_str()); @@ -699,6 +711,8 @@ wl_status_t AutoConnect::_waitForConnect(unsigned long timeout) { */ void AutoConnect::_disconnectWiFi(bool wifiOff) { #if defined(ARDUINO_ARCH_ESP8266) + if (wifiOff) + ESP.eraseConfig(); WiFi.disconnect(wifiOff); #elif defined(ARDUINO_ARCH_ESP32) WiFi.disconnect(wifiOff, true); diff --git a/src/AutoConnect.h b/src/AutoConnect.h index bea3726..ba6a353 100644 --- a/src/AutoConnect.h +++ b/src/AutoConnect.h @@ -52,7 +52,7 @@ class AutoConnectConfig { netmask(AUTOCONNECT_AP_NM), apid(String(AUTOCONNECT_APID)), psk(String(AUTOCONNECT_PSK)), - channel(1), + channel(AUTOCONNECT_AP_CH), hidden(0), autoSave(AC_SAVECREDENTIAL_AUTO), boundaryOffset(AC_IDENTIFIER_OFFSET), @@ -70,13 +70,13 @@ class AutoConnectConfig { /** * Configure by SSID for the captive portal access point and password. */ - AutoConnectConfig(const char* ap, const char* password) : + AutoConnectConfig(const char* ap, const char* password, const uint8_t channel = AUTOCONNECT_AP_CH) : apip(AUTOCONNECT_AP_IP), gateway(AUTOCONNECT_AP_GW), netmask(AUTOCONNECT_AP_NM), apid(String(ap)), psk(String(password)), - channel(1), + channel(channel), hidden(0), autoSave(AC_SAVECREDENTIAL_AUTO), boundaryOffset(AC_IDENTIFIER_OFFSET), diff --git a/src/AutoConnectDefs.h b/src/AutoConnectDefs.h index 2fb2690..a05e596 100644 --- a/src/AutoConnectDefs.h +++ b/src/AutoConnectDefs.h @@ -50,6 +50,9 @@ #ifndef AUTOCONNECT_AP_NM #define AUTOCONNECT_AP_NM 0x00FFFFFF //*< 255.255.255.0 */ #endif // !AUTOCONNECT_AP_NM +#ifndef AUTOCONNECT_AP_CH +#define AUTOCONNECT_AP_CH 1 +#endif // !AUTOCONNECT_AP_CH // AutoConnect menu root path #ifndef AUTOCONNECT_URI