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
|
#ifdef WIFI
|
||||||
void connectToWiFi() {
|
void connectToWiFi() {
|
||||||
int numNetworks = WiFi.scanNetworks();
|
const int maxAttempts = 3; // Maximum number of attempts to connect to WiFi
|
||||||
int strongestSignal = -100; // Initialize with a low signal strength
|
int attemptCount = 0; // Counter for the number of connection attempts
|
||||||
int strongestIndex = -1; // Index of the strongest access point
|
|
||||||
|
|
||||||
#ifdef SERIAL_DEBUG
|
while (attemptCount < maxAttempts) {
|
||||||
Serial.println("Scan complete");
|
int strongestSignal = -100; // Variable to store the strongest signal strength
|
||||||
Serial.print(numNetworks);
|
String strongestSSID; // Variable to store the SSID with the strongest signal
|
||||||
Serial.println(" networks found");
|
|
||||||
|
|
||||||
if (numNetworks > 0) {
|
// Scan for available networks
|
||||||
Serial.println("Strongest networks:");
|
int numNetworks = WiFi.scanNetworks();
|
||||||
for (int i = 0; i < numNetworks; i++) {
|
|
||||||
String currentSSID = WiFi.SSID(i);
|
// Find the SSID with the strongest signal
|
||||||
int currentSignal = WiFi.RSSI(i);
|
for (int i = 0; i < numNetworks; i++) {
|
||||||
Serial.print(i + 1);
|
int signalStrength = WiFi.RSSI(i);
|
||||||
Serial.print(": ");
|
String networkSSID = WiFi.SSID(i);
|
||||||
Serial.print(currentSSID);
|
|
||||||
Serial.print(" (");
|
if (signalStrength > strongestSignal) {
|
||||||
Serial.print(currentSignal);
|
strongestSignal = signalStrength;
|
||||||
Serial.println(" dBm)");
|
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
|
#endif
|
||||||
|
|
||||||
for (int i = 0; i < numNetworks; i++) {
|
delay(1004); // Delay before attempting to reconnect
|
||||||
String currentSSID = WiFi.SSID(i);
|
ESP.restart();
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GetWifiInfo(){
|
void GetWifiInfo(){
|
||||||
|
|
Loading…
Reference in a new issue