wifi logic
This commit is contained in:
parent
4b4d6f5870
commit
1b7c03739f
1 changed files with 46 additions and 66 deletions
112
src/main.cpp
112
src/main.cpp
|
@ -14,79 +14,59 @@ unsigned int maxReaderWaitTime = 9000; // Variable to timeout reader after too
|
|||
|
||||
#ifdef WIFI
|
||||
void connectToWiFi() {
|
||||
int numNetworks = WiFi.scanNetworks();
|
||||
int strongestSignal = -100; // Initialize with a low signal strength
|
||||
int strongestIndex = -1; // Index of the strongest access point
|
||||
const int maxAttempts = 3; // Maximum number of attempts to connect to WiFi
|
||||
int attemptCount = 0; // Counter for the number of connection attempts
|
||||
|
||||
#ifdef SERIAL_DEBUG
|
||||
Serial.println("Scan complete");
|
||||
Serial.print(numNetworks);
|
||||
Serial.println(" networks found");
|
||||
while (attemptCount < maxAttempts) {
|
||||
int strongestSignal = -100; // Variable to store the strongest signal strength
|
||||
String strongestSSID; // Variable to store the SSID with the strongest signal
|
||||
|
||||
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)");
|
||||
// Scan for available networks
|
||||
int numNetworks = WiFi.scanNetworks();
|
||||
|
||||
// Find the SSID with the strongest signal
|
||||
for (int i = 0; i < numNetworks; i++) {
|
||||
int signalStrength = WiFi.RSSI(i);
|
||||
String networkSSID = WiFi.SSID(i);
|
||||
|
||||
if (signalStrength > strongestSignal) {
|
||||
strongestSignal = signalStrength;
|
||||
strongestSSID = networkSSID;
|
||||
}
|
||||
}
|
||||
|
||||
// Connect to the provided SSID and password
|
||||
if (strongestSignal > -100 && strongestSSID.equals(ssid)) {
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
// Wait for WiFi connection
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
// Print network information
|
||||
#ifdef SERIAL_DEBUG
|
||||
Serial.print("[*] Connected to network: ");
|
||||
Serial.println(strongestSSID);
|
||||
Serial.print("[+] Signal Strength: ");
|
||||
Serial.println(strongestSignal);
|
||||
Serial.print("[+] IP Address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
#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
|
||||
|
||||
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);
|
||||
|
||||
#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
|
||||
}
|
||||
delay(1004); // Delay before attempting to reconnect
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
void GetWifiInfo(){
|
||||
|
|
Loading…
Reference in a new issue