feat: keep admin portal online

This commit is contained in:
Asklv
2026-03-08 23:04:14 +08:00
parent 6c283553f9
commit 163f946e50
4 changed files with 31 additions and 6 deletions

View File

@@ -159,10 +159,14 @@ void app_main(void)
if (!wifi_ok) {
ESP_LOGW(TAG, "Entering WiFi onboarding mode...");
wifi_onboard_start(); /* blocks, restarts on success */
wifi_onboard_start(WIFI_ONBOARD_MODE_CAPTIVE); /* blocks, restarts on success */
return; /* unreachable */
}
if (wifi_onboard_start(WIFI_ONBOARD_MODE_ADMIN) != ESP_OK) {
ESP_LOGW(TAG, "Local admin portal unavailable; continuing without config hotspot");
}
{
/* Outbound dispatch task should start first to avoid dropping early replies. */
ESP_ERROR_CHECK((xTaskCreatePinnedToCore(

View File

@@ -2,9 +2,14 @@
#include "esp_err.h"
typedef enum {
WIFI_ONBOARD_MODE_CAPTIVE = 0,
WIFI_ONBOARD_MODE_ADMIN,
} wifi_onboard_mode_t;
/**
* Start WiFi onboarding captive portal.
* Opens a soft AP, DNS hijacker, and HTTP configuration server.
* Blocks until the user submits credentials, then saves to NVS and restarts.
* Start WiFi onboarding/configuration portal.
* CAPTIVE mode opens DNS hijack + config page and blocks forever.
* ADMIN mode keeps a local config hotspot alive without captive redirects.
*/
esp_err_t wifi_onboard_start(void);
esp_err_t wifi_onboard_start(wifi_onboard_mode_t mode);

View File

@@ -15,6 +15,7 @@ static EventGroupHandle_t s_wifi_event_group;
static int s_retry_count = 0;
static char s_ip_str[16] = "0.0.0.0";
static bool s_connected = false;
static bool s_reconnect_enabled = true;
static const char *wifi_reason_to_str(wifi_err_reason_t reason)
{
@@ -44,7 +45,7 @@ static void event_handler(void *arg, esp_event_base_t event_base,
if (disc) {
ESP_LOGW(TAG, "Disconnected (reason=%d:%s)", disc->reason, wifi_reason_to_str(disc->reason));
}
if (s_retry_count < MIMI_WIFI_MAX_RETRY) {
if (s_reconnect_enabled && s_retry_count < MIMI_WIFI_MAX_RETRY) {
/* Exponential backoff: 1s, 2s, 4s, 8s, ... capped at 30s */
uint32_t delay_ms = MIMI_WIFI_RETRY_BASE_MS << s_retry_count;
if (delay_ms > MIMI_WIFI_RETRY_MAX_MS) {
@@ -120,6 +121,7 @@ esp_err_t wifi_manager_start(void)
return ESP_ERR_NOT_FOUND;
}
s_reconnect_enabled = true;
ESP_LOGI(TAG, "Connecting to SSID: %s", wifi_cfg.sta.ssid);
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
@@ -252,6 +254,7 @@ bool wifi_manager_has_credentials(void)
esp_err_t wifi_manager_stop(void)
{
s_reconnect_enabled = false;
esp_wifi_disconnect();
esp_wifi_stop();
s_connected = false;
@@ -260,3 +263,11 @@ esp_err_t wifi_manager_stop(void)
ESP_LOGI(TAG, "WiFi stopped");
return ESP_OK;
}
void wifi_manager_set_reconnect_enabled(bool enabled)
{
s_reconnect_enabled = enabled;
if (!enabled) {
s_retry_count = 0;
}
}

View File

@@ -59,3 +59,8 @@ bool wifi_manager_has_credentials(void);
* Stop WiFi (for mode switching during onboarding).
*/
esp_err_t wifi_manager_stop(void);
/**
* Enable or disable STA auto-reconnect on disconnect events.
*/
void wifi_manager_set_reconnect_enabled(bool enabled);