Revert "Merge pull request #153 from kharnt0x/wifi-monitor"

This reverts commit 26cbc47c15, reversing
changes made to 38524e27d1.
This commit is contained in:
Brett Christensen 2024-02-07 06:35:55 +11:00
parent 6bc253ff6e
commit 89a8a947af
3 changed files with 25 additions and 63 deletions

View file

@ -157,7 +157,6 @@ void loop() {
#ifdef WEBSERVER #ifdef WEBSERVER
// Over-the-air updates by ElegantOTA // Over-the-air updates by ElegantOTA
ElegantOTA.loop(); ElegantOTA.loop();
WiFi_monitor_loop();
#ifdef MQTT #ifdef MQTT
mqtt_loop(); mqtt_loop();
#endif #endif

View file

@ -42,9 +42,7 @@ bool wifi_connected;
// Wifi connect time declarations and definition // Wifi connect time declarations and definition
unsigned long wifi_connect_start_time; unsigned long wifi_connect_start_time;
unsigned long wifi_connect_current_time; unsigned long wifi_connect_current_time;
unsigned long wifi_connect_timeout = 5000; // Timeout for WiFi connect in milliseconds const long wifi_connect_timeout = 5000; // Timeout for WiFi connect in milliseconds
unsigned long wifi_monitor_loop_time = 30000; // Will check if WiFi is connected and try reconnect every x milliseconds
unsigned long last_wifi_monitor_run = 0;
void init_webserver() { void init_webserver() {
// Configure WiFi // Configure WiFi
@ -249,23 +247,6 @@ void init_webserver() {
#endif #endif
} }
void WiFi_monitor_loop() {
unsigned long currentMillis = millis();
if (currentMillis - last_wifi_monitor_run > wifi_monitor_loop_time) {
last_wifi_monitor_run = currentMillis;
if (WiFi.status() != WL_CONNECTED && wifi_state != "Connecting") {
wifi_connected = false;
wifi_state = "Not connected";
Serial.print("Wifi disconnected. Attempting reconnection");
init_WiFi_STA(ssid, password);
} else if (WiFi.status() == WL_CONNECTED && wifi_state != "Connected") {
wifi_connected = true;
wifi_state = "Connected";
Serial.println("Wifi reconnected");
}
}
}
void init_WiFi_AP() { void init_WiFi_AP() {
Serial.print("Creating Access Point: "); Serial.print("Creating Access Point: ");
Serial.println(ssidAP); Serial.println(ssidAP);
@ -281,45 +262,36 @@ void init_WiFi_AP() {
} }
void init_WiFi_STA(const char* ssid, const char* password) { void init_WiFi_STA(const char* ssid, const char* password) {
// If we're already connected, there's nothing to do // Connect to Wi-Fi network with SSID and password
if (WiFi.status() == WL_CONNECTED) { Serial.print("Connecting to ");
if (wifi_state != "Connected") { Serial.println(ssid);
wifi_connected = true; WiFi.begin(ssid, password);
wifi_state = "Connected";
// Print local IP address and start web server
Serial.println("");
Serial.print("Connected to WiFi network: ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
return;
}
// If we're not currently trying to connect, start the connection process wifi_connect_start_time = millis();
if (wifi_state != "Connecting") { wifi_connect_current_time = wifi_connect_start_time;
Serial.print("Connecting to: "); while ((wifi_connect_current_time - wifi_connect_start_time) <= wifi_connect_timeout &&
WiFi.status() != WL_CONNECTED) { // do this loop for up to 5000ms
// to break the loop when the connection is not established (wrong ssid or password).
delay(500);
Serial.print(".");
wifi_connect_current_time = millis();
}
if (WiFi.status() == WL_CONNECTED) { // WL_CONNECTED is assigned when connected to a WiFi network
wifi_connected = true;
wifi_state = "Connected";
// Print local IP address and start web server
Serial.println("");
Serial.print("Connected to WiFi network: ");
Serial.println(ssid); Serial.println(ssid);
WiFi.begin(ssid, password); Serial.print("IP address: ");
wifi_state = "Connecting"; Serial.println(WiFi.localIP());
wifi_connect_start_time = millis(); } else {
return; wifi_connected = false;
}
// If we've been trying to connect for more than 5000ms, give up
if (millis() - wifi_connect_start_time > wifi_connect_timeout) {
wifi_state = "Not connected"; wifi_state = "Not connected";
Serial.print("Failed to connect to WiFi network: "); Serial.print("Not connected to WiFi network: ");
Serial.println(ssid); Serial.println(ssid);
Serial.println("Please check WiFi network name and password, and if WiFi network is available."); Serial.println("Please check WiFi network name and password, and if WiFi network is available.");
Serial.print("Will try again in ");
Serial.print((wifi_monitor_loop_time - (millis() - last_wifi_monitor_run)) / 1000);
Serial.println(" seconds.");
return;
} }
// Otherwise, just print a dot to indicate that we're still trying to connect
Serial.print(".");
} }
void init_ElegantOTA() { void init_ElegantOTA() {

View file

@ -81,15 +81,6 @@ void init_WiFi_AP();
*/ */
void init_WiFi_STA(const char* ssid, const char* password); void init_WiFi_STA(const char* ssid, const char* password);
/**
* @brief Monitoring loop for WiFi. Will attempt to reconnect to access point if the connection goes down.
*
* @param[in] void
*
* @return void
*/
void WiFi_monitor_loop();
/** /**
* @brief Initialization function for ElegantOTA. * @brief Initialization function for ElegantOTA.
* *