mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-05 10:49:42 +02:00
add wifi monitoring loop
This commit is contained in:
parent
fa9c9125db
commit
266528587a
3 changed files with 63 additions and 25 deletions
|
@ -148,6 +148,7 @@ 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();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Input
|
// Input
|
||||||
|
|
|
@ -42,7 +42,9 @@ 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;
|
||||||
const long wifi_connect_timeout = 5000; // Timeout for WiFi connect in milliseconds
|
unsigned 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
|
||||||
|
@ -221,6 +223,23 @@ void init_webserver() {
|
||||||
server.begin();
|
server.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
@ -236,21 +255,9 @@ void init_WiFi_AP() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_WiFi_STA(const char* ssid, const char* password) {
|
void init_WiFi_STA(const char* ssid, const char* password) {
|
||||||
// Connect to Wi-Fi network with SSID and password
|
// If we're already connected, there's nothing to do
|
||||||
Serial.print("Connecting to ");
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
Serial.println(ssid);
|
if (wifi_state != "Connected") {
|
||||||
WiFi.begin(ssid, password);
|
|
||||||
|
|
||||||
wifi_connect_start_time = millis();
|
|
||||||
wifi_connect_current_time = wifi_connect_start_time;
|
|
||||||
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_connected = true;
|
||||||
wifi_state = "Connected";
|
wifi_state = "Connected";
|
||||||
// Print local IP address and start web server
|
// Print local IP address and start web server
|
||||||
|
@ -259,13 +266,34 @@ void init_WiFi_STA(const char* ssid, const char* password) {
|
||||||
Serial.println(ssid);
|
Serial.println(ssid);
|
||||||
Serial.print("IP address: ");
|
Serial.print("IP address: ");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
} else {
|
}
|
||||||
wifi_connected = false;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we're not currently trying to connect, start the connection process
|
||||||
|
if (wifi_state != "Connecting") {
|
||||||
|
Serial.print("Connecting to: ");
|
||||||
|
Serial.println(ssid);
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
wifi_state = "Connecting";
|
||||||
|
wifi_connect_start_time = millis();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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("Not connected to WiFi network: ");
|
Serial.print("Failed to connect 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() {
|
||||||
|
|
|
@ -71,6 +71,15 @@ 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.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue