diff --git a/src/gfx.c b/src/gfx.c index 2ca7bc5..dc803d7 100644 --- a/src/gfx.c +++ b/src/gfx.c @@ -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)); @@ -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); @@ -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; diff --git a/src/main.c b/src/main.c index b8d83bc..bf816d9 100644 --- a/src/main.c +++ b/src/main.c @@ -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. } } @@ -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 } @@ -290,6 +294,7 @@ void app_main(void) { // for app_dwell_secs vTaskDelay(pdMS_TO_TICKS(1000)); } + wifi_health_check(); } } diff --git a/src/wifi.c b/src/wifi.c index 7bcbd6a..0d71348 100644 --- a/src/wifi.c +++ b/src/wifi.c @@ -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; @@ -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 ?? - 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 @@ -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)); + esp_restart(); + } +} diff --git a/src/wifi.h b/src/wifi.h index 94275d2..0daa93b 100644 --- a/src/wifi.h +++ b/src/wifi.h @@ -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);