ESP32: WiFi does not connect to nearest AP (5 APs with same SSID)

We live an a large farmers house with barns ad I need fife AP (all with the same SSID) to get good coverage of the property. The network is set up with a FritzBox using Fritz Mesh. It works perfect with all our mobile devices ...

What I discover is, that the ESP32 connects in an unpredictable way to any on the APs and most often to those far away. This leads to very poor network speed and often, connection is lost.

I googled a lot and found that people frequently report such issues since years but I did not find any working solution. I even tried to Connect with WiFi.begin(SSID, pwd, 0, Bssid) and selected the Bssid of the AP which was just two meters away - this did not get any connection at all.

Does anybody know a working solution for a networks with several AP with the same SSID and getting the ESP32 connecting automatically to the strongest AP or is it known the the ESP32 with the WiFi.h lib is useless in such a common network with multiple APs with same SSID?

Please refer to the code and the result. What you can see:

  • I try to connect to the SSID Lanegenau
  • there are five Laengnau APs visible, one with 100% signal which is about two meters away from the ESP, the others are further away and much weaker
  • The ESP does not connect to the AP nearby - it connects in almost all cases to a very weak one making the connection unreliable
  • The BSSID-function does not work at all - so, the fifve "Lanegenau"-APs look all identical to the code and I do not see a chance to scan the network myself and find the strongest AP to connect with this one.
  • Trying to connect to an AP with known BSSID dies also not work (I have commented the code line out)

Any help much appreciated!

#include <WiFi.h>

#include "TD_Credentials.h"

byte bssid[] = {0x32, 0xC9, 0x29, 0xAB, 0x91, 0x4E}; // Repeater FeWo, 2.4 GHz
//byte bssid[] = {0x4E, 0x91, 0xAB, 0x29, 0xC9, 0x32}; // Repeater FeWo, 2.4 GHz

void setup()
{

  int i = 0;
  Serial.begin(115200);
  Serial.println("----------------- Start Setup --------------");

  scanNetwork();

  WiFi.begin(ssid, password);
  //WiFi.begin(ssid, password, 0, bssid); // was hoping to connect die an AP with defined BSSID - dies not work ...

  Serial.print("Connecting ");
  while ((WiFi.status() != WL_CONNECTED) && (i < 500)) {
    delay(500);
    Serial.print(".");
    i++;
  }

  Serial.println("!");

  Serial.println();

  Serial.print(WiFi.SSID());
  Serial.print(" (");
  Serial.print(WiFi.RSSI());
  Serial.print("dBm, ");
  Serial.print(constrain(2 * (WiFi.RSSI() + 100), 0, 100));
  Serial.print("%)      ");
  Serial.println();

  // shoud print the MAC address of the router you're attached to, does not work!!!!
  byte bssid[6];
  WiFi.BSSID(*bssid);
  Serial.print("BSSID: ");
  Serial.print(bssid[5], HEX);
  Serial.print(":");
  Serial.print(bssid[4], HEX);
  Serial.print(":");
  Serial.print(bssid[3], HEX);
  Serial.print(":");
  Serial.print(bssid[2], HEX);
  Serial.print(":");
  Serial.print(bssid[1], HEX);
  Serial.print(":");
  Serial.println(bssid[0], HEX);
  Serial.println("----------------- End Setup --------------");
}

void scanNetwork()
{
  Serial.println("scan start");
  byte bssid[6];
  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");
  } else {
    Serial.print(n);
    Serial.println(" networks found");
    for (int i = 0; i < n; ++i) {
      // Print SSID and RSSI for each network found
      Serial.print(i + 1);
      Serial.print(": ");
      Serial.print(WiFi.SSID(i));
      Serial.print(" (");
      Serial.print(WiFi.RSSI(i));
      Serial.print("dBm, ");
      Serial.print(constrain(2 * (WiFi.RSSI(i) + 100), 0, 100));
      Serial.print("%)      ");

      WiFi.BSSID(*bssid);
      Serial.print("BSSID: ");
      Serial.print(bssid[5], HEX);
      Serial.print(":");
      Serial.print(bssid[4], HEX);
      Serial.print(":");
      Serial.print(bssid[3], HEX);
      Serial.print(":");
      Serial.print(bssid[2], HEX);
      Serial.print(":");
      Serial.print(bssid[1], HEX);
      Serial.print(":");
      Serial.print(bssid[0], HEX);
      Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " open" : " encrypted");
      delay(10);
    }
  }
  Serial.println("");


}
void loop () {}


---------------------------------------------------
----------------- Start Setup --------------
scan start
scan done
10 networks found
1: Laengenau (-46dBm, 100%)      BSSID: 0:0:0:0:0:0 encrypted
2: Dennerguest (-47dBm, 100%)      BSSID: 0:0:0:0:0:0 encrypted
3: Laengenau (-74dBm, 52%)      BSSID: 0:0:0:0:0:0 encrypted
4: Dennerguest (-75dBm, 50%)      BSSID: 0:0:0:0:0:0 encrypted
5: Laengenau (-85dBm, 30%)      BSSID: 0:0:0:0:0:0 encrypted
6: Dennerguest (-87dBm, 26%)      BSSID: 0:0:0:0:0:0 encrypted
7: Laengenau (-88dBm, 24%)      BSSID: 0:0:0:0:0:0 encrypted
8: Dennerguest (-89dBm, 22%)      BSSID: 0:0:0:0:0:0 encrypted
9: Dennerguest (-90dBm, 20%)      BSSID: 0:0:0:0:0:0 encrypted
10: Laengenau (-93dBm, 14%)      BSSID: 0:0:0:0:0:0 encrypted

Connecting .!

Laengenau (-74dBm, 52%)      
BSSID: 0:0:0:0:0:0
----------------- End Setup --------------

I think you can fix the BSSID display issue as follows:

void setup()
...
  byte *bssid = WiFi.BSSID();
  Serial.print("BSSID: ");
...
void scanNetwork()
...
      byte *bssid = WiFi.BSSID(i);
      Serial.print("BSSID: ");

You could try using the access point connection method as shown in the WiFiMulti example, so Arduino IDE -> File -> Examples -> WiFi -> WiFiMulti. I think it will auto select the best AP even when you only specify one set of connection arguments.

Make sure you are using WiFi.begin() with the BSSID the right way around.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.