Add homeassisstant compatibility and choose strongest AP on wifi connection.
This commit is contained in:
@@ -1059,7 +1059,12 @@ uint8_t WiFiManager::connectWifi(String ssid, String pass, bool connect) {
|
||||
else {
|
||||
// connect using saved ssid if there is one
|
||||
if (WiFi_hasAutoConnect()) {
|
||||
wifiConnectDefault();
|
||||
if (_findBestRSSI) {
|
||||
wifiConnectNew(WiFi_SSID(), WiFi_psk(), connect);
|
||||
} else {
|
||||
// connect to saved ssid
|
||||
wifiConnectDefault();
|
||||
}
|
||||
connRes = waitForConnectResult();
|
||||
}
|
||||
else {
|
||||
@@ -1110,7 +1115,62 @@ bool WiFiManager::wifiConnectNew(String ssid, String pass,bool connect){
|
||||
#endif
|
||||
WiFi_enableSTA(true,storeSTAmode); // storeSTAmode will also toggle STA on in default opmode (persistent) if true (default)
|
||||
WiFi.persistent(true);
|
||||
ret = WiFi.begin(ssid.c_str(), pass.c_str(), 0, NULL, connect);
|
||||
|
||||
|
||||
if (_findBestRSSI) {
|
||||
if (!_numNetworks)
|
||||
WiFi_scanNetworks(); // scan in case this gets called before any scans
|
||||
|
||||
int n = _numNetworks;
|
||||
if (n == 0) {
|
||||
#ifdef WM_DEBUG_LEVEL
|
||||
DEBUG_WM(F("No networks found"));
|
||||
#endif
|
||||
} else {
|
||||
#ifdef WM_DEBUG_LEVEL
|
||||
DEBUG_WM(n, F("networks found"));
|
||||
#endif
|
||||
int bestConnection = -1;
|
||||
// Find best RSSI AP for given SSID
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (ssid == WiFi.SSID(i)) {
|
||||
#ifdef WM_DEBUG_LEVEL
|
||||
DEBUG_WM(String(F("SSID ")) + ssid + String(F(" found with RSSI: ")) +
|
||||
String(WiFi.RSSI(i)) + String(F("(")) +
|
||||
String(constrain((100.0 + WiFi.RSSI(i)) * 2, 0, 100)) +
|
||||
String(F(" %) and BSSID: ")) + WiFi.BSSIDstr(i) +
|
||||
String(F(" and channel: ")) + String(WiFi.channel(i)));
|
||||
#endif
|
||||
if (bestConnection == -1) {
|
||||
bestConnection = i;
|
||||
} else {
|
||||
if (WiFi.RSSI(i) > WiFi.RSSI(bestConnection)) {
|
||||
bestConnection = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (bestConnection == -1) {
|
||||
#ifdef WM_DEBUG_LEVEL
|
||||
DEBUG_WM(F("No network found with SSID: "), ssid);
|
||||
#endif
|
||||
} else {
|
||||
#ifdef WM_DEBUG_LEVEL
|
||||
DEBUG_WM(String(F("Trying to connect to SSID ")) + ssid + String(F(" found with RSSI: ")) +
|
||||
String(WiFi.RSSI(bestConnection)) + String(F("(")) +
|
||||
String(constrain((100.0 + WiFi.RSSI(bestConnection)) * 2, 0, 100)) +
|
||||
String(F(" %) and BSSID: ")) + WiFi.BSSIDstr(bestConnection) +
|
||||
String(F(" and channel: ")) + String(WiFi.channel(bestConnection)));
|
||||
#endif
|
||||
ret = WiFi.begin(ssid.c_str(), pass.c_str(), 0,
|
||||
WiFi.BSSID(bestConnection), connect);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ret = WiFi.begin(ssid.c_str(), pass.c_str(), 0, NULL, connect);
|
||||
}
|
||||
|
||||
|
||||
WiFi.persistent(false);
|
||||
#ifdef WM_DEBUG_LEVEL
|
||||
if(!ret) DEBUG_WM(WM_DEBUG_ERROR,F("[ERROR] wifi begin failed"));
|
||||
@@ -3138,6 +3198,12 @@ String WiFiManager::getWiFiHostname(){
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* toggle showing find best RSSID
|
||||
* @param boolean enabled
|
||||
*/
|
||||
void WiFiManager::setFindBestRSSI(boolean enabled) { _findBestRSSI = enabled; }
|
||||
|
||||
/**
|
||||
* [setTitle description]
|
||||
* @param String title, set app title
|
||||
|
||||
@@ -431,6 +431,7 @@ class WiFiManager
|
||||
// clean connect, always disconnect before connecting
|
||||
void setCleanConnect(bool enable); // default false
|
||||
|
||||
void setFindBestRSSI(boolean enabled);
|
||||
// set custom menu items and order, vector or arr
|
||||
// see _menutokens for ids
|
||||
void setMenu(std::vector<const char*>& menu);
|
||||
@@ -531,6 +532,7 @@ class WiFiManager
|
||||
unsigned long _lastscan = 0; // ms for timing wifi scans
|
||||
unsigned long _startscan = 0; // ms for timing wifi scans
|
||||
unsigned long _startconn = 0; // ms for timing wifi connects
|
||||
boolean _findBestRSSI = true; // find best rssi ap in wifiscan
|
||||
|
||||
// defaults
|
||||
const byte DNS_PORT = 53;
|
||||
|
||||
Reference in New Issue
Block a user