Skip to content

Wifi health check #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/gfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ static void gfx_loop(void *args) {
}

// Draw it
ESP_LOGI(TAG, "calling draw_webp");
// ESP_LOGI(TAG, "calling draw_webp");
if (draw_webp(webp, len, isAnimating)) {
ESP_LOGE(TAG, "Could not draw webp");
vTaskDelay(pdMS_TO_TICKS(1 * 1000));
Expand All @@ -174,14 +174,14 @@ static void gfx_loop(void *args) {

static int draw_webp(uint8_t *buf, size_t len, int32_t *isAnimating) {
// Set up WebP decoder
ESP_LOGI(TAG, "starting draw_webp");
// ESP_LOGI(TAG, "starting draw_webp");
int app_dwell_secs = *isAnimating;


int64_t dwell_us;

if (app_dwell_secs <= 0 ) {
ESP_LOGW(TAG,"isAnimating is already 0. Looping one more time while we wait.");
// ESP_LOGW(TAG,"isAnimating is already 0. Looping one more time while we wait.");
dwell_us = 1 * 1000000; // default to 1s if it's zero so we loop again or show the image for 1 more second.
} else {
// ESP_LOGI(TAG, "dwell_secs : %d", app_dwell_secs);
Expand Down Expand Up @@ -243,7 +243,7 @@ static int draw_webp(uint8_t *buf, size_t len, int32_t *isAnimating) {

// In case of a single frame, sleep for app_dwell_secs
if (animation.frame_count == 1) {
ESP_LOGI(TAG, "single frame delay for %d", app_dwell_secs);
// ESP_LOGI(TAG, "single frame delay for %d", app_dwell_secs);
// xTaskDelayUntil(&start_us, dwell_us);
vTaskDelay(pdMS_TO_TICKS(dwell_us / 1000)); // full dwell delay
// *isAnimating = 0;
Expand Down
5 changes: 5 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ void app_main(void) {
if (!wifi_is_connected()) {
ESP_LOGW(TAG,"Pausing main task until wifi connected . . . ");
while (!wifi_is_connected()) {
static int counter = 0;
counter++;
vTaskDelay(pdMS_TO_TICKS(1 * 1000));
if (counter > 600) esp_restart(); // after 10 minutes reboot because maybe we got stuck here after power outage or something.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The magic number 600 representing the WiFi connection timeout should be a named constant for better readability.

      if (counter > INITIAL_WIFI_CONNECT_TIMEOUT_SECONDS) esp_restart();

}
}

Expand Down Expand Up @@ -255,6 +258,7 @@ void app_main(void) {
ESP_LOGI(TAG, "Reconnected to WebSocket server.");
}
}
wifi_health_check();
// Do other stuff or vTaskDelay
vTaskDelay(pdMS_TO_TICKS(5000)); // check every 5s
}
Expand Down Expand Up @@ -290,6 +294,7 @@ void app_main(void) {
// for app_dwell_secs
vTaskDelay(pdMS_TO_TICKS(1000));
}
wifi_health_check();
}
}

Expand Down
47 changes: 46 additions & 1 deletion src/wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ static char s_image_url[MAX_URL_LEN + 1] = {0};
static int s_reconnect_attempts = 0;
static bool s_connection_given_up = false;

// Counter for tracking consecutive WiFi disconnections
static int s_wifi_disconnect_counter = 0;

// Callback functions
static void (*s_connect_callback)(void) = NULL;
static void (*s_disconnect_callback)(void) = NULL;
Expand Down Expand Up @@ -272,8 +275,9 @@ static bool has_saved_config = false;
ap_config.ap.max_connection = 4;
ap_config.ap.authmode = WIFI_AUTH_OPEN;
ap_config.ap.beacon_interval = 100; // Default beacon interval
// failure_retry_cnt ??

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Please clarify the purpose of the // failure_retry_cnt ?? comment, or remove it if it's no longer relevant.


ESP_LOGI(TAG, "Setting AP SSID: %s", DEFAULT_AP_SSID);
ESP_LOGI(TAG, "Setting AP SSID: %s", DEFAULT_AP_SSID);
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_config));

// Start WiFi
Expand Down Expand Up @@ -760,3 +764,44 @@ static void url_decode(char *str) {
}
*dst = '\0'; // Null-terminate the decoded string
}

/**
* @brief Check WiFi health and attempt reconnection if needed
*
* This function checks if WiFi is connected. If not, it attempts to reconnect
* and increments a counter. If the counter reaches 10 consecutive failures,
* the system will reboot. The counter is reset whenever WiFi is connected.
*/
void wifi_health_check(void) {
if (wifi_is_connected()) {
// Reset counter when WiFi is connected
if (s_wifi_disconnect_counter > 0) {
// ESP_LOGI(TAG, "WiFi reconnected successfully, resetting disconnect counter");
s_wifi_disconnect_counter = 0;
}
return;
}

// WiFi is not connected, increment counter
s_wifi_disconnect_counter++;
ESP_LOGW(TAG, "WiFi Health check. Disconnect count: %d/15", s_wifi_disconnect_counter);

// Try to reconnect
if (strlen(s_wifi_ssid) > 0) {
ESP_LOGI(TAG, "Attempting to reconnect to WiFi...");
esp_err_t err = esp_wifi_connect();
if (err != ESP_OK) {
ESP_LOGW(TAG, "WiFi reconnect attempt failed: %s", esp_err_to_name(err));
}
} else {
ESP_LOGW(TAG, "No SSID configured, cannot reconnect");
}

// If counter reaches threshold, reboot the system
if (s_wifi_disconnect_counter >= 15) {
ESP_LOGE(TAG, "WiFi disconnected for 15 consecutive checks. Rebooting system...");
// Wait a moment before rebooting
vTaskDelay(pdMS_TO_TICKS(1000));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The magic number 1000 for the reboot delay should be a named constant for readability.

        vTaskDelay(pdMS_TO_TICKS(WIFI_HEALTH_REBOOT_DELAY_MS));

esp_restart();
}
}
9 changes: 9 additions & 0 deletions src/wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,12 @@ void wifi_register_connect_callback(void (*callback)(void));
* @param callback Function to call when WiFi disconnects
*/
void wifi_register_disconnect_callback(void (*callback)(void));

/**
* @brief Check WiFi health and attempt reconnection if needed
*
* Checks if WiFi is connected. If not, attempts to reconnect and increments
* a counter. If the counter reaches 10 consecutive failures, the system will
* reboot. The counter is reset whenever WiFi is connected.
*/
void wifi_health_check(void);