ESP8266 device connect to strongest WiFi

I have a few boards used as data collectors, which are based on the ESP07S modules.
The sketch code is pretty standard with the usual WiFi connect code.
Now I have discovered that the devices seem not to connect to the strongest WiFi available and I wonder if there is a way to make them do that?

The data collected are sent by MQTT to a broker for handling and I have added an item for the WiFi strength so I can check it and it turns out that sometimes the device switches to an access point at a longer distance and hence a weaker signal. I have graphed the varying signal level from the database where it winds up and here is how it looks like for the last 10 days:

When I installed the system the WiFi stayed around -70 to -77 dBm or so for a while and operation was stable, but recently I have had several reboots (probably triggered by low WiFi signal) and then I discovered the low signal level...
The data collector is located outside my house and is about 15 m from it and it is supposed to use the closest access point located in the room inside of the closest part of the house. But it seems to connect to the AP at the far end of the house some 15 m farther away and thus getting a signal strength that is almost as low as -90 dBm

Both AP:s use the same SSID so it is not possible to change the connection by SSID value.
Is there a way to code the ESP07S such that it will scan the available AP:s using the same SSID and select the strongest one to connect to?

This is the current connect code used:

    WiFi.mode(WIFI_STA);
    WiFi.setPhyMode(WIFI_PHY_MODE_11G);
    WiFi.hostname(MyHostname);
    WiFi.begin(MySSID, MyPasswd);

The setPhyMode call was added in order to stabilize the connection in the presence of a more advanced WiFi router with automatic 2.4/5 Gb switching...

Then how to tell them apart ?

There is a way to scan and determine which one is the strangest, but in the end it's

WiFi.begin(SSID,PASSWORD);

so you can not select, and chances are it will select the AP scanned first which may not be the one which is strongest. I'd say the way would be to give them a unique name, possibly with a standard prefix. I use the Chip-id.

The Chip-ID pertains to the ESP07S, right?
I use that as the hostname but it does not apply to the router itself, right?

Anyway the channels are possible to separate using the mac address of the two ASUS routers, they are definitely different.
But how to use during connection (if possible)?

I have now rebooted the AP which is the closest to the ESP07S device and strangely it now seems to have connected to that AP rather than the further away located (and weaker) main router's WiFi.
It has been on the stronger signal now for more than 19 hours...
Strange, I would have suspected that while the AP rebooted the ESP would have connected to the router instead and stayed there afterwards. But it seems like the opposite happened.

I that's terrible, now you don't have a problem anymore :wink:

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