Skip to content

Disable AP mode after we're configured and connected to WiFi #41

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

Closed
wants to merge 5 commits into from
Closed
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
14 changes: 11 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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
Expand All @@ -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 . . . ");
Expand Down
43 changes: 38 additions & 5 deletions src/wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ static const char *s_success_html = "<!DOCTYPE html>"
"</body>"
"</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);
Expand Down Expand Up @@ -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++;
Expand All @@ -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)",
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down