wifi based on closest AP
This commit is contained in:
parent
fc287d668c
commit
4b4d6f5870
1 changed files with 66 additions and 22 deletions
48
src/main.cpp
48
src/main.cpp
|
@ -13,13 +13,52 @@ unsigned int maxReaderWaitTime = 9000; // Variable to timeout reader after too
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef WIFI
|
#ifdef WIFI
|
||||||
|
|
||||||
void connectToWiFi() {
|
void connectToWiFi() {
|
||||||
|
int numNetworks = WiFi.scanNetworks();
|
||||||
|
int strongestSignal = -100; // Initialize with a low signal strength
|
||||||
|
int strongestIndex = -1; // Index of the strongest access point
|
||||||
|
|
||||||
|
#ifdef SERIAL_DEBUG
|
||||||
|
Serial.println("Scan complete");
|
||||||
|
Serial.print(numNetworks);
|
||||||
|
Serial.println(" networks found");
|
||||||
|
|
||||||
|
if (numNetworks > 0) {
|
||||||
|
Serial.println("Strongest networks:");
|
||||||
|
for (int i = 0; i < numNetworks; i++) {
|
||||||
|
String currentSSID = WiFi.SSID(i);
|
||||||
|
int currentSignal = WiFi.RSSI(i);
|
||||||
|
Serial.print(i + 1);
|
||||||
|
Serial.print(": ");
|
||||||
|
Serial.print(currentSSID);
|
||||||
|
Serial.print(" (");
|
||||||
|
Serial.print(currentSignal);
|
||||||
|
Serial.println(" dBm)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
for (int i = 0; i < numNetworks; i++) {
|
||||||
|
String currentSSID = WiFi.SSID(i);
|
||||||
|
int currentSignal = WiFi.RSSI(i);
|
||||||
|
|
||||||
|
if (currentSSID.equals(ssid) && currentSignal > strongestSignal) {
|
||||||
|
strongestSignal = currentSignal;
|
||||||
|
strongestIndex = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strongestIndex != -1) {
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
WiFi.begin(ssid, password);
|
WiFi.begin(ssid, password);
|
||||||
|
|
||||||
#ifdef SERIAL_DEBUG
|
#ifdef SERIAL_DEBUG
|
||||||
Serial.print("Connecting to WiFi ..");
|
Serial.print("Connecting to WiFi ");
|
||||||
|
Serial.print(ssid);
|
||||||
|
Serial.print(" (Signal Strength: ");
|
||||||
|
Serial.print(strongestSignal);
|
||||||
|
Serial.println(" dBm)...");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
unsigned long startTime = millis(); // Record the start time of connection
|
unsigned long startTime = millis(); // Record the start time of connection
|
||||||
|
@ -43,6 +82,11 @@ void connectToWiFi() {
|
||||||
#ifdef SERIAL_DEBUG
|
#ifdef SERIAL_DEBUG
|
||||||
Serial.println("WiFi connected");
|
Serial.println("WiFi connected");
|
||||||
#endif
|
#endif
|
||||||
|
} else {
|
||||||
|
#ifdef SERIAL_DEBUG
|
||||||
|
Serial.println("No access point with SSID " + String(ssid) + " found");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GetWifiInfo(){
|
void GetWifiInfo(){
|
||||||
|
|
Loading…
Reference in a new issue