Arduino Forum

Using Arduino => Programming Questions => Topic started by: BosseB on Apr 18, 2019, 10:07 am

Title: esp8266 connect when starting to which AP?
Post by: BosseB on Apr 18, 2019, 10:07 am
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...
Title: Re: esp8266 connect when starting to which AP?
Post by: Deva_Rishi on Apr 18, 2019, 11:40 am
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 ??)
Title: Re: esp8266 connect when starting to which AP?
Post by: PaulS on Apr 18, 2019, 01:09 pm
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.
Title: Re: esp8266 connect when starting to which AP?
Post by: BosseB on Apr 18, 2019, 01:12 pm
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...
Title: Re: esp8266 connect when starting to which AP?
Post by: PaulS on Apr 18, 2019, 01:35 pm
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.
Title: Re: esp8266 connect when starting to which AP?
Post by: BosseB on Apr 19, 2019, 02:18 pm
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?
Title: Re: esp8266 connect when starting to which AP?
Post by: Juraj on Apr 19, 2019, 02:37 pm
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))
Title: Re: esp8266 connect when starting to which AP?
Post by: BosseB on Apr 19, 2019, 05:30 pm
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...
Title: Re: esp8266 connect when starting to which AP?
Post by: Deva_Rishi on Apr 19, 2019, 06:24 pm
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.
Title: Re: esp8266 connect when starting to which AP?
Post by: BosseB on Apr 19, 2019, 07:06 pm
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).

Title: Re: esp8266 connect when starting to which AP?
Post by: BosseB on Apr 20, 2019, 12:04 am
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...
Title: Re: esp8266 connect when starting to which AP?
Post by: Juraj on Apr 20, 2019, 07:36 am
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?
Title: Re: esp8266 connect when starting to which AP?
Post by: BosseB on Apr 20, 2019, 08:09 am
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
Title: Re: esp8266 connect when starting to which AP?
Post by: Deva_Rishi on Apr 20, 2019, 02:02 pm
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.
Title: Re: esp8266 connect when starting to which AP?
Post by: BosseB on Apr 20, 2019, 04:28 pm
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.

Title: Re: esp8266 connect when starting to which AP?
Post by: BosseB on Apr 21, 2019, 12:52 am
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):

Code: [Select]
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?
Title: Re: esp8266 connect when starting to which AP?
Post by: Juraj on Apr 21, 2019, 07:55 am
 
Code: [Select]
   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)
Title: Re: esp8266 connect when starting to which AP?
Post by: BosseB on Apr 21, 2019, 10:12 am
I tried this:

Code: [Select]
    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?
Title: Re: esp8266 connect when starting to which AP?
Post by: Juraj on Apr 21, 2019, 10:40 am
// @suppress("Invalid arguments")
Title: Re: esp8266 connect when starting to which AP?
Post by: BosseB on Apr 21, 2019, 01:41 pm
// @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.
 
Code: [Select]
               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.
Title: Re: esp8266 connect when starting to which AP?
Post by: Juraj on Apr 21, 2019, 02:11 pm
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.
 
Code: [Select]
               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.
open pop-up on the the mark on left border.

(http://forum.arduino.cc/index.php?action=dlattach;topic=610805.0;attach=304858)
Title: Re: esp8266 connect when starting to which AP?
Post by: BosseB on Apr 21, 2019, 03:00 pm
When you say "open pop-up" do you mean:
- rightclick red mark on  the border
- Select "suppress" in the menu shown

That does not work for me, the pop-up menu that is shown when right-clicking is completely different and is the same independent of where in the left border I click. It has 13 items to select from starting with "Toggle Breakpoint".
Seems not related to the problem we are discussing.

I am on Sloeber 4.2 and I know there is a newer version available, could this be something new in a newer Sloeber?
Unfortunately I did not dare upgrade because the description on how to do it was so incredibly difficult to follow and I stayed with the "working" version...
Title: Re: esp8266 connect when starting to which AP?
Post by: Juraj on Apr 21, 2019, 04:37 pm
sorry, normal click, not second mouse button
Title: Re: esp8266 connect when starting to which AP?
Post by: BosseB on Apr 22, 2019, 08:38 am
I cannot get that menu pop-up to appear whatever I do.
Is it part of a newer Sloeber version?
I am on 4.2

If so is there any upgrade path or do I have to install a side by side higher version and configure it to work like the one I have?
Title: Re: esp8266 connect when starting to which AP?
Post by: Juraj on Apr 22, 2019, 12:10 pm
I cannot get that menu pop-up to appear whatever I do.
Is it part of a newer Sloeber version?
I am on 4.2

If so is there any upgrade path or do I have to install a side by side higher version and configure it to work like the one I have?

you can simply copy the comment

it is Eclipse C++ support.

(http://forum.arduino.cc/index.php?action=dlattach;topic=610805.0;attach=304972)
Title: Re: esp8266 connect when starting to which AP?
Post by: BosseB on Apr 22, 2019, 02:16 pm
I don't see this at all. When I hover the mouse over the red mark a small rectangle appears with a yellow text box below.
Please see the attached image:
(http://forum.arduino.cc/index.php?action=dlattach;topic=610805.0;attach=305026)
Title: Re: esp8266 connect when starting to which AP?
Post by: Juraj on Apr 22, 2019, 02:36 pm
I don't see this at all. When I hover the mouse over the red mark a small rectangle appears with a yellow text box below.
Please see the attached image:
(http://forum.arduino.cc/index.php?action=dlattach;topic=610805.0;attach=305026)
and if you click on it?
Title: Re: esp8266 connect when starting to which AP?
Post by: BosseB on Apr 22, 2019, 04:12 pm
If I click on the red blob the message disappears momentarily and then returns.
If I click on the little green point to the right, it adds a breakpoint on that line.

Nothing else seems to happen and if I again click the green point the breakpoint disappears.


Must be some mis-configuration inside Sloeber 4.2...
Title: Re: esp8266 connect when starting to which AP?
Post by: Juraj on Apr 22, 2019, 04:42 pm
I have a year old Eclipse Oxygen instalation with Sloeber as add-on
Title: Re: esp8266 connect when starting to which AP?
Post by: BosseB on Apr 23, 2019, 11:46 am
OK, I'll give up now. After all the code builds OK.
The issue is about Eclipse/Sloeber and probably requires a different topic.
I wonder if Jantje reads this forum?
Title: Re: esp8266 connect when starting to which AP?
Post by: Juraj on Apr 23, 2019, 12:55 pm
OK, I'll give up now. After all the code builds OK.
The issue is about Eclipse/Sloeber and probably requires a different topic.
I wonder if Jantje reads this forum?
Sloeber only uses existing Eclipse C++ development support. This problem is there