diff options
| author | Your Name <you@example.com> | 2026-05-19 13:33:09 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-19 13:33:09 +0530 |
| commit | 2e65cdfdb582cebe6c4fa0f351df88ffe9c4c21c (patch) | |
| tree | b87f74264351fef39e43c1ab27ddae3b04a89a19 | |
| parent | aa58b47996083f36e3587b8e10f9bbb681610491 (diff) | |
fix: transition display to ERROR after WiFi retries exhausted
Previously the display stayed at BOOT forever when WiFi was configured
but unreachable. Now uses a total retry counter (10 attempts) and
transitions to DISPLAY_ERROR with setup URL when all retries fail.
| -rw-r--r-- | main/tollgate_main.c | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/main/tollgate_main.c b/main/tollgate_main.c index 9421e16..9960ea5 100644 --- a/main/tollgate_main.c +++ b/main/tollgate_main.c | |||
| @@ -26,6 +26,7 @@ | |||
| 26 | #include "display.h" | 26 | #include "display.h" |
| 27 | 27 | ||
| 28 | #define MAX_STA_RETRY 5 | 28 | #define MAX_STA_RETRY 5 |
| 29 | #define MAX_TOTAL_RETRIES 10 | ||
| 29 | static const char *TAG = "tollgate_main"; | 30 | static const char *TAG = "tollgate_main"; |
| 30 | 31 | ||
| 31 | static EventGroupHandle_t s_wifi_event_group; | 32 | static EventGroupHandle_t s_wifi_event_group; |
| @@ -34,6 +35,7 @@ static const int WIFI_CONNECTED_BIT = BIT0; | |||
| 34 | static esp_netif_t *s_sta_netif = NULL; | 35 | static esp_netif_t *s_sta_netif = NULL; |
| 35 | static esp_netif_t *s_ap_netif = NULL; | 36 | static esp_netif_t *s_ap_netif = NULL; |
| 36 | static int s_retry_count = 0; | 37 | static int s_retry_count = 0; |
| 38 | static int s_total_retries = 0; | ||
| 37 | static bool s_services_running = false; | 39 | static bool s_services_running = false; |
| 38 | static SemaphoreHandle_t s_services_mutex = NULL; | 40 | static SemaphoreHandle_t s_services_mutex = NULL; |
| 39 | static char s_ap_ip_str[16] = "10.0.0.1"; | 41 | static char s_ap_ip_str[16] = "10.0.0.1"; |
| @@ -52,13 +54,24 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base, | |||
| 52 | esp_wifi_connect(); | 54 | esp_wifi_connect(); |
| 53 | } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { | 55 | } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { |
| 54 | s_retry_count++; | 56 | s_retry_count++; |
| 55 | ESP_LOGW(TAG, "WiFi disconnected, retry %d/%d", s_retry_count, MAX_STA_RETRY); | 57 | s_total_retries++; |
| 58 | ESP_LOGW(TAG, "WiFi disconnected, retry %d/%d (total %d/%d)", | ||
| 59 | s_retry_count, MAX_STA_RETRY, s_total_retries, MAX_TOTAL_RETRIES); | ||
| 56 | tollgate_client_on_sta_disconnected(); | 60 | tollgate_client_on_sta_disconnected(); |
| 57 | display_notify_wifi_disconnected(); | 61 | display_notify_wifi_disconnected(); |
| 58 | if (s_services_running) { | 62 | if (s_services_running) { |
| 59 | stop_services(); | 63 | stop_services(); |
| 60 | display_set_state(DISPLAY_ERROR); | 64 | display_set_state(DISPLAY_ERROR); |
| 61 | } | 65 | } |
| 66 | if (s_total_retries >= MAX_TOTAL_RETRIES) { | ||
| 67 | ESP_LOGW(TAG, "All WiFi retries exhausted"); | ||
| 68 | const tollgate_config_t *cfg = tollgate_config_get(); | ||
| 69 | char portal_url[128]; | ||
| 70 | snprintf(portal_url, sizeof(portal_url), "http://%s/", cfg->ap_ip_str); | ||
| 71 | display_update(NULL, 0, 0, portal_url, NULL, 0, "WiFi failed"); | ||
| 72 | display_set_state(DISPLAY_ERROR); | ||
| 73 | return; | ||
| 74 | } | ||
| 62 | display_update(NULL, 0, 0, NULL, NULL, 0, "WiFi retry..."); | 75 | display_update(NULL, 0, 0, NULL, NULL, 0, "WiFi retry..."); |
| 63 | if (s_retry_count < MAX_STA_RETRY) { | 76 | if (s_retry_count < MAX_STA_RETRY) { |
| 64 | esp_wifi_connect(); | 77 | esp_wifi_connect(); |
| @@ -71,6 +84,13 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base, | |||
| 71 | ESP_LOGI(TAG, "Trying WiFi network %d: %s", idx, cfg->networks[idx].ssid); | 84 | ESP_LOGI(TAG, "Trying WiFi network %d: %s", idx, cfg->networks[idx].ssid); |
| 72 | s_retry_count = 0; | 85 | s_retry_count = 0; |
| 73 | esp_wifi_connect(); | 86 | esp_wifi_connect(); |
| 87 | } else { | ||
| 88 | ESP_LOGW(TAG, "No more WiFi networks to try"); | ||
| 89 | const tollgate_config_t *cfg = tollgate_config_get(); | ||
| 90 | char portal_url[128]; | ||
| 91 | snprintf(portal_url, sizeof(portal_url), "http://%s/", cfg->ap_ip_str); | ||
| 92 | display_update(NULL, 0, 0, portal_url, NULL, 0, "WiFi failed"); | ||
| 93 | display_set_state(DISPLAY_ERROR); | ||
| 74 | } | 94 | } |
| 75 | } | 95 | } |
| 76 | } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED) { | 96 | } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED) { |
| @@ -101,6 +121,7 @@ static void ip_event_handler(void *arg, esp_event_base_t event_base, | |||
| 101 | ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; | 121 | ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; |
| 102 | ESP_LOGI(TAG, "Got IP:" IPSTR ", GW:" IPSTR, IP2STR(&event->ip_info.ip), IP2STR(&event->ip_info.gw)); | 122 | ESP_LOGI(TAG, "Got IP:" IPSTR ", GW:" IPSTR, IP2STR(&event->ip_info.ip), IP2STR(&event->ip_info.gw)); |
| 103 | s_retry_count = 0; | 123 | s_retry_count = 0; |
| 124 | s_total_retries = 0; | ||
| 104 | xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); | 125 | xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); |
| 105 | 126 | ||
| 106 | char ip_str[16]; | 127 | char ip_str[16]; |