Merge branch 'enhance/AutoConnectAux' of https://github.com/Hieromon/AutoConnect into enhance/AutoConnectAux

pull/41/head
Hieromon Ikasamo 6 years ago
commit 2ef031f600
  1. 32
      src/AutoConnect.cpp
  2. 6
      src/AutoConnect.h
  3. 3
      src/AutoConnectDefs.h

@ -47,6 +47,7 @@ AutoConnect::AutoConnect(WebServerClass& webServer) {
void AutoConnect::_initialize() { void AutoConnect::_initialize() {
_rfConnect = false; _rfConnect = false;
_rfReset = false; _rfReset = false;
_responsePage = nullptr;
_currentPageElement = nullptr; _currentPageElement = nullptr;
_menuTitle = String(AUTOCONNECT_MENU_TITLE); _menuTitle = String(AUTOCONNECT_MENU_TITLE);
_portalTimeout = AUTOCONNECT_TIMEOUT; _portalTimeout = AUTOCONNECT_TIMEOUT;
@ -88,9 +89,10 @@ bool AutoConnect::begin(const char* ssid, const char* passphrase, unsigned long
_portalTimeout = timeout; _portalTimeout = timeout;
// Start WiFi connection with station mode. // Start WiFi connection with station mode.
WiFi.softAPdisconnect(false); WiFi.softAPdisconnect(true);
WiFi.enableAP(false); WiFi.enableAP(false);
delay(100); _disconnectWiFi(false);
WiFi.setAutoReconnect(false);
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
delay(100); delay(100);
@ -143,9 +145,6 @@ bool AutoConnect::begin(const char* ssid, const char* passphrase, unsigned long
} }
_currentHostIP = WiFi.localIP(); _currentHostIP = WiFi.localIP();
// It doesn't matter the connection status for launching the Web server.
_startWebServer();
// Rushing into the portal. // Rushing into the portal.
if (!cs) { 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 // Change WiFi working mode, Enable AP with STA
WiFi.setAutoConnect(false); WiFi.setAutoConnect(false);
_disconnectWiFi(true); _disconnectWiFi(true);
WiFi.mode(WIFI_AP_STA);
delay(300);
// Connection unsuccessful, launch the captive portal. // 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))) { 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. // Fork to the exit routine that starts captive portal.
cs = _onDetectExit ? _onDetectExit(_currentHostIP) : true; 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. // Start captive portal without cancellation by DetectExit.
if (cs) { if (cs) {
// Prepare for redirecting captive portal detection. // 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; return cs;
} }
@ -312,6 +322,7 @@ void AutoConnect::_startWebServer() {
_webServer->onNotFound(std::bind(&AutoConnect::_handleNotFound, this)); _webServer->onNotFound(std::bind(&AutoConnect::_handleNotFound, this));
// here, Prepare PageBuilders for captive portal // here, Prepare PageBuilders for captive portal
_responsePage = new PageBuilder(); _responsePage = new PageBuilder();
// _responsePage->chunked(PB_ByteStream);
_responsePage->chunked(PB_ByteStream); _responsePage->chunked(PB_ByteStream);
_responsePage->exitCanHandle(std::bind(&AutoConnect::_classifyHandle, this, std::placeholders::_1, std::placeholders::_2)); _responsePage->exitCanHandle(std::bind(&AutoConnect::_classifyHandle, this, std::placeholders::_1, std::placeholders::_2));
_responsePage->insert(*_webServer); _responsePage->insert(*_webServer);
@ -362,7 +373,8 @@ void AutoConnect::handleRequest() {
// An attempt to establish a new AP. // An attempt to establish a new AP.
AC_DBG("Request for %s\n", (const char*)_credential.ssid); 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 (_waitForConnect(_portalTimeout) == WL_CONNECTED) {
if (WiFi.BSSID() != NULL) { if (WiFi.BSSID() != NULL) {
memcpy(_credential.bssid, WiFi.BSSID(), sizeof(station_config::bssid)); memcpy(_credential.bssid, WiFi.BSSID(), sizeof(station_config::bssid));
@ -686,7 +698,7 @@ wl_status_t AutoConnect::_waitForConnect(unsigned long timeout) {
} }
#ifdef AC_DEBUG #ifdef AC_DEBUG
AC_DEBUG_PORT.print('.'); AC_DEBUG_PORT.print('.');
#endif // AC_DEBUG #endif // !AC_DEBUG
delay(300); delay(300);
} }
AC_DBG("%s IP:%s\n", wifiStatus == WL_CONNECTED ? "established" : "time out", WiFi.localIP().toString().c_str()); 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) { void AutoConnect::_disconnectWiFi(bool wifiOff) {
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
if (wifiOff)
ESP.eraseConfig();
WiFi.disconnect(wifiOff); WiFi.disconnect(wifiOff);
#elif defined(ARDUINO_ARCH_ESP32) #elif defined(ARDUINO_ARCH_ESP32)
WiFi.disconnect(wifiOff, true); WiFi.disconnect(wifiOff, true);

@ -52,7 +52,7 @@ class AutoConnectConfig {
netmask(AUTOCONNECT_AP_NM), netmask(AUTOCONNECT_AP_NM),
apid(String(AUTOCONNECT_APID)), apid(String(AUTOCONNECT_APID)),
psk(String(AUTOCONNECT_PSK)), psk(String(AUTOCONNECT_PSK)),
channel(1), channel(AUTOCONNECT_AP_CH),
hidden(0), hidden(0),
autoSave(AC_SAVECREDENTIAL_AUTO), autoSave(AC_SAVECREDENTIAL_AUTO),
boundaryOffset(AC_IDENTIFIER_OFFSET), boundaryOffset(AC_IDENTIFIER_OFFSET),
@ -70,13 +70,13 @@ class AutoConnectConfig {
/** /**
* Configure by SSID for the captive portal access point and password. * 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), apip(AUTOCONNECT_AP_IP),
gateway(AUTOCONNECT_AP_GW), gateway(AUTOCONNECT_AP_GW),
netmask(AUTOCONNECT_AP_NM), netmask(AUTOCONNECT_AP_NM),
apid(String(ap)), apid(String(ap)),
psk(String(password)), psk(String(password)),
channel(1), channel(channel),
hidden(0), hidden(0),
autoSave(AC_SAVECREDENTIAL_AUTO), autoSave(AC_SAVECREDENTIAL_AUTO),
boundaryOffset(AC_IDENTIFIER_OFFSET), boundaryOffset(AC_IDENTIFIER_OFFSET),

@ -50,6 +50,9 @@
#ifndef AUTOCONNECT_AP_NM #ifndef AUTOCONNECT_AP_NM
#define AUTOCONNECT_AP_NM 0x00FFFFFF //*< 255.255.255.0 */ #define AUTOCONNECT_AP_NM 0x00FFFFFF //*< 255.255.255.0 */
#endif // !AUTOCONNECT_AP_NM #endif // !AUTOCONNECT_AP_NM
#ifndef AUTOCONNECT_AP_CH
#define AUTOCONNECT_AP_CH 1
#endif // !AUTOCONNECT_AP_CH
// AutoConnect menu root path // AutoConnect menu root path
#ifndef AUTOCONNECT_URI #ifndef AUTOCONNECT_URI

Loading…
Cancel
Save