esp8266 connect when starting to which AP?

I have a device built with an ESP-07 module and it is configured to connect to a certain access point using the SSID and password programmed into the EEPROM config data.
This device goes into deep sleep between measurements and is reset to wake up every hour.

In my location there is a WiFi network consisting of two WiFi routers wired together with Ethernet cable. One is the main router with the DHCP server, gateway etc. The other is a "slave" router operating as an extension of the main (via the cable) and uses the same SSID and password. DHCP calls are routed to the main router via the cable.

Now my question is:
Which AP will the ESP8266 connect to on wakeup since both uses the same SSID and password?
The signal strength depends on the location of the device, i.e. which AP it is closest to. When connected the ESP will be on the same network in both cases so it would be best if it chooses the AP with the highest signal strength.
But does it?
Or will the ESP8266 subsystem (outside of Arduino programmability) try to connect to the AP with the same MAC address as the last connect in this case? If so it will hold on to a WiFi AP with much lower signal quality and strength if it is moved to a different location between deep sleep awakes...

But does it?

dunno, you actually have the opportunity to find out ! But if you want it to connect to the strongest one

ESP8266 subsystem (outside of Arduino programmability)

nothing is outside of that. Also why would the one that is the strongest when deepsleep starts no longer be the strongest at wake-up ? are you moving the device ? (or the routers ??)

are you moving the device ?

I thought that that was obvious. At least, that was the assumption I made, without even thinking about it.

I think the real question is why would anyone set up two access points with the same name, if they cared about knowing which one was connected to.

Deva_Rishi:
dunno, you actually have the opportunity to find out ! But if you want it to connect to the strongest one nothing is outside of that. Also why would the one that is the strongest when deepsleep starts no longer be the strongest at wake-up ? are you moving the device ? (or the routers ??)

Well, I am configuring the device indoors and then I move it to the deployment place and this is when I don't know what will happen. I cannot say for sure which is the strongest at that place.
So I figured I'd ask....
I have tested with an app on my phone and it is clear that when close to one AP the other is at very low strength...

Regarding caring, it is about the ESP connecting to a weak AP when there is a stronger one available...

Regarding caring, it is about the ESP connecting to a weak AP when there is a stronger one available...

Why do you care? Suppose that you have two pitchers. One can throw a fastball at 130MPH. The other can throw a fastball at 100MPH. You've got a batter coming to the plate that has never been able to hit a ball traveling more that 10 MPH. Does it matter who pitches to the batter?

If the ESP connects to an access point that is good enough to get data to and from a server, why does it matter that there is another access point that might, at the moment, be better?

What would be better would be for you to take your ESPxx to the mysterious location, and see what it actually does.

I know my device which was working fine in the lab only survived a short time in the deployment location.
I attribute this to the low signal strength and the way I had designed the firmware such that it would switch to AP mode if STA would not connect. In AP mode it waits for user interaction top modify its config.
That will never happen and after a while the batteries are depleted.

Is there a way to read the mac address off of the AP one finds in tghe air and select the one with the correct value?
My phone has a WiFi checking app that can display the SSID and Mac of all networks in reach...
Can the ESP check it too without connecting and then select the one to connect to?

+
https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/scan-class.html#bssid
+
https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/station-class.html#begin
(WiFi.begin(ssid, password, channel, bssid, connect))

Thanks Juraj!
I already added a small function to my sketch that makes it possible to read its signal strength while connected using RSSI.
I did not know of the BSSID item, I will test it when I get hold of the prototype again.

With the existing function I can see that my 4-channel logger in the attic operates on marginal levels, currently bssid = -87 (ouch!). I probably need to take it down and add an external antenna to the ESP-07.
The device in this thread, though, is battery operated and is right now 100 km away so I have to wait until I can fix that.
It is now running since 2 days once per hour, but I think it is OK signal-wise since there is only a floor between the main router and itself and the distance is approx 4m.
When this was located in a second house with the slave access point it still probably tried to use the main router and got too low signal level. And ran out of battery after a while when it reverted to AP mode for configuration...

-87 (ouch!).

the levels are if i do a scan in my living room, for my main WiFi -50 admittedly -87 is not to good, but that is still enough for a reliable connection, keep in mind that "direct line of sight" provides the strongest connection.

It has been better by about 5-6 dB before...
One problem is that the ESP-07 has a built-in ceramic antenna and it is on the top edge of the module between pins 1 and 16.
Depending on the orientation of the plastic box it is inside the antenna lobe may or may not find the router's antenna. Furthermore to make things more involved, also here I have two routers because my house is very long and thin (7x20 m). So the main router is in one end where the fiber connection enters the house and the other AP is 3-4 m from the other end of the house where I had Ethernet cable available.
Both use the same SSID and the extra router is configured as an AP for the main router so all traffic is sent to the main router.
So if the ESP device in the attic has connected to the AP furthest away I have a non-optimal connection...

Tested just now:
If I stand below the device in the attic and use WiFi Analyzer to read the signal strengths from the two routers there is a difference of about 5-6 dB and the levels are between 70-75 dB. In the attic there is more obstructing material so it would be weaker there...

I really have to get to work on a connection manager that can first check available networks, and then locate the one with the highest strength for the requested SSID. Then it should connect to this network through the mac address (how this is done I will have to find out).

Juraj:
Arduino/libraries/ESP8266WiFi/examples/WiFiScan/WiFiScan.ino at master · esp8266/Arduino · GitHub

I tried using the code from the GitHub example as follows:

case '9':   //Scan the WiFi networks
{
	SerialDebug.print("Starting WiFi network scan");
	//Put call here to scan and analyze the WiFi networks:
	// WiFi.scanNetworks will return the number of networks found
	int m = WiFi.scanNetworks();
	SerialDebug.println("Scan done");
	if (m == 0) 
	{
		SerialDebug.println("no networks found");
	} 
	else 
	{
		SerialDebug.print(n);
		SerialDebug.println(" networks found");
		for (i = 0; i < m; ++i) 
		{
		  // Print SSID and RSSI for each network found
		  SerialDebug.print(i + 1);
		  SerialDebug.print(": ");
		  SerialDebug.print(WiFi.SSID(i));// <== Gives argument error
		  SerialDebug.print(" (");
		  SerialDebug.print(WiFi.RSSI(i));// <== Gives argument error
		  SerialDebug.print(")");
		  SerialDebug.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
		  delay(10);
		}
	}
	strcpy(msg, "WiFi scan done");
	ReplyCmd(msg, strlen(msg));
	
}
break;  //subcmd 9

But I am getting "invalid arguments" on the SerialDebug.print lines shown above.
Something is amiss, I guess...

BosseB:
I tried using the code from the GitHub example as follows:

case '9':   //Scan the WiFi networks

{
SerialDebug.print("Starting WiFi network scan");
//Put call here to scan and analyze the WiFi networks:
// WiFi.scanNetworks will return the number of networks found
int m = WiFi.scanNetworks();
SerialDebug.println("Scan done");
if (m == 0)
{
SerialDebug.println("no networks found");
}
else
{
SerialDebug.print(n);
SerialDebug.println(" networks found");
for (i = 0; i < m; ++i)
{
  // Print SSID and RSSI for each network found
  SerialDebug.print(i + 1);
  SerialDebug.print(": ");
  SerialDebug.print(WiFi.SSID(i));// <== Gives argument error
  SerialDebug.print(" (");
  SerialDebug.print(WiFi.RSSI(i));// <== Gives argument error
  SerialDebug.print(")");
  SerialDebug.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
  delay(10);
}
}
strcpy(msg, "WiFi scan done");
ReplyCmd(msg, strlen(msg));

}
break;  //subcmd 9




But I am getting "invalid arguments" on the SerialDebug.print lines shown above.
Something is amiss, I guess...

and the example in IDE Examples menu?

Well, I am using Sloeber IDE since a year ago to manage projects with many source files. But I don't know where to find an Examples menu item...

So I opened my old Arduino IDE and managed to find WiFiScan.
Its code is like this:

  int n = WiFi.scanNetworks();
  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(")");
      Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*");
      delay(10);
    }
  }

Those print statements look the same to me...

EDIT:
Interestingly when I built the firmware it did not throw errors and I could upload the flash to my lab prototype and it runs!
Seems like the error messages in the IDE code window are Sloeber misinterpreting the code....

A WiFi scan takes 1.6 seconds and one result I have is this:

Rx: e9
Starting WiFi network scansl
scandone
usl
Scan done, T=1602
6 networks found
1: Karlbom (-85 dB) (mac= 54:A0:50:79:02:B8) (enc= *)
2: HP-Setup>09-M277 LaserJet (-54 dB) (mac= 42:49:0F:06:5F:09) (enc= open)
3: bossefiber (-46 dB) (mac= 2C:4D:54:4A:D3:E8) (enc= *)
4: bosseguest (-52 dB) (mac= 2C:4D:54:4A:D3:E9) (enc= *)
5: bossefiber (-82 dB) (mac= 88:D7:F6:BA:91:78) (enc= *)
6: VML 2.4GHz (-82 dB) (mac= AC:22:0B:87:A4:E0) (enc= *)
WiFi scan done[03]

#1 = our neighbor
#2 = my HP printer
#3&4 = my main router
#5 = my slave router 15 m away behind a number of walls
#6 = I have no idea

I think you'll be fine then ! Now it is just a matter of selection, if your slave router is still visible when far away.

One problem is that the ESP-07 has a built-in ceramic antenna and it is on the top edge of the module between pins 1 and 16.
Depending on the orientation of the plastic box it is inside the antenna lobe may or may not find the router's antenna.

I thought the ESP-07 has an external antenna connection point ? and a plastic box will hardly hamper your connection.

Deva_Rishi:
I think you'll be fine then ! Now it is just a matter of selection, if your slave router is still visible when far away.

Yes, I will have to create a WiFiConnect() function, which looks for multiple AP with the same ssid, then selects the strongest to connect to.

I thought the ESP-07 has an external antenna connection point ? and a plastic box will hardly hamper your connection.

Yes there is an external connector for an antenna. But there is also a ceramic antenna on board.
If you connect an external antenna thebn a 0 ohm resistor connecting the ceramic antenna must be removed.
I have done so on several other projects and it works good.
I have not said the plastic hampers the signal, but since the box can be placed on one of several sides it can be so oriented that there is no good signal from the AP. The ceramic antenna is parallel to two coordinates and at right angle to the last.

LATER
I have now written a WiFiConnectSta() function to connect to the strongest of the networks matching the ssid.
I am doing this using Sloeber IDE 4.2 and I am having some problems with its checking of the source code...
Here is my function (not yet tested on the real hardware):

bool ConnectWiFiSta(char *ssid, char *passwd)
{
	int i, j, m;
	bool ret = false;
	bool found = false;
	uint8_t mac[6];
	int32_t level, channel;

	SerialDebug.print("Starting WiFi scan to check availability at T="); SerialDebug.println(millis());
	m = WiFi.scanNetworks(); //Return network count.
	SerialDebug.print("Processing scan result at T="); SerialDebug.println(millis());
	if (m > 0) //Networks found
	{
		for (i = 0; i < m; i++) //Check if ssid compares
		{	
			if (strcmp(ssid, WiFi.SSID(i).c_str()) == 0) //We have a match!
			{
				if (!found)  //First match so grab whetever comes along
				{
					for (j = 0; j<6; j++) mac[j] = WiFi.BSSID(i)[j];
					level = WiFi.RSSI(i);
					channel = WiFi.channel(i);
					found = true; //Only once here...
				}
				else if (WiFi.RSSI(i) > level) //A new match is stronger
				{
					for (j = 0; j<6; j++) mac[j] = WiFi.BSSID(i)[j];
					level = WiFi.RSSI(i);
					channel = WiFi.channel(i);
				}
			}
		}
                if (found)
                {
		    //So we have checked the networks and have a good strength match
		    //Now connect:
	            WiFi.begin(ssid, passwd, channel, mac, true);
	            ret = true;
                }
	}
	return (ret);
}

It compiles OK, but Sloeber shows an error marker on each line dealing with the array of scan result, such as:
level = WiFi.RSSI(i);
All in all there are 8 such error bombs in the left side margin.
Apparently Sloeber cannot figure out that there is an overload where there is an index inside the call like (i).
What can I do to fix this?

   String ssid = WiFi.SSID(index); // @suppress("Invalid arguments")

Eclipse offers fixes in a pop-up on the error mark on left margin. most of the time they are not useful. (for Java it works much better)

I tried this:

    String ssid_s;

    m = WiFi.scanNetworks(); //Return network count.
    if (m > 0) //Networks found
    {
        for (i = 0; i < m; i++) //Check if ssid compares
        {
            ssid_s = WiFi.SSID(i); // <== Still marked with the bomb and invalid arguments
            ....

I thought that Sloeber is not able to correctly parse out the indexed version of the WiFi properties...
In that case maybe some update to an iclude file or similar would have helped, but which?

// @suppress("Invalid arguments")

Juraj:
// @suppress("Invalid arguments")

Isn't that a comment???
I treated it as such and did not include it, but if I paste it into one line the warning/error does not go away.

               SerialDebug.println(WiFi.BSSIDstr(i)); // @suppress("Invalid arguments")

                if (!found)  //First match so grab whatever comes along
                {
                    for (j = 0; j<6; j++) mac[j] = WiFi.BSSID(i)[j];
                    level = WiFi.RSSI(i);
                    channel = WiFi.channel(i); // @suppress("Invalid arguments")
                    found = true; //Only once here...
                }

Both lines above still show the warning, even after a build that succeeds.