diff --git a/src/AutoConnect.cpp b/src/AutoConnect.cpp index 669f727..07016a2 100644 --- a/src/AutoConnect.cpp +++ b/src/AutoConnect.cpp @@ -121,6 +121,14 @@ bool AutoConnect::begin(const char* ssid, const char* passphrase, unsigned long } AC_DBG("WiFi.begin(%s%s%s)\n", ssid == nullptr ? "" : ssid, passphrase == nullptr ? "" : ",", passphrase == nullptr ? "" : passphrase); cs = _waitForConnect(_connectTimeout) == WL_CONNECTED; +#ifdef INC_RSSI_USE_SUP //!!! + if(cs==true) { + if(WiFi.RSSI()<_apConfig.conMinRSSI || _apConfig.conFindMaxRSSI==true) { + _disconnectWiFi(false); + cs=false; + } + } +#endif } // Reconnect with a valid credential as the autoReconnect option is enabled. @@ -625,13 +633,46 @@ bool AutoConnect::_loadAvailCredential(const char* ssid) { AC_DBG("%d network(s) found\n", (int)nn); if (nn > 0) { // Determine valid credentials by BSSID. +#ifdef INC_RSSI_USE_SUP //!!! + int maxRSSIWifiIndex=-1; + int maxRSSICredIndex=-1; +#endif for (uint8_t i = 0; i < credential.entries(); i++) { credential.load(i, &_credential); for (uint8_t n = 0; n < nn; n++) { +#ifdef INC_RSSI_USE_SUP //!!! + if (!memcmp(_credential.bssid, WiFi.BSSID(n), sizeof(station_config_t::bssid))) { + + if(WiFi.RSSI(n)>=_apConfig.conMinRSSI) { + if(_apConfig.conFindMaxRSSI!=true) { + return true; + } + + if(maxRSSIWifiIndex<0) { + maxRSSIWifiIndex=n; + maxRSSICredIndex=i; + } else { + if(WiFi.RSSI(n)>WiFi.RSSI(maxRSSIWifiIndex)) { + maxRSSIWifiIndex=n; + maxRSSICredIndex=i; + } + } + + } + + } +#else if (!memcmp(_credential.bssid, WiFi.BSSID(n), sizeof(station_config_t::bssid))) return true; +#endif } } +#ifdef INC_RSSI_USE_SUP //!!! + if(maxRSSIWifiIndex>=0) { + credential.load(maxRSSICredIndex, &_credential); + return true; + } +#endif } } else if (strlen(ssid)) diff --git a/src/AutoConnect.h b/src/AutoConnect.h index 1f34f02..60beba6 100644 --- a/src/AutoConnect.h +++ b/src/AutoConnect.h @@ -58,6 +58,10 @@ class AutoConnectConfig { * assigned from macro. Password is same as above too. */ AutoConnectConfig() : +#ifdef INC_RSSI_USE_SUP //!!! + conMinRSSI(AUTOCONNECT_CON_MIN_RSSI), + conFindMaxRSSI(AUTOCONNECT_CON_FIND_MAX_RSSI), +#endif apip(AUTOCONNECT_AP_IP), gateway(AUTOCONNECT_AP_GW), netmask(AUTOCONNECT_AP_NM), @@ -90,6 +94,10 @@ class AutoConnectConfig { * Configure by SSID for the captive portal access point and password. */ AutoConnectConfig(const char* ap, const char* password, const unsigned long portalTimeout = 0, const uint8_t channel = AUTOCONNECT_AP_CH) : +#ifdef INC_RSSI_USE_SUP //!!! + conMinRSSI(AUTOCONNECT_CON_MIN_RSSI), + conFindMaxRSSI(AUTOCONNECT_CON_FIND_MAX_RSSI), +#endif apip(AUTOCONNECT_AP_IP), gateway(AUTOCONNECT_AP_GW), netmask(AUTOCONNECT_AP_NM), @@ -122,6 +130,10 @@ class AutoConnectConfig { ~AutoConnectConfig() {} AutoConnectConfig& operator=(const AutoConnectConfig& o) { +#ifdef INC_RSSI_USE_SUP //!!! + conMinRSSI=o.conMinRSSI; + conFindMaxRSSI=o.conFindMaxRSSI; +#endif apip = o.apip; gateway = o.gateway; netmask = o.netmask; @@ -153,6 +165,11 @@ class AutoConnectConfig { return *this; } + +#ifdef INC_RSSI_USE_SUP //!!! + int conMinRSSI; /**< Minimum AP signal strength accepted for connection */ + bool conFindMaxRSSI; /**< Find stored AP with highest signal strength for connection */ +#endif IPAddress apip; /**< SoftAP IP address */ IPAddress gateway; /**< SoftAP gateway address */ IPAddress netmask; /**< SoftAP subnet mask */ diff --git a/src/AutoConnectDefs.h b/src/AutoConnectDefs.h index 3c407eb..1d10455 100644 --- a/src/AutoConnectDefs.h +++ b/src/AutoConnectDefs.h @@ -10,6 +10,19 @@ #ifndef _AUTOCONNECTDEFS_H_ #define _AUTOCONNECTDEFS_H_ +//!!! +#define INC_RSSI_USE_SUP 1 + +#ifdef INC_RSSI_USE_SUP //!!! +#ifndef AUTOCONNECT_CON_MIN_RSSI +#define AUTOCONNECT_CON_MIN_RSSI -100 // no limit +#endif + +#ifndef AUTOCONNECT_CON_FIND_MAX_RSSI +#define AUTOCONNECT_CON_FIND_MAX_RSSI false // use first available +#endif +#endif + // Uncomment the following AC_DEBUG to enable debug output. //#define AC_DEBUG @@ -25,6 +38,7 @@ #define AC_DBG_DUMB(...) #endif // !AC_DEBUG + // Indicator to specify that AutoConnectAux handles elements with JSON. // Comment out the AUTOCONNECT_USE_JSON macro to detach the ArduinoJson. #ifndef AUTOCONNECT_NOUSE_JSON