wifi based on closest AP
This commit is contained in:
parent
fc287d668c
commit
4b4d6f5870
1 changed files with 66 additions and 22 deletions
88
src/main.cpp
88
src/main.cpp
|
@ -13,36 +13,80 @@ unsigned int maxReaderWaitTime = 9000; // Variable to timeout reader after too
|
|||
#endif
|
||||
|
||||
#ifdef WIFI
|
||||
|
||||
void connectToWiFi() {
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ssid, password);
|
||||
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.print("Connecting to WiFi ..");
|
||||
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
|
||||
|
||||
unsigned long startTime = millis(); // Record the start time of connection
|
||||
for (int i = 0; i < numNetworks; i++) {
|
||||
String currentSSID = WiFi.SSID(i);
|
||||
int currentSignal = WiFi.RSSI(i);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
|
||||
#ifdef SERIAL_DEBUG
|
||||
Serial.print(".");
|
||||
#endif
|
||||
|
||||
// Check if it's time to reboot
|
||||
if (millis() - startTime >= wifiRebootTimeout) { // Reboot after 20 seconds
|
||||
#ifdef SERIAL_DEBUG
|
||||
Serial.println("\nFailed to connect. Rebooting...");
|
||||
#endif
|
||||
ESP.restart(); // Reboot the Arduino board
|
||||
if (currentSSID.equals(ssid) && currentSignal > strongestSignal) {
|
||||
strongestSignal = currentSignal;
|
||||
strongestIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef SERIAL_DEBUG
|
||||
Serial.println("WiFi connected");
|
||||
#endif
|
||||
|
||||
if (strongestIndex != -1) {
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
#ifdef SERIAL_DEBUG
|
||||
Serial.print("Connecting to WiFi ");
|
||||
Serial.print(ssid);
|
||||
Serial.print(" (Signal Strength: ");
|
||||
Serial.print(strongestSignal);
|
||||
Serial.println(" dBm)...");
|
||||
#endif
|
||||
|
||||
unsigned long startTime = millis(); // Record the start time of connection
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
|
||||
#ifdef SERIAL_DEBUG
|
||||
Serial.print(".");
|
||||
#endif
|
||||
|
||||
// Check if it's time to reboot
|
||||
if (millis() - startTime >= wifiRebootTimeout) { // Reboot after 20 seconds
|
||||
#ifdef SERIAL_DEBUG
|
||||
Serial.println("\nFailed to connect. Rebooting...");
|
||||
#endif
|
||||
ESP.restart(); // Reboot the Arduino board
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef SERIAL_DEBUG
|
||||
Serial.println("WiFi connected");
|
||||
#endif
|
||||
} else {
|
||||
#ifdef SERIAL_DEBUG
|
||||
Serial.println("No access point with SSID " + String(ssid) + " found");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void GetWifiInfo(){
|
||||
|
|
Loading…
Reference in a new issue