tzapu / WiFiManager

ESP8266 WiFi Connection manager with web captive portal
http://tzapu.com/esp8266-wifi-connection-manager-library-arduino-ide/
MIT License
6.45k stars 1.94k forks source link

Connect to strongest network signal where duplicate AP's exist. #1707

Open andrewmunro opened 4 months ago

andrewmunro commented 4 months ago

Heya,

I live in a house with terrible wifi signal, and so I have many APs setup with duplicate SSIDs. I've found that when using my ESP32, it seems to frequently connect to a distant AP despite being next to one, resulting in poor signal. I have to keep rebooting it until it finds the AP that is next to it...

I've found a person with a similar issue here. It looks like they solve it by doing a scan and supplying the bssid field to the WiFi.begin method, e.g:

String scanAndConnectToStrongestNetwork() {
  int i_strongest = -1;
  int32_t rssi_strongest = -100;
  Serial.printf("Start scanning for SSID %s\r\n", wifi_ssid);

  int n = WiFi.scanNetworks(); // WiFi.scanNetworks will return the number of networks found
  Serial.println("Scan done.");

  if (n == 0) {
    Serial.println("No networks found!");
    return ("");
  } else {
    Serial.printf("%d networks found:", n);
    for (int i = 0; i < n; ++i) {
      // Print SSID and RSSI for each network found
      Serial.printf("%d: BSSID: %s  %2ddBm, %3d%%  %9s  %s\r\n", i, WiFi.BSSIDstr(i).c_str(), WiFi.RSSI(i), constrain(2 * (WiFi.RSSI(i) + 100), 0, 100), (WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? "open" : "encrypted", WiFi.SSID(i).c_str());
      if ((String(wifi_ssid) == String(WiFi.SSID(i)) && (WiFi.RSSI(i)) > rssi_strongest)) {
        rssi_strongest = WiFi.RSSI(i);
        i_strongest = i;
      }
    }
  }

  if (i_strongest < 0) {
    Serial.printf("No network with SSID %s found!\r\n", wifi_ssid);
    return ("");
  }
  Serial.printf("SSID match found at %d. Connecting...\r\n", i_strongest);
  WiFi.begin(wifi_ssid, wifi_pass, 0, WiFi.BSSID(i_strongest));
  return (WiFi.BSSIDstr(i_strongest));
}

Could we add an option for this to the connection made in https://github.com/tzapu/WiFiManager/blob/master/WiFiManager.cpp#L1113? This is a really cool library and I'd love to continue using it, but I may have to just hard code the AP for now and implement something similar above...