- ----------------- 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 --------------
Establish WiFi connection to AP with strongest radio signal
-
- Posts: 5
- Joined: Tue Jan 12, 2021 11:04 pm
Establish WiFi connection to AP with strongest radio signal
Dear all,
I started a week ago with ESP32-Camera module (AI Thinker) and the application should send a photo by eMail every once in a while. The code is generally working, however, I have huge problems with the WiFi connection.
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 ...
I have a simple code basically unsing WiFi.begin(SSID, PWD) to connect to WiFi.
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 during eMial sending.
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 network?
Thanks al lot for a working solution,
Thomas
I started a week ago with ESP32-Camera module (AI Thinker) and the application should send a photo by eMail every once in a while. The code is generally working, however, I have huge problems with the WiFi connection.
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 ...
I have a simple code basically unsing WiFi.begin(SSID, PWD) to connect to WiFi.
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 during eMial sending.
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 network?
Thanks al lot for a working solution,
Thomas
Re: Establish WiFi connection to AP with strongest radio signal
I have the same module, and like others I am experiencing wifi issues even with the board a few feet from the router (the only router).
I think it is a hardware issue, because other boards, like the m5stack camera board works fine.
I think it is a hardware issue, because other boards, like the m5stack camera board works fine.
-
- Posts: 5
- Joined: Tue Jan 12, 2021 11:04 pm
Re: Establish WiFi connection to AP with strongest radio signal
Hi Radu79,
thanks - does this mean that other ESP32 boards do actually connect automatically to the strongest AP at a given SSID? It is hard for me to think of an hardware issue as connecting to the strongest AP needs the implementation of a proper algorithm to 1st scan the network for all available APs and then connect to the one with highest signal strength.
As a side note: I downloaded a short sketch that does a scan and it shows all available APs with SSID and signal strength. So, the Module „Sees“ the APs, but it does not connect to the strongest AP.
Unfortunately, I was unable to get the BSSID of the different APs in this scan. And even when knowing the BSSID of a specific AP, I was unable to connect to that particular AP. WiFi.begin(SSID, Password, 0, Bssid) did not work at all.
If someone knows how to scan the wlan for all APs including the BSSIDs and then connecting to a specific BSSID, I could easily write the algorithm that finds the strongest and connect. However, I am completely stuck now:
- the ESP32 does not automatically to the strongest AP
- I can scan the network and I find all APs, but I get only the SSID and Signal strength, but no BSSID, so, I can’t distinguish between the different APs (with identical SSID but different location and therefore different signal strength)
- even if I know the BSSID from the router, I can’t connect to that AP by using the BSSID.
So many great things work easily with ESP32 but I am surprised that such a simple thing like connecting to the strongest AP in a network with several APs with identical SSID dies not simply work ...
thanks - does this mean that other ESP32 boards do actually connect automatically to the strongest AP at a given SSID? It is hard for me to think of an hardware issue as connecting to the strongest AP needs the implementation of a proper algorithm to 1st scan the network for all available APs and then connect to the one with highest signal strength.
As a side note: I downloaded a short sketch that does a scan and it shows all available APs with SSID and signal strength. So, the Module „Sees“ the APs, but it does not connect to the strongest AP.
Unfortunately, I was unable to get the BSSID of the different APs in this scan. And even when knowing the BSSID of a specific AP, I was unable to connect to that particular AP. WiFi.begin(SSID, Password, 0, Bssid) did not work at all.
If someone knows how to scan the wlan for all APs including the BSSIDs and then connecting to a specific BSSID, I could easily write the algorithm that finds the strongest and connect. However, I am completely stuck now:
- the ESP32 does not automatically to the strongest AP
- I can scan the network and I find all APs, but I get only the SSID and Signal strength, but no BSSID, so, I can’t distinguish between the different APs (with identical SSID but different location and therefore different signal strength)
- even if I know the BSSID from the router, I can’t connect to that AP by using the BSSID.
So many great things work easily with ESP32 but I am surprised that such a simple thing like connecting to the strongest AP in a network with several APs with identical SSID dies not simply work ...
-
- Posts: 5
- Joined: Tue Jan 12, 2021 11:04 pm
Re: Establish WiFi connection to AP with strongest radio signal
I checked the WiFi connection now with an ESP32 dev board, no camera. Same problem, so, not linked to the camera. I checked with the following code and from the results one can see:
As one can see:
- I want to connect to Laengenau network
- there are 5 AP with this SSID visible
- one with 100% is only 2 m away from the ESP
- the other fife are much further away and have only a weak signal
- the ESP almost never connects to the strongest one right next to it, it chooses one of the weaker APs
- I don’t get the BSSID and I can’t connect to anspecific AP using the BSSID
I get the following result:
Any help much appreciated.
As one can see:
- I want to connect to Laengenau network
- there are 5 AP with this SSID visible
- one with 100% is only 2 m away from the ESP
- the other fife are much further away and have only a weak signal
- the ESP almost never connects to the strongest one right next to it, it chooses one of the weaker APs
- I don’t get the BSSID and I can’t connect to anspecific AP using the BSSID
- #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 () {}
Any help much appreciated.
Re: Establish WiFi connection to AP with strongest radio signal
Hello Thomas
I don't have a ready made solution, but if you look at https://github.com/espressif/arduino-es ... WiFiScan.h you'll see the WiFi API, eg.
And calling should give you the BSSID string of the scanned AP whereas gives you the BSSID pointer needed for
Cheers
Felix
I don't have a ready made solution, but if you look at https://github.com/espressif/arduino-es ... WiFiScan.h you'll see the WiFi API, eg.
Code: Select all
String BSSIDstr(uint8_t networkItem);
And calling
Code: Select all
WiFi.BSSIDstr(i);
Code: Select all
WiFi.BSSID(i);
Code: Select all
WiFi.begin()
Felix
-
- Posts: 5
- Joined: Tue Jan 12, 2021 11:04 pm
Re: Establish WiFi connection to AP with strongest radio signal
Hi Felix,
thanks for the help - I got it working now and I will post the solution here after some cleanup.
Thomas
thanks for the help - I got it working now and I will post the solution here after some cleanup.
Thomas
-
- Posts: 1
- Joined: Sat Feb 06, 2021 9:22 pm
Re: Establish WiFi connection to AP with strongest radio signal
I'm very interested to see your implementation. Thanks
-
- Posts: 5
- Joined: Tue Jan 12, 2021 11:04 pm
Re: Establish WiFi connection to AP with strongest radio signal
Hi there,
I am happy to share my solution, not as a complete sketch but with two procedures that are useful. There are some #definitions needed which I hope speak for themselves.
connect2nearestAP does hat it says: scanning the network for all available SSIDs on all APs (the list is fortunately sorted from stronest to weakest) and connects to the first/strongest with matching SSID.
checkWiFi is helpful if the ESP is moving around and the nearest AP is changing. This routine should be called on a regular basis and/or if highest thruput is needed (e.g. you want to send an eMail with attachments). This routine checks if the strengths is above a certain tresold - if not, it calls connect2nearestAP.
The code looks awkward in many places - one reason ist that I started programming in C only a few month ago The other is, that there are many flaws in the ESP WiFi realization, especially in the ESP-CAM-Module. So, when I call routines again and agian, make delays, ... than this is the distillation of things I found in different discussions about the unreliable ESP32 WiFi ... with all these things it works now mostly reliable on three ESP32-CAM. And yes, my three ESP32-CAM modules do not behave in the same way. One connects to WiFi only every second reboot, one has a significantly lower signal strength than the other two. But if you google around, it seems to be normal ...
I am happy to share my solution, not as a complete sketch but with two procedures that are useful. There are some #definitions needed which I hope speak for themselves.
connect2nearestAP does hat it says: scanning the network for all available SSIDs on all APs (the list is fortunately sorted from stronest to weakest) and connects to the first/strongest with matching SSID.
checkWiFi is helpful if the ESP is moving around and the nearest AP is changing. This routine should be called on a regular basis and/or if highest thruput is needed (e.g. you want to send an eMail with attachments). This routine checks if the strengths is above a certain tresold - if not, it calls connect2nearestAP.
The code looks awkward in many places - one reason ist that I started programming in C only a few month ago The other is, that there are many flaws in the ESP WiFi realization, especially in the ESP-CAM-Module. So, when I call routines again and agian, make delays, ... than this is the distillation of things I found in different discussions about the unreliable ESP32 WiFi ... with all these things it works now mostly reliable on three ESP32-CAM. And yes, my three ESP32-CAM modules do not behave in the same way. One connects to WiFi only every second reboot, one has a significantly lower signal strength than the other two. But if you google around, it seems to be normal ...
Code: Select all
#define MaxSecWiFi 30 // max time in s for WiFi setup, function will return WiFiErrorCode if WiFi was not successful.
#define WiFiErrorCode 1000 // see above
#define SaveDisconnectTime 1000 // Time im ms for save disconnection, needed to avoid that WiFi works only evey secon boot: https://github.com/espressif/arduino-esp32/issues/2501
#define WiFiTime 10 // Max time in s for successful WiFi connection.
#define WiFiOK 0 // WiFi connected
#define WiFiTimedOut 1 // WiFi connection timed out
#define WiFiNoSSID 2 // The SSID was not found during network scan
#define WiFiRSSI -75 // Min. required signal strength for a good WiFi cennection. If lower, new scan is initiated in checkWiFi()
int connect2nearestAP()
{
// this routine disconnects from WiFi, scans the network for the strongest AP at the defined SSID and tries to connect. returns WiFiOK, WiFiTimedOut, or WiFiNoSSID
int n;
int i = 0;
// the following awkward code seems to work to avoid the following bug in ESP32 WiFi connection: https://github.com/espressif/arduino-esp32/issues/2501
WiFi.disconnect(true); // delete old config
WiFi.persistent(false); //Avoid to store Wifi configuration in Flash
WiFi.mode(WIFI_OFF);
WiFi.mode(WIFI_STA);
WiFi.disconnect(true); // delete old config
WiFi.setHostname(HostName);
WiFi.begin();
delay(SaveDisconnectTime); // 500ms seems to work in most cases, may depend on AP
WiFi.disconnect(true); // delete old config
Serial.println("scan start");
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("BSSID: ");
Serial.print(WiFi.BSSIDstr(i));
Serial.print(" ");
Serial.print(WiFi.RSSI(i));
Serial.print("dBm, ");
Serial.print(constrain(2 * (WiFi.RSSI(i) + 100), 0, 100));
Serial.print("% ");
Serial.print((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? "open " : "encrypted ");
Serial.println(WiFi.SSID(i));
delay(10);
}
}
Serial.println();
// while ( !strcmp(ssid, &WiFi.SSID(i)) && (i < n)) {
while ( String(ssid) != String(WiFi.SSID(i)) && (i < n)) {
i++;
}
if (i == n) {
Serial.print("No network with SSID ");
Serial.println(ssid);
Serial.print(" found!");
return WiFiNoSSID;
}
Serial.print("SSID match found at: ");
Serial.println(i + 1);
WiFi.setHostname(HostName);
WiFi.begin(ssid, password, 0, WiFi.BSSID(i));
i = 0;
Serial.print("Connecting ");
while ((WiFi.status() != WL_CONNECTED) && (i < WiFiTime * 10)) {
delay(100);
Serial.print(".");
i++;
}
Serial.println();
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Connection to ");
Serial.print(ssid);
Serial.println(" timed out! ");
return WiFiTimedOut;
}
Serial.print("Connected to BSSID: ");
Serial.print(WiFi.BSSIDstr());
Serial.print(" ");
Serial.print(WiFi.RSSI());
Serial.print("dBm, ");
Serial.print(constrain(2 * (WiFi.RSSI() + 100), 0, 100));
Serial.print("% ");
Serial.print(WiFi.SSID());
Serial.print(" IP: ");
Serial.println(WiFi.localIP());
return (WiFiOK);
}
void checkWiFi() { // Check the signal strength of the connection - if too low, reconnect to the strongest AP; good if the ESP is moving around and reconnection to the nearest AP makes sense
int i = 1;
if ((WiFi.status() == WL_CONNECTED) && (WiFi.RSSI() > WiFiRSSI) && (WiFi.RSSI() < -25)) { // good connection
Serial.print("Connection good: ");
Serial.print(WiFi.RSSI());
Serial.print("dBm, ");
Serial.print(constrain(2 * (WiFi.RSSI() + 100), 0, 100));
Serial.print("% ");
Serial.println(" ... keep geoing");
return;
}
while (connect2nearestAP() != WiFiOK) {
Serial.print("connect2nearestAP was called #");
Serial.println(i);
delay(1000);
i++;
if (i > 10) {
Serial.println("Rebooting cause WiFi setup failes");
ESP.restart();
}
}
}
Re: Establish WiFi connection to AP with strongest radio signal
Hello and I am glad that your solution is working.
May I suggest our solution as an alternative.
We have a situation where there are many AP's throughout our facility with the same SSID.
It is important for our esp32 device to connect to the strongest AP with the appropriate SSID.
To accomplish this we simply set the sort method for AP selection to WIFI_CONNECT_AP_BY_SIGNAL
See below.
We also found it important to select ALL_CHANNEL_SCAN so it did not stop on the first match, which may nbot be the strongest signal.
wifi_config_t sta_config = {
.sta = {
.ssid = CONNECT_TO_SSID,
.password = CONNECT_TO_PW,
.scan_method = WIFI_ALL_CHANNEL_SCAN,
.sort_method = WIFI_CONNECT_AP_BY_SIGNAL,
.bssid_set = false
}
I hope this may also help.
Regards
};
May I suggest our solution as an alternative.
We have a situation where there are many AP's throughout our facility with the same SSID.
It is important for our esp32 device to connect to the strongest AP with the appropriate SSID.
To accomplish this we simply set the sort method for AP selection to WIFI_CONNECT_AP_BY_SIGNAL
See below.
We also found it important to select ALL_CHANNEL_SCAN so it did not stop on the first match, which may nbot be the strongest signal.
wifi_config_t sta_config = {
.sta = {
.ssid = CONNECT_TO_SSID,
.password = CONNECT_TO_PW,
.scan_method = WIFI_ALL_CHANNEL_SCAN,
.sort_method = WIFI_CONNECT_AP_BY_SIGNAL,
.bssid_set = false
}
I hope this may also help.
Regards
};
-
- Posts: 2
- Joined: Sat Mar 06, 2021 7:14 pm
Re: Establish WiFi connection to AP with strongest radio signal
@FluxGS we have also tried this method, but it is still connecting to AP with a weaker RSSI value. Do you have any clue why is that? We are using esp-idf version 4.4.2.
Any help would be much appreciated!
Thank you,
Best regards
S
Any help would be much appreciated!
Thank you,
Best regards
S
Who is online
Users browsing this forum: Bing [Bot] and 37 guests