PlatformIO中ESP-IDF框架下ESP32 WiFi配置不更新问题求助
问题解决:PlatformIO中ESP-IDF代码无法覆盖旧WiFi配置
问题描述
此前通过Espressif-IDE使用Kconfig.projbuild配置了ESP32的WiFi SSID与密码,切换到PlatformIO的ESP-IDF框架编写新代码后,代码中定义了新的WiFi SSID和密码,但设备仍沿用旧配置。已尝试通过PlatformIO的擦除Flash功能清除旧配置,擦除后设备显示ESP32默认SSID,推测PlatformIO无法正确写入新的WiFi配置。相关代码如下:
#include <stdio.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/timers.h" #include "freertos/event_groups.h" #include "esp_wifi.h" #include "esp_log.h" #include "nvs_flash.h" #include "esp_netif.h" #include "esp_http_server.h" #define SSID "new ssid" #define PASS "new pasword" static void wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { switch (event_id) { case WIFI_EVENT_STA_START: printf("WiFi connecting ... \n"); break; case WIFI_EVENT_STA_CONNECTED: printf("WiFi connected ... \n"); break; case WIFI_EVENT_STA_DISCONNECTED: printf("WiFi lost connection ... \n"); break; case IP_EVENT_STA_GOT_IP: printf("WiFi got IP ... \n\n"); break; default: break; } } void wifi_connection() { // 1 - Wi-Fi/LwIP Init Phase esp_netif_init(); // TCP/IP initiation s1.1 esp_event_loop_create_default(); // event loop s1.2 esp_netif_create_default_wifi_sta(); // WiFi station s1.3 wifi_init_config_t wifi_initiation = WIFI_INIT_CONFIG_DEFAULT(); esp_wifi_init(&wifi_initiation); // s1.4 // 2 - Wi-Fi Configuration Phase esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, NULL); esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler, NULL); wifi_config_t wifi_configuration = { .sta = { .ssid = SSID, .password = PASS}}; esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_configuration); // 3 - Wi-Fi Start Phase esp_wifi_start(); // 4- Wi-Fi Connect Phase esp_wifi_connect(); } static esp_err_t get_handler(httpd_req_t *req) { char *response_message = "URI Response ..."; httpd_resp_send(req, response_message, HTTPD_RESP_USE_STRLEN); return ESP_OK; } void server_initiation() { httpd_config_t server_config = HTTPD_DEFAULT_CONFIG(); httpd_handle_t server_handle = NULL; httpd_start(&server_handle, &server_config); httpd_uri_t uri_get = { .uri = "/", .method = HTTP_GET, .handler = get_handler, .user_ctx = NULL}; httpd_register_uri_handler(server_handle, &uri_get); } void app_main(void) { nvs_flash_init(); wifi_connection(); server_initiation(); }
原因分析
- Kconfig配置优先级更高:Espressif-IDE中通过Kconfig.projbuild设置的WiFi参数会被编译到固件的配置分区,优先级高于代码中直接设置的参数。即使擦除Flash,若PlatformIO编译时仍继承旧Kconfig配置,设备会优先读取旧参数。
- NVS存储残留:擦除Flash时可能没完全清除NVS分区,旧WiFi配置仍存在其中,设备启动后自动加载NVS中的配置,覆盖代码设置。
- 代码逻辑缺失:ESP-IDF默认优先从NVS读取WiFi参数,当前代码未禁用这个逻辑,导致代码中的新配置不生效。
解决方案
1. 修改代码强制覆盖配置
在调用esp_wifi_set_config前,添加代码禁用NVS加载并清除旧存储:
// 禁用从NVS自动加载WiFi配置,改用RAM存储 esp_wifi_set_storage(WIFI_STORAGE_RAM); // 清除NVS中旧的WiFi配置(确保彻底清除) esp_wifi_restore();
修改后的wifi_connection函数:
void wifi_connection() { // 1 - Wi-Fi/LwIP Init Phase esp_netif_init(); // TCP/IP initiation s1.1 esp_event_loop_create_default(); // event loop s1.2 esp_netif_create_default_wifi_sta(); // WiFi station s1.3 wifi_init_config_t wifi_initiation = WIFI_INIT_CONFIG_DEFAULT(); esp_wifi_init(&wifi_initiation); // s1.4 // 新增:禁用NVS存储,强制使用代码配置 esp_wifi_set_storage(WIFI_STORAGE_RAM); esp_wifi_restore(); // 2 - Wi-Fi Configuration Phase esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, NULL); esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler, NULL); wifi_config_t wifi_configuration = { .sta = { .ssid = SSID, .password = PASS}}; esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_configuration); // 3 - Wi-Fi Start Phase esp_wifi_start(); // 4- Wi-Fi Connect Phase esp_wifi_connect(); }
2. 全芯片擦除Flash
如果普通擦除无效,用PlatformIO命令行执行全Flash擦除:
pio run --target erase
或者在PlatformIO IDE的设备菜单中选择「Erase Flash (Full Chip)」。
3. 清理PlatformIO中的Kconfig配置
- 删除项目中可能存在的
Kconfig.projbuild文件; - 在
platformio.ini中添加编译标志,清空默认Kconfig的WiFi参数:
[env:esp32dev] platform = espressif32 board = esp32dev framework = espidf build_flags = -DCONFIG_WIFI_SSID="" -DCONFIG_WIFI_PASSWORD=""
验证方法
- 重新编译并上传修改后的代码;
- 查看串口输出,确认设备尝试连接代码中定义的新SSID;
- 若仍有问题,执行全芯片擦除后再次上传代码。
内容的提问来源于stack exchange,提问作者yaser kh
相关产品推荐
相关产品推荐

