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 #endif
#ifdef WIFI #ifdef WIFI
void connectToWiFi() { String getESP32ChipID() {
const int maxAttempts = 3; // Maximum number of attempts to connect to WiFi uint64_t chipId = ESP.getEfuseMac(); // Get the ESP32 chip ID
int attemptCount = 0; // Counter for the number of connection attempts return String(chipId, HEX);
}
while (attemptCount < maxAttempts) { void connectToWiFi(String ssid, String password) {
int strongestSignal = -100; // Variable to store the strongest signal strength String hostname = "MX-" + getESP32ChipID() + "-RDR";
String strongestSSID; // Variable to store the SSID with the strongest signal 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 #ifdef SERIAL_DEBUG
Serial.print("[*] Connected to network: "); Serial.println("Connecting to WiFi...");
Serial.println(strongestSSID); #endif
Serial.print("[+] Signal Strength: ");
Serial.println(strongestSignal); WiFi.begin(ssid.c_str(), password.c_str());
Serial.print("[+] IP Address: ");
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()); Serial.println(WiFi.localIP());
#endif #endif
} else {
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 #ifdef SERIAL_DEBUG
Serial.println("[!] No networks available or connection attempts failed. Rebooting..."); Serial.println("\nFailed to connect to WiFi. Rebooting...");
#endif #endif
delay(1000);
delay(1004); // Delay before attempting to reconnect
ESP.restart(); ESP.restart();
}
} }
void GetWifiInfo(){ void GetWifiInfo(){
@ -99,7 +82,7 @@ void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.println("Trying to Reconnect"); Serial.println("Trying to Reconnect");
#endif #endif
connectToWiFi(); connectToWiFi(ssid, password);
} }
void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info){ 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(WiFiStationConnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_CONNECTED);
WiFi.onEvent(WiFiGotIP, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP); WiFi.onEvent(WiFiGotIP, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
WiFi.onEvent(WiFiStationDisconnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED); WiFi.onEvent(WiFiStationDisconnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
connectToWiFi(); connectToWiFi(ssid, password);
#endif #endif
#ifdef RELAY1 #ifdef RELAY1