Simple WIFI

This commit is contained in:
Matthew Frost 2023-11-21 21:36:49 +01:00
parent afc1a519a5
commit e0f090fdf6

View file

@ -13,60 +13,43 @@ unsigned int maxReaderWaitTime = 9000; // Variable to timeout reader after too
#endif
#ifdef WIFI
void connectToWiFi() {
const int maxAttempts = 3; // Maximum number of attempts to connect to WiFi
int attemptCount = 0; // Counter for the number of connection attempts
String getESP32ChipID() {
uint64_t chipId = ESP.getEfuseMac(); // Get the ESP32 chip ID
return String(chipId, HEX);
}
while (attemptCount < maxAttempts) {
int strongestSignal = -100; // Variable to store the strongest signal strength
String strongestSSID; // Variable to store the SSID with the strongest signal
void connectToWiFi(String ssid, String password) {
String hostname = "MX-" + getESP32ChipID() + "-RDR";
WiFi.hostname(hostname.c_str());
// 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...");
Serial.println("Connecting to WiFi...");
#endif
delay(1004); // Delay before attempting to reconnect
ESP.restart();
WiFi.begin(ssid.c_str(), password.c_str());
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 30) {
delay(1000);
#ifdef SERIAL_DEBUG
Serial.print(".");
#endif
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
#ifdef SERIAL_DEBUG
Serial.println("\nConnected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
#endif
} else {
#ifdef SERIAL_DEBUG
Serial.println("\nFailed to connect to WiFi. Rebooting...");
#endif
delay(1000);
ESP.restart();
}
}
void GetWifiInfo(){
@ -99,7 +82,7 @@ void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.println("Trying to Reconnect");
#endif
connectToWiFi();
connectToWiFi(ssid, password);
}
void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info){
@ -202,7 +185,7 @@ void setup() {
WiFi.onEvent(WiFiStationConnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_CONNECTED);
WiFi.onEvent(WiFiGotIP, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
WiFi.onEvent(WiFiStationDisconnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
connectToWiFi();
connectToWiFi(ssid, password);
#endif
#ifdef RELAY1