Go Down

Topic: esp8266 connect when starting to which AP? (Read 594 times) previous topic - next topic

BosseB

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...

Deva_Rishi

Quote
But does it?
dunno, you actually have the opportunity to find out ! But if you want it to connect to the strongest one 
Quote
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 ??)
To 'Correct' you have to be Correct. (and not be condescending..)

PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

BosseB

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...

PaulS

#4
Apr 18, 2019, 01:35 pm Last Edit: Apr 18, 2019, 01:35 pm by PaulS
Quote
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.
The art of getting good answers lies in asking good questions.

BosseB

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?

Juraj

#6
Apr 19, 2019, 02:37 pm Last Edit: Apr 19, 2019, 02:40 pm by Juraj
https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiScan/WiFiScan.ino
+
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))
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

BosseB

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...

Deva_Rishi

Quote
-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.
To 'Correct' you have to be Correct. (and not be condescending..)

BosseB

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).


BosseB

https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiScan/WiFiScan.ino
I tried using the code from the GitHub example as follows:

Code: [Select]

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...

Juraj

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

Code: [Select]

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?
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

BosseB

#12
Apr 20, 2019, 08:09 am Last Edit: Apr 20, 2019, 10:20 am by BosseB
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:
Code: [Select]
  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:
Code: [Select]
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

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.
Quote
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.
To 'Correct' you have to be Correct. (and not be condescending..)

BosseB

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.
Quote
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.


Go Up