wifi logic

This commit is contained in:
Matthew Frost 2023-11-20 18:24:15 +01:00
parent 4b4d6f5870
commit 1b7c03739f

View file

@ -14,79 +14,59 @@ unsigned int maxReaderWaitTime = 9000; // Variable to timeout reader after too
#ifdef WIFI #ifdef WIFI
void connectToWiFi() { void connectToWiFi() {
const int maxAttempts = 3; // Maximum number of attempts to connect to WiFi
int attemptCount = 0; // Counter for the number of connection attempts
while (attemptCount < maxAttempts) {
int strongestSignal = -100; // Variable to store the strongest signal strength
String strongestSSID; // Variable to store the SSID with the strongest signal
// Scan for available networks
int numNetworks = WiFi.scanNetworks(); 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 // Find the SSID with the strongest signal
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++) { for (int i = 0; i < numNetworks; i++) {
String currentSSID = WiFi.SSID(i); int signalStrength = WiFi.RSSI(i);
int currentSignal = WiFi.RSSI(i); String networkSSID = WiFi.SSID(i);
Serial.print(i + 1);
Serial.print(": "); if (signalStrength > strongestSignal) {
Serial.print(currentSSID); strongestSignal = signalStrength;
Serial.print(" ("); strongestSSID = networkSSID;
Serial.print(currentSignal);
Serial.println(" dBm)");
} }
} }
#endif // Connect to the provided SSID and password
if (strongestSignal > -100 && strongestSSID.equals(ssid)) {
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.begin(ssid, password); WiFi.begin(ssid, password);
#ifdef SERIAL_DEBUG // Wait for WiFi connection
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) { while (WiFi.status() != WL_CONNECTED) {
delay(500); delay(1000);
#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
}
} }
// Print network information
#ifdef SERIAL_DEBUG #ifdef SERIAL_DEBUG
Serial.println("WiFi connected"); Serial.print("[*] Connected to network: ");
#endif Serial.println(strongestSSID);
} else { Serial.print("[+] Signal Strength: ");
#ifdef SERIAL_DEBUG Serial.println(strongestSignal);
Serial.println("No access point with SSID " + String(ssid) + " found"); Serial.print("[+] IP Address: ");
Serial.println(WiFi.localIP());
#endif #endif
return; // Exit the function after successful connection
} }
attemptCount++; // Increment the attempt count
}
// Reboot if no networks are available or connection attempts have failed
#ifdef SERIAL_DEBUG
Serial.println("[!] No networks available or connection attempts failed. Rebooting...");
#endif
delay(1004); // Delay before attempting to reconnect
ESP.restart();
} }
void GetWifiInfo(){ void GetWifiInfo(){