Skip to content

Commit

Permalink
Merge pull request #153 from kharnt0x/wifi-monitor
Browse files Browse the repository at this point in the history
add wifi monitoring loop
  • Loading branch information
dalathegreat authored Feb 2, 2024
2 parents 38524e2 + 2665285 commit 26cbc47
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 25 deletions.
1 change: 1 addition & 0 deletions Software/Software.ino
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ void loop() {
#ifdef WEBSERVER
// Over-the-air updates by ElegantOTA
ElegantOTA.loop();
WiFi_monitor_loop();
#endif

// Input
Expand Down
78 changes: 53 additions & 25 deletions Software/src/devboard/webserver/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ bool wifi_connected;
// Wifi connect time declarations and definition
unsigned long wifi_connect_start_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() {
// Configure WiFi
Expand Down Expand Up @@ -221,6 +223,23 @@ void init_webserver() {
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() {
Serial.print("Creating Access Point: ");
Serial.println(ssidAP);
Expand All @@ -236,36 +255,45 @@ void init_WiFi_AP() {
}

void init_WiFi_STA(const char* ssid, const char* password) {
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
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 we're already connected, there's nothing to do
if (WiFi.status() == WL_CONNECTED) {
if (wifi_state != "Connected") {
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.print("IP address: ");
Serial.println(WiFi.localIP());
}
return;
}
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: ");

// If we're not currently trying to connect, start the connection process
if (wifi_state != "Connecting") {
Serial.print("Connecting to: ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
} else {
wifi_connected = false;
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";
Serial.print("Not connected to WiFi network: ");
Serial.print("Failed to connect to WiFi network: ");
Serial.println(ssid);
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() {
Expand Down
9 changes: 9 additions & 0 deletions Software/src/devboard/webserver/webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ void init_WiFi_AP();
*/
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.
*
Expand Down

0 comments on commit 26cbc47

Please sign in to comment.