diff --git a/src/AutoConnect.cpp b/src/AutoConnect.cpp index 173082c..8794660 100644 --- a/src/AutoConnect.cpp +++ b/src/AutoConnect.cpp @@ -53,6 +53,7 @@ void AutoConnect::_initialize() { _currentPageElement = nullptr; _menuTitle = String(F(AUTOCONNECT_MENU_TITLE)); _connectTimeout = AUTOCONNECT_TIMEOUT; + _scanCount = 0; memset(&_credential, 0x00, sizeof(struct station_config)); #ifdef ARDUINO_ARCH_ESP32 _disconnectEventId = -1; // The member available for ESP32 only @@ -95,10 +96,7 @@ bool AutoConnect::begin(const char* ssid, const char* passphrase, unsigned long _connectTimeout = timeout; // Start WiFi connection with station mode. -#if defined(ARDUINO_ARCH_ESP32) - WiFi.softAPdisconnect(false); -#endif - WiFi.enableAP(false); + WiFi.softAPdisconnect(true); WiFi.mode(WIFI_STA); delay(100); @@ -446,8 +444,9 @@ void AutoConnect::handleRequest() { _disconnectWiFi(true); // An attempt to establish a new AP. - AC_DBG("Request for %s\n", reinterpret_cast(_credential.ssid)); - WiFi.begin(reinterpret_cast(_credential.ssid), reinterpret_cast(_credential.password), _apConfig.channel); + int32_t ch = _connectCh == 0 ? _apConfig.channel : _connectCh; + AC_DBG("Request(%d) for %s\n", (int)ch, reinterpret_cast(_credential.ssid)); + WiFi.begin(reinterpret_cast(_credential.ssid), reinterpret_cast(_credential.password), ch); if (_waitForConnect(_connectTimeout) == WL_CONNECTED) { if (WiFi.BSSID() != NULL) { memcpy(_credential.bssid, WiFi.BSSID(), sizeof(station_config::bssid)); @@ -491,7 +490,7 @@ void AutoConnect::handleRequest() { if (_rfDisconnect) { // Disconnect from the current AP. - _waitForEndTransmission(); +// _waitForEndTransmission(); _stopPortal(); _disconnectWiFi(false); while (WiFi.status() == WL_CONNECTED) { @@ -706,6 +705,16 @@ String AutoConnect::_induceConnect(PageArgument& args) { strncpy(reinterpret_cast(_credential.password), args.arg(AUTOCONNECT_PARAMID_PASS).c_str(), sizeof(_credential.password)); } + // Determine the connection channel based on the scan result. + _connectCh = 0; + for (uint8_t nn = 0; nn < _scanCount; nn++) { + String ssid = WiFi.SSID(nn); + if (!strcmp(ssid.c_str(), reinterpret_cast(_credential.ssid))) { + _connectCh = WiFi.channel(nn); + break; + } + } + // Turn on the trigger to start WiFi.begin(). _rfConnect = true; diff --git a/src/AutoConnect.h b/src/AutoConnect.h index d580a9a..07dadb1 100644 --- a/src/AutoConnect.h +++ b/src/AutoConnect.h @@ -263,6 +263,8 @@ class AutoConnect { AutoConnectConfig _apConfig; struct station_config _credential; uint8_t _hiddenSSIDCount; + int16_t _scanCount; + uint8_t _connectCh; unsigned long _connectTimeout; unsigned long _portalAccessPeriod; diff --git a/src/AutoConnectPage.cpp b/src/AutoConnectPage.cpp index 52fe816..5406f3f 100644 --- a/src/AutoConnectPage.cpp +++ b/src/AutoConnectPage.cpp @@ -40,8 +40,9 @@ const char AutoConnect::_CSS_BASE[] PROGMEM = { "}" ".base-panel>*>label{" "display:inline-block;" - "width:3.0em;" - "text-align:right;" + "font-size:0.9em;" + "margin-left:10px;" + "text-align:left;" "}" "input{" "-moz-appearance:none;" @@ -119,10 +120,10 @@ const char AutoConnect::_CSS_UL[] PROGMEM = { const char AutoConnect::_CSS_ICON_LOCK[] PROGMEM = { ".img-lock{" "display:inline-block;" - "width:24px;" - "height:24px;" - "margin-left:12px;" - "vertical-align:middle;" + "width:22px;" + "height:22px;" + "margin-top:14px;" + "float:right;" "background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAACXBIWXMAAAsTAAALEwEAmpwYAAAB1ElEQVRIibWVu0scURTGf3d2drBQFAWbbRQVCwuVLIZdi2gnWIiF/4GtKyuJGAJh8mgTcU0T8T8ICC6kiIVu44gvtFEQQWwsbExQJGHXmZtiZsOyzCN3Vz+4cDjfvec7j7l3QAF95onRZ54YKmdE1IbnS0c9mnAyAjkBxDy3LRHrjtRyu7OD52HntTAyvbw/HxP2hkCearrRb2WSCSuTTGi60S+QpzFhbwznDl/VVMHw0sF7hEjFbW2qkB38lfp8nNDipWcATil+uDM3cDWyeNRSijnfkHJnezb5Vkkgvbg3IOXD2e1ts93S+icnkZOAVaalZK3YQMa4L+pC6L1WduhYSeCf0PLBdxzOjZ93Lwvm6APAiLmlF1ubPiHotmaS41ExQjH0ZbfNM1NAFpgD0lVcICIrANqAVaAd+AFIYAy4BqaBG+Wsq5AH3vgk8xpYrzf4KLAZwhe8PYEIvQe4vc6H8Hnc2dQs0AFchvAXQGdEDF8s4A5TZS34BQqqQNaS1WMI3KD4WUbNoBJfce9CO7BSr4BfBe8A21vmUwh0VdjdTyHwscL+UK+AHxoD7FDoAX6/Cnpxn4ay/egCjcCL/w1chkqLakLQ/6ABhT57uAd+Vzv/Ara3iY6fK4WxAAAAAElFTkSuQmCC) no-repeat;" "}" }; @@ -141,7 +142,7 @@ const char AutoConnect::_CSS_INPUT_BUTTON[] PROGMEM = { "input[type=\"button\"]{" "background-color:#1b5e20;" "border-color:#1b5e20;" - "width:16em;" + "width:15em;" "}" ".aux-page input[type=\"button\"]{" "font-weight:normal;" @@ -514,7 +515,7 @@ const char AutoConnect::_ELM_MENU_PRE[] PROGMEM = { "
    " "
  • " "MENU_TITLE" - "" + "" "
  • " "
  • Configure new AP
  • " "
  • Open SSIDs
  • " @@ -1097,13 +1098,13 @@ String AutoConnect::_token_LIST_SSID(PageArgument& args) { String ssidList = String(""); _hiddenSSIDCount = 0; WiFi.scanDelete(); - int8_t nn = WiFi.scanNetworks(false, true); - AC_DBG("%d network(s) found\n", (int)nn); - for (uint8_t i = 0; i < nn; i++) { + _scanCount = WiFi.scanNetworks(false, true); + AC_DBG("%d network(s) found\n", (int)_scanCount); + for (uint8_t i = 0; i < _scanCount; i++) { String ssid = WiFi.SSID(i); if (ssid.length() > 0) { ssidList += String(F(""); - ssidList += String(F("")); + ssidList += String(F("")); if (WiFi.encryptionType(i) != ENC_TYPE_NONE) ssidList += String(F("")); ssidList += String(F("
    ")); @@ -1125,12 +1126,11 @@ String AutoConnect::_token_OPEN_SSID(PageArgument& args) { struct station_config entry; String ssidList; String rssiSym; - int16_t wn; uint8_t creEntries = credit.entries(); if (creEntries > 0) { ssidList = String(""); - wn = WiFi.scanNetworks(false, true); + _scanCount = WiFi.scanNetworks(false, true); } else ssidList = String(F("

    No saved credentials.

    ")); @@ -1140,9 +1140,10 @@ String AutoConnect::_token_OPEN_SSID(PageArgument& args) { AC_DBG("A credential #%d loaded\n", (int)i); ssidList += String(F("(entry.ssid)) + String(F("\">")); - for (int8_t sc = 0; sc < wn; sc++) { + for (int8_t sc = 0; sc < (int8_t)_scanCount; sc++) { if (!memcmp(entry.bssid, WiFi.BSSID(sc), sizeof(station_config::bssid))) { - rssiSym = String(AutoConnect::_toWiFiQuality(WiFi.RSSI(sc))) + String(F("%")); + _connectCh = WiFi.channel(sc); + rssiSym = String(AutoConnect::_toWiFiQuality(WiFi.RSSI(sc))) + String(F("% Ch.")) + String(_connectCh) + String(F("")); if (WiFi.encryptionType(sc) != ENC_TYPE_NONE) rssiSym += String(F("")); break;