Wifi Scan and Connect to Strongest Signal
Posted: Sun Nov 01, 2015 3:05 am
I'm still a noob so forgive me the hack type code.
I've been searching for code to Connect to the WiFi access point with the highest signal strength. I know the Esp8266 does not do this by default, and there are no plans to make the module do it. I have 4 access points around the property I work on, and I wanted my ESP to connect to the strongest one, but if that refuses connection to go to the next strongest on the list. If I do a WiFi.scanNetworks(), I cannot find a pattern in what is returned by listing the SSID vs RSSI. It seems random.
So I created a function that finds the strongest signal and connects to it, and on failure goes to the next strongest signal and attempts connection to that.
This code is based on know the password (pass) and that is entered as a char at the beginning.
I have a few questions as there are a number of redundancies.
1. Can I only scan when disconnected?
2. Is there a known timeout for WiFi.begin()?
3. I've used the WiFi.status() at least 3 times, is there a more efficient way of figureing out if a connection has occured?
4. I adapted a "insert sort" sorting algorithm I saw on the forums to work with a 2 Dimensional array. Is there a better way?
5. I was really just trying to learn how to write a function, so sorry that I didn't use more.
6. You can mostly ignore the loop() as it just calls the function and then waits
7. Also I left the Serial debugging - delete it if its annoying.
This code will likely be redundant if esspresif decide that the ESP will just connect to the strongest station.
I've been searching for code to Connect to the WiFi access point with the highest signal strength. I know the Esp8266 does not do this by default, and there are no plans to make the module do it. I have 4 access points around the property I work on, and I wanted my ESP to connect to the strongest one, but if that refuses connection to go to the next strongest on the list. If I do a WiFi.scanNetworks(), I cannot find a pattern in what is returned by listing the SSID vs RSSI. It seems random.
So I created a function that finds the strongest signal and connects to it, and on failure goes to the next strongest signal and attempts connection to that.
This code is based on know the password (pass) and that is entered as a char at the beginning.
I have a few questions as there are a number of redundancies.
1. Can I only scan when disconnected?
2. Is there a known timeout for WiFi.begin()?
3. I've used the WiFi.status() at least 3 times, is there a more efficient way of figureing out if a connection has occured?
4. I adapted a "insert sort" sorting algorithm I saw on the forums to work with a 2 Dimensional array. Is there a better way?
5. I was really just trying to learn how to write a function, so sorry that I didn't use more.
6. You can mostly ignore the loop() as it just calls the function and then waits
7. Also I left the Serial debugging - delete it if its annoying.
This code will likely be redundant if esspresif decide that the ESP will just connect to the strongest station.
Code: Select all
#include "ESP8266WiFi.h"
const char pass[] = "****"; //my password
void setup() {
Serial.begin(9600);
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
//WiFi.disconnect(); // is this completely neccesary?
delay(100);
Serial.println("Setup done");
}
void loop()
{
ScanAndConnect(); // Wait a bit before scanning again
delay(3000);
}
// Write re-entrant code
//Insert sort used as basis of algrothm.
bool ScanAndConnect(void)
{
int n = WiFi.scanNetworks();
if(n==0)
return false;
else
{
int i=0;
int SigStrength[10][10]; //maximum of 10 access points.
for ( i = 0; i<n; ++i)
{
//initialise array
//2 columms column 0 is the signal Strength
SigStrength[1][i] = i;
SigStrength[0][i] = WiFi.RSSI(i);
}
for (int i = 1; i < n; ++i)
{// insert sort into strongest signal
int j = SigStrength[0][i]; //holding value for signal strength
int l = SigStrength[1][i];
int k;
for (k = i - 1; (k >= 0) && (j > SigStrength[0][k]); k--)
{
SigStrength[0][k + 1] = SigStrength[0][k];
l = SigStrength[1][k+1];
SigStrength[1][k+1]=SigStrength[1][k];
SigStrength[1][k]=l; //swap index values here.
}
SigStrength[0][k + 1] = j;
SigStrength[1][k + 1] = l; //swap index values here to re-order.
}
int j = 0;
while ((j < n) && (WiFi.status() != WL_CONNECTED) )
{
Serial.print("Connecting: "); //debugging
Serial.println(WiFi.SSID(SigStrength[1][j])); //debugging
int k =0;
WiFi.begin(WiFi.SSID(SigStrength[1][j]), pass); //conect to the j'th value in our new array
while ((WiFi.status() != WL_CONNECTED) && k < 15 )
{
Serial.print("k");
delay(500);
k++; //counter for up to 8 seconds - why 8 ? who knows
}
Serial.println("");
if (k > 15)
Serial.println("NEXT Wifi Accest Point");
j++; //choose the next SSID
}
if (WiFi.status() == WL_CONNECTED)
{
Serial.print("Connected:");
Serial.println(WiFi.SSID());
return true;
}
else
{
Serial.println("No Connection :( (Sad face)");
}
}
}