feat: 添加配网模式选择功能,支持按键触发与常驻配网模式

This commit is contained in:
Wang Beihong
2026-03-05 18:46:18 +08:00
parent ebde114be6
commit 32f3f9980d
5 changed files with 117 additions and 27 deletions

View File

@@ -43,6 +43,24 @@
static const char *TAG = "wifi_connect";
static inline bool wifi_connect_is_always_on_mode(void)
{
#if CONFIG_WIFI_CONNECT_PROVISION_MODE_ALWAYS_ON
return true;
#else
return false;
#endif
}
static inline bool wifi_connect_is_button_mode(void)
{
#if CONFIG_WIFI_CONNECT_PROVISION_MODE_BUTTON
return true;
#else
return false;
#endif
}
static void wifi_connect_log_state_i(const char *state, const char *detail)
{
if (detail != NULL && detail[0] != '\0') {
@@ -160,7 +178,7 @@ static const char *s_html_page =
"catch(e){setError('清除失败');setStatus('');}finally{setBusy(false);}}"
"async function pollStatus(){"
"try{const r=await fetch('/api/status');const d=await r.json();setStatus('状态: '+statusText(d.status));setError(d.error||'');"
"if(d.status==='connected'){setStatus('连接成功,配网服务即将关闭');setError('');}}catch(e){setError('状态获取失败');}}"
"if(d.status==='connected'){setStatus('连接成功');setError('');}}catch(e){setError('状态获取失败');}}"
"loadScan();setInterval(pollStatus,2500);"
"</script></body></html>";
@@ -180,6 +198,9 @@ static void wifi_connect_set_error_locked(const char *message)
static void wifi_connect_refresh_idle_timeout(void)
{
if (wifi_connect_is_always_on_mode()) {
return;
}
if (s_ctx.idle_timer == NULL) {
return;
}
@@ -865,12 +886,18 @@ static void wifi_connect_connect_timeout_cb(void *arg)
static void wifi_connect_ap_stop_timer_cb(void *arg)
{
(void)arg;
if (wifi_connect_is_always_on_mode()) {
return;
}
wifi_connect_stop();
}
static void wifi_connect_idle_timeout_cb(void *arg)
{
(void)arg;
if (wifi_connect_is_always_on_mode()) {
return;
}
xSemaphoreTake(s_ctx.lock, portMAX_DELAY);
bool should_stop = s_ctx.provisioning_active;
if (should_stop) {
@@ -920,11 +947,15 @@ static void wifi_connect_event_handler(void *arg, esp_event_base_t event_base, i
}
if (provisioning_active) {
if (CONFIG_WIFI_CONNECT_AP_GRACEFUL_STOP_SEC == 0) {
wifi_connect_stop();
if (wifi_connect_is_always_on_mode()) {
wifi_connect_log_state_i("常驻配网模式", "联网成功后保持配网热点开启");
} else {
esp_timer_stop(s_ctx.ap_stop_timer);
esp_timer_start_once(s_ctx.ap_stop_timer, (uint64_t)CONFIG_WIFI_CONNECT_AP_GRACEFUL_STOP_SEC * 1000000ULL);
if (CONFIG_WIFI_CONNECT_AP_GRACEFUL_STOP_SEC == 0) {
wifi_connect_stop();
} else {
esp_timer_stop(s_ctx.ap_stop_timer);
esp_timer_start_once(s_ctx.ap_stop_timer, (uint64_t)CONFIG_WIFI_CONNECT_AP_GRACEFUL_STOP_SEC * 1000000ULL);
}
}
}
return;
@@ -1091,6 +1122,9 @@ esp_err_t wifi_connect_start(void)
char ap_msg[96] = {0};
snprintf(ap_msg, sizeof(ap_msg), "配网热点已开启SSID=%s访问 http://192.168.4.1", s_ctx.ap_ssid);
wifi_connect_log_state_i("配网已启动", ap_msg);
if (wifi_connect_is_always_on_mode()) {
wifi_connect_log_state_i("当前模式", "常驻配网模式(不会自动关闭)");
}
return ESP_OK;
}
@@ -1163,14 +1197,16 @@ esp_err_t wifi_connect_init(void)
ESP_RETURN_ON_ERROR(esp_wifi_start(), TAG, "wifi start failed");
s_ctx.wifi_started = true;
gpio_config_t io = {
.pin_bit_mask = (1ULL << CONFIG_WIFI_CONNECT_BUTTON_GPIO),
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE,
};
ESP_RETURN_ON_ERROR(gpio_config(&io), TAG, "button gpio config failed");
if (wifi_connect_is_button_mode()) {
gpio_config_t io = {
.pin_bit_mask = (1ULL << CONFIG_WIFI_CONNECT_BUTTON_GPIO),
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE,
};
ESP_RETURN_ON_ERROR(gpio_config(&io), TAG, "button gpio config failed");
}
s_ctx.lock = xSemaphoreCreateMutex();
ESP_RETURN_ON_FALSE(s_ctx.lock != NULL, ESP_ERR_NO_MEM, TAG, "create lock failed");
@@ -1191,11 +1227,19 @@ esp_err_t wifi_connect_init(void)
ESP_RETURN_ON_ERROR(esp_timer_create(&idle_timer_args, &s_ctx.idle_timer), TAG, "idle timer create failed");
ESP_RETURN_ON_ERROR(esp_timer_create(&ap_stop_timer_args, &s_ctx.ap_stop_timer), TAG, "ap stop timer create failed");
BaseType_t ok = xTaskCreate(wifi_connect_button_task, "wifi_btn", 3072, NULL, 4, &s_ctx.button_task);
ESP_RETURN_ON_FALSE(ok == pdPASS, ESP_ERR_NO_MEM, TAG, "button task create failed");
if (wifi_connect_is_button_mode()) {
BaseType_t ok = xTaskCreate(wifi_connect_button_task, "wifi_btn", 3072, NULL, 4, &s_ctx.button_task);
ESP_RETURN_ON_FALSE(ok == pdPASS, ESP_ERR_NO_MEM, TAG, "button task create failed");
}
s_ctx.initialized = true;
if (wifi_connect_is_always_on_mode()) {
wifi_connect_log_state_i("配网模式", "常驻配网(上电自动开启且不会自动关闭)");
} else {
wifi_connect_log_state_i("配网模式", "按键触发配网(长按进入)");
}
err = wifi_connect_try_auto_connect();
if (err != ESP_OK) {
char skip_msg[96] = {0};
@@ -1203,6 +1247,17 @@ esp_err_t wifi_connect_init(void)
wifi_connect_log_state_w("初始化后自动重连未执行", skip_msg);
}
wifi_connect_log_state_i("wifi-connect 初始化完成", "长按按键可进入配网");
if (wifi_connect_is_always_on_mode()) {
wifi_connect_log_state_i("wifi-connect 初始化完成", "常驻配网模式已启用");
err = wifi_connect_start();
if (err != ESP_OK) {
char mode_msg[96] = {0};
snprintf(mode_msg, sizeof(mode_msg), "自动开启配网失败,错误=%s", esp_err_to_name(err));
wifi_connect_log_state_w("常驻配网启动失败", mode_msg);
return err;
}
} else {
wifi_connect_log_state_i("wifi-connect 初始化完成", "长按按键可进入配网");
}
return ESP_OK;
}