Skip to content

Commit b0d7ff9

Browse files
authored
Merge pull request #47 from tronbyt/wifi_health_check
Wifi health check
2 parents 4cce7df + 6b4cf3d commit b0d7ff9

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

src/gfx.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static void gfx_loop(void *args) {
162162
}
163163

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

175175
static int draw_webp(uint8_t *buf, size_t len, int32_t *isAnimating) {
176176
// Set up WebP decoder
177-
ESP_LOGI(TAG, "starting draw_webp");
177+
// ESP_LOGI(TAG, "starting draw_webp");
178178
int app_dwell_secs = *isAnimating;
179179

180180

181181
int64_t dwell_us;
182182

183183
if (app_dwell_secs <= 0 ) {
184-
ESP_LOGW(TAG,"isAnimating is already 0. Looping one more time while we wait.");
184+
// ESP_LOGW(TAG,"isAnimating is already 0. Looping one more time while we wait.");
185185
dwell_us = 1 * 1000000; // default to 1s if it's zero so we loop again or show the image for 1 more second.
186186
} else {
187187
// 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) {
243243

244244
// In case of a single frame, sleep for app_dwell_secs
245245
if (animation.frame_count == 1) {
246-
ESP_LOGI(TAG, "single frame delay for %d", app_dwell_secs);
246+
// ESP_LOGI(TAG, "single frame delay for %d", app_dwell_secs);
247247
// xTaskDelayUntil(&start_us, dwell_us);
248248
vTaskDelay(pdMS_TO_TICKS(dwell_us / 1000)); // full dwell delay
249249
// *isAnimating = 0;

src/main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,10 @@ void app_main(void) {
198198
if (!wifi_is_connected()) {
199199
ESP_LOGW(TAG,"Pausing main task until wifi connected . . . ");
200200
while (!wifi_is_connected()) {
201+
static int counter = 0;
202+
counter++;
201203
vTaskDelay(pdMS_TO_TICKS(1 * 1000));
204+
if (counter > 600) esp_restart(); // after 10 minutes reboot because maybe we got stuck here after power outage or something.
202205
}
203206
}
204207

@@ -255,6 +258,7 @@ void app_main(void) {
255258
ESP_LOGI(TAG, "Reconnected to WebSocket server.");
256259
}
257260
}
261+
wifi_health_check();
258262
// Do other stuff or vTaskDelay
259263
vTaskDelay(pdMS_TO_TICKS(5000)); // check every 5s
260264
}
@@ -290,6 +294,7 @@ void app_main(void) {
290294
// for app_dwell_secs
291295
vTaskDelay(pdMS_TO_TICKS(1000));
292296
}
297+
wifi_health_check();
293298
}
294299
}
295300

src/wifi.c

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ static char s_image_url[MAX_URL_LEN + 1] = {0};
5757
static int s_reconnect_attempts = 0;
5858
static bool s_connection_given_up = false;
5959

60+
// Counter for tracking consecutive WiFi disconnections
61+
static int s_wifi_disconnect_counter = 0;
62+
6063
// Callback functions
6164
static void (*s_connect_callback)(void) = NULL;
6265
static void (*s_disconnect_callback)(void) = NULL;
@@ -272,8 +275,9 @@ static bool has_saved_config = false;
272275
ap_config.ap.max_connection = 4;
273276
ap_config.ap.authmode = WIFI_AUTH_OPEN;
274277
ap_config.ap.beacon_interval = 100; // Default beacon interval
278+
// failure_retry_cnt ??
275279

276-
ESP_LOGI(TAG, "Setting AP SSID: %s", DEFAULT_AP_SSID);
280+
ESP_LOGI(TAG, "Setting AP SSID: %s", DEFAULT_AP_SSID);
277281
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_config));
278282

279283
// Start WiFi
@@ -760,3 +764,44 @@ static void url_decode(char *str) {
760764
}
761765
*dst = '\0'; // Null-terminate the decoded string
762766
}
767+
768+
/**
769+
* @brief Check WiFi health and attempt reconnection if needed
770+
*
771+
* This function checks if WiFi is connected. If not, it attempts to reconnect
772+
* and increments a counter. If the counter reaches 10 consecutive failures,
773+
* the system will reboot. The counter is reset whenever WiFi is connected.
774+
*/
775+
void wifi_health_check(void) {
776+
if (wifi_is_connected()) {
777+
// Reset counter when WiFi is connected
778+
if (s_wifi_disconnect_counter > 0) {
779+
// ESP_LOGI(TAG, "WiFi reconnected successfully, resetting disconnect counter");
780+
s_wifi_disconnect_counter = 0;
781+
}
782+
return;
783+
}
784+
785+
// WiFi is not connected, increment counter
786+
s_wifi_disconnect_counter++;
787+
ESP_LOGW(TAG, "WiFi Health check. Disconnect count: %d/15", s_wifi_disconnect_counter);
788+
789+
// Try to reconnect
790+
if (strlen(s_wifi_ssid) > 0) {
791+
ESP_LOGI(TAG, "Attempting to reconnect to WiFi...");
792+
esp_err_t err = esp_wifi_connect();
793+
if (err != ESP_OK) {
794+
ESP_LOGW(TAG, "WiFi reconnect attempt failed: %s", esp_err_to_name(err));
795+
}
796+
} else {
797+
ESP_LOGW(TAG, "No SSID configured, cannot reconnect");
798+
}
799+
800+
// If counter reaches threshold, reboot the system
801+
if (s_wifi_disconnect_counter >= 15) {
802+
ESP_LOGE(TAG, "WiFi disconnected for 15 consecutive checks. Rebooting system...");
803+
// Wait a moment before rebooting
804+
vTaskDelay(pdMS_TO_TICKS(1000));
805+
esp_restart();
806+
}
807+
}

src/wifi.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,12 @@ void wifi_register_connect_callback(void (*callback)(void));
6868
* @param callback Function to call when WiFi disconnects
6969
*/
7070
void wifi_register_disconnect_callback(void (*callback)(void));
71+
72+
/**
73+
* @brief Check WiFi health and attempt reconnection if needed
74+
*
75+
* Checks if WiFi is connected. If not, attempts to reconnect and increments
76+
* a counter. If the counter reaches 10 consecutive failures, the system will
77+
* reboot. The counter is reset whenever WiFi is connected.
78+
*/
79+
void wifi_health_check(void);

0 commit comments

Comments
 (0)