diff --git a/src/main.c b/src/main.c index 0311334..b0d8d13 100644 --- a/src/main.c +++ b/src/main.c @@ -146,6 +146,15 @@ static void websocket_event_handler(void *handler_args, esp_event_base_t base, } } +// Whenever we fall back into AP+STA, redraw the config page: +static void show_config_screen(void) { + ESP_LOGI(TAG, "Loading Config WEBP"); + // Render the “TRONBYT-CONFIG” WebP asset + gfx_update(ASSET_CONFIG_WEBP, ASSET_CONFIG_WEBP_LEN); + // Force an immediate repaint + isAnimating = -1; +} + void app_main(void) { ESP_LOGI(TAG, "App Main Start"); @@ -171,6 +180,7 @@ void app_main(void) { ESP_LOGE(TAG, "failed to initialize WiFi"); return; } + wifi_register_disconnect_callback(show_config_screen); esp_register_shutdown_handler(&wifi_shutdown); // Wait a bit for the AP to start @@ -190,10 +200,8 @@ void app_main(void) { ESP_LOGI(TAG, "WiFi connected successfully!"); } - // Load up the config webp so that we don't just loop the boot screen over and over again but show the ap config info webp - ESP_LOGI(TAG, "Loading Config WEBP"); - gfx_update(ASSET_CONFIG_WEBP, ASSET_CONFIG_WEBP_LEN); + show_config_screen(); if (!wifi_is_connected()) { ESP_LOGW(TAG,"Pausing main task until wifi connected . . . "); diff --git a/src/wifi.c b/src/wifi.c index 3662c92..4399b62 100644 --- a/src/wifi.c +++ b/src/wifi.c @@ -121,6 +121,16 @@ static const char *s_success_html = "" "" ""; +// Returns true if our AP interface is currently active +static bool ap_is_running(void) { + wifi_mode_t mode; + if (esp_wifi_get_mode(&mode) != ESP_OK) { + return false; + } + // APSTA or AP-only both count + return (mode == WIFI_MODE_APSTA || mode == WIFI_MODE_AP); +} + // Function prototypes static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data); static esp_err_t save_wifi_config_to_nvs(void); @@ -381,10 +391,15 @@ void wifi_register_disconnect_callback(void (*callback)(void)) { static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { if (event_base == WIFI_EVENT) { if (event_id == WIFI_EVENT_STA_START) { - // STA started, reset reconnection counter and try to connect - s_reconnect_attempts = 0; - s_connection_given_up = false; - esp_wifi_connect(); + if (!s_connection_given_up) { + // Only auto-reconnect if we're still “trying” + s_reconnect_attempts = 0; + ESP_LOGI(TAG, "STA_START: auto-connecting to %s", s_wifi_ssid); + esp_wifi_connect(); + } else { + // We've already given up and left AP up — do nothing + ESP_LOGI(TAG, "STA_START: in fallback mode, skipping auto-connect"); + } } else if (event_id == WIFI_EVENT_STA_DISCONNECTED) { // Increment reconnection counter s_reconnect_attempts++; @@ -402,7 +417,13 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t e if (s_reconnect_attempts >= MAX_RECONNECT_ATTEMPTS && !s_connection_given_up) { ESP_LOGW(TAG, "Maximum reconnection attempts (%d) reached, giving up", MAX_RECONNECT_ATTEMPTS); s_connection_given_up = true; - // We'll continue in AP mode only at this point + + // Flip back into AP so config SSID comes back + if (!ap_is_running()) { + ESP_LOGI(TAG, "Switching to AP-only mode for configuration"); + ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_AP) ); + ESP_ERROR_CHECK( esp_netif_dhcps_start(s_ap_netif) ); + } } else if (!s_connection_given_up) { // Only try to reconnect if we haven't given up yet ESP_LOGI(TAG, "WiFi disconnected, trying to reconnect... (attempt %d/%d)", @@ -428,6 +449,14 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t e xEventGroupClearBits(s_wifi_event_group, WIFI_FAIL_BIT); xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); + if (ap_is_running()) { + ESP_LOGI(TAG, "Station connected - disabling configuration AP"); + // Also, stop the DHCP server + ESP_ERROR_CHECK(esp_netif_dhcps_stop(s_ap_netif)); + // Drop the AP interface, leaving STA-only + ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); + } + // Call connect callback if registered if (s_connect_callback != NULL) { s_connect_callback(); @@ -678,6 +707,10 @@ static esp_err_t save_handler(httpd_req_t *req) { httpd_resp_send(req, s_success_html, strlen(s_success_html)); // Connect to the new AP + ESP_LOGI(TAG, "Re-enabling AP+STA to attempt new connection"); + ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_APSTA) ); + // Enable the DHCP server here, just in case we were previously in STA mode only (no DHCP) + esp_netif_dhcps_start(s_ap_netif); connect_to_ap(); return ESP_OK;