From 8c8564fcc9408597acf65d1aac684172a7c4e2af Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Sat, 23 May 2026 21:56:27 -0500 Subject: [PATCH 1/4] feat: Auto-init nvs in wifi if wifi nvs storage enabled in config --- components/wifi/CMakeLists.txt | 2 +- components/wifi/include/wifi.hpp | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/components/wifi/CMakeLists.txt b/components/wifi/CMakeLists.txt index 571cb1da9..8faea49ed 100644 --- a/components/wifi/CMakeLists.txt +++ b/components/wifi/CMakeLists.txt @@ -1,4 +1,4 @@ idf_component_register( INCLUDE_DIRS "include" SRC_DIRS "src" - REQUIRES esp_wifi nvs_flash base_component cli) + REQUIRES esp_wifi nvs base_component cli) diff --git a/components/wifi/include/wifi.hpp b/components/wifi/include/wifi.hpp index 0cf96744f..ac8ba62e2 100644 --- a/components/wifi/include/wifi.hpp +++ b/components/wifi/include/wifi.hpp @@ -15,6 +15,10 @@ #include "wifi_format_helpers.hpp" #include "wifi_sta.hpp" +#if CONFIG_ESP32_WIFI_NVS_ENABLED +#include "nvs.hpp" +#endif + namespace espp { /// @brief The Wifi class provides access to the ESP32 Wifi functionality. @@ -568,7 +572,17 @@ class Wifi : public espp::BaseComponent { /// @details The Wifi object is a singleton object and can only be /// constructed once. WiFi stack initialization is deferred to init(). Wifi() - : BaseComponent("Wifi") {} + : BaseComponent("Wifi") { +#if CONFIG_ESP32_WIFI_NVS_ENABLED + std::error_code ec; + nvs_.init(ec); + if (ec) { + logger_.error("Failed to initialize NVS for WiFi credentials: {}", ec.message()); + } else { + logger_.info("NVS initialized for WiFi credentials"); + } +#endif + } /// @brief Destructor - cleans up WiFi stack. ~Wifi() { @@ -678,6 +692,7 @@ class Wifi : public espp::BaseComponent { return true; } + espp::Nvs nvs_; ///< NVS instance for storing WiFi credentials if enabled mutable std::recursive_mutex mutex_; ///< Mutex for thread-safe access to configs and state std::unordered_map ap_configs_; std::unordered_map sta_configs_; From bdbdb469fdb39461712f435891d5a5166a25999c Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Sat, 23 May 2026 22:10:42 -0500 Subject: [PATCH 2/4] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- components/wifi/include/wifi.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/wifi/include/wifi.hpp b/components/wifi/include/wifi.hpp index ac8ba62e2..f0942f9ea 100644 --- a/components/wifi/include/wifi.hpp +++ b/components/wifi/include/wifi.hpp @@ -692,7 +692,7 @@ class Wifi : public espp::BaseComponent { return true; } - espp::Nvs nvs_; ///< NVS instance for storing WiFi credentials if enabled + espp::Nvs nvs_; ///< NVS instance used to initialize NVS when WiFi NVS support is enabled mutable std::recursive_mutex mutex_; ///< Mutex for thread-safe access to configs and state std::unordered_map ap_configs_; std::unordered_map sta_configs_; From b6a2cd80ab6774c33e105fdcf00a616061ec34ba Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Sat, 23 May 2026 22:20:05 -0500 Subject: [PATCH 3/4] fix bugs and improve docs / example for wifi related to nvs changes --- components/wifi/example/main/wifi_example.cpp | 14 -------------- components/wifi/idf_component.yml | 1 + components/wifi/include/wifi.hpp | 4 +++- components/wifi/include/wifi_ap.hpp | 5 ----- components/wifi/include/wifi_sta.hpp | 5 ----- 5 files changed, 4 insertions(+), 25 deletions(-) diff --git a/components/wifi/example/main/wifi_example.cpp b/components/wifi/example/main/wifi_example.cpp index eb536eb05..ccf0a48ee 100644 --- a/components/wifi/example/main/wifi_example.cpp +++ b/components/wifi/example/main/wifi_example.cpp @@ -4,10 +4,6 @@ #include "sdkconfig.h" -#if CONFIG_ESP32_WIFI_NVS_ENABLED -#include "nvs_flash.h" -#endif - #include "logger.hpp" #include "task.hpp" #include "wifi.hpp" @@ -35,16 +31,6 @@ extern "C" void app_main(void) { size_t num_seconds_to_run = 2; -#if CONFIG_ESP32_WIFI_NVS_ENABLED - // Initialize NVS - esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - ESP_ERROR_CHECK(ret); -#endif - { //! [wifi sta menu example] espp::WifiSta::Config config{.ssid = "", // use whatever was saved to NVS (if any) diff --git a/components/wifi/idf_component.yml b/components/wifi/idf_component.yml index 569c1d8ff..78b158f1c 100644 --- a/components/wifi/idf_component.yml +++ b/components/wifi/idf_component.yml @@ -19,3 +19,4 @@ dependencies: version: '>=5.0' espp/base_component: '>=1.0' espp/cli: '>=1.0' + espp/nvs: '>=1.0' diff --git a/components/wifi/include/wifi.hpp b/components/wifi/include/wifi.hpp index f0942f9ea..b15b7f9eb 100644 --- a/components/wifi/include/wifi.hpp +++ b/components/wifi/include/wifi.hpp @@ -692,7 +692,9 @@ class Wifi : public espp::BaseComponent { return true; } - espp::Nvs nvs_; ///< NVS instance used to initialize NVS when WiFi NVS support is enabled +#if CONFIG_ESP32_WIFI_NVS_ENABLED + espp::Nvs nvs_; ///< NVS instance used to initialize NVS when WiFi NVS support is enabled +#endif mutable std::recursive_mutex mutex_; ///< Mutex for thread-safe access to configs and state std::unordered_map ap_configs_; std::unordered_map sta_configs_; diff --git a/components/wifi/include/wifi_ap.hpp b/components/wifi/include/wifi_ap.hpp index c6ce0229a..129ec8fda 100644 --- a/components/wifi/include/wifi_ap.hpp +++ b/components/wifi/include/wifi_ap.hpp @@ -6,7 +6,6 @@ #include "esp_mac.h" #include "esp_wifi.h" #include "esp_wifi_ap_get_sta_list.h" -#include "nvs_flash.h" #include "base_component.hpp" #include "wifi_base.hpp" @@ -19,10 +18,6 @@ namespace espp { * see * https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/wifi.html#esp32-wi-fi-ap-general-scenario * - * @note If CONFIG_ESP32_WIFI_NVS_ENABLED is set to `y` (which is the - * default), then you must ensure that you call `nvs_flash_init()` - * prior to creating the WiFi Access Point. - * * \section wifiap_ex1 WiFi Access Point Example * \snippet wifi_example.cpp wifi ap example */ diff --git a/components/wifi/include/wifi_sta.hpp b/components/wifi/include/wifi_sta.hpp index 6a8605e6b..3a0088bf2 100644 --- a/components/wifi/include/wifi_sta.hpp +++ b/components/wifi/include/wifi_sta.hpp @@ -8,7 +8,6 @@ #include "esp_event.h" #include "esp_mac.h" #include "esp_wifi.h" -#include "nvs_flash.h" #include "base_component.hpp" #include "wifi_base.hpp" @@ -21,10 +20,6 @@ namespace espp { * see * https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/wifi.html#esp32-wi-fi-station-general-scenario * - * @note If CONFIG_ESP32_WIFI_NVS_ENABLED is set to `y` (which is the - * default), then you must ensure that you call `nvs_flash_init()` - * prior to creating the WiFi Station. - * * \section wifista_ex1 WiFi Station Example * \snippet wifi_example.cpp wifi sta example */ From 7cb22a3137aa26eb9f39d1b1e27b9ce3651b8dfc Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Sat, 23 May 2026 22:25:12 -0500 Subject: [PATCH 4/4] remove wifi-specific nvs code from related examples --- .../dns_server/example/main/dns_server_example.cpp | 10 ---------- components/ftp/example/main/ftp_example.cpp | 14 -------------- .../iperf_menu/example/main/iperf_menu_example.cpp | 14 -------------- components/ping/example/main/ping_example.cpp | 10 ---------- .../example/main/remote_debug_example.cpp | 11 ----------- components/rtsp/example/main/rtsp_example.cpp | 14 -------------- components/socket/example/main/socket_example.cpp | 14 -------------- 7 files changed, 87 deletions(-) diff --git a/components/dns_server/example/main/dns_server_example.cpp b/components/dns_server/example/main/dns_server_example.cpp index cfa573a0c..00430258a 100644 --- a/components/dns_server/example/main/dns_server_example.cpp +++ b/components/dns_server/example/main/dns_server_example.cpp @@ -13,16 +13,6 @@ extern "C" void app_main(void) { logger.info("Starting DNS Server Example"); -#if CONFIG_ESP32_WIFI_NVS_ENABLED - // Initialize NVS - esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - ESP_ERROR_CHECK(ret); -#endif - // Initialize WiFi in AP mode std::string ap_ssid = "ESP-DNS-Test"; std::string ap_password = "testpassword"; diff --git a/components/ftp/example/main/ftp_example.cpp b/components/ftp/example/main/ftp_example.cpp index 4cf35ab62..7e71951e9 100644 --- a/components/ftp/example/main/ftp_example.cpp +++ b/components/ftp/example/main/ftp_example.cpp @@ -4,10 +4,6 @@ #include #include -#if CONFIG_ESP32_WIFI_NVS_ENABLED -#include "nvs_flash.h" -#endif - #include "logger.hpp" #include "task.hpp" #include "wifi_sta.hpp" @@ -23,16 +19,6 @@ extern "C" void app_main(void) { logger.info("Stating socket example!"); -#if CONFIG_ESP32_WIFI_NVS_ENABLED - // Initialize NVS - esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - ESP_ERROR_CHECK(ret); -#endif - std::string ip_address; espp::WifiSta wifi_sta({.ssid = CONFIG_ESP_WIFI_SSID, .password = CONFIG_ESP_WIFI_PASSWORD, diff --git a/components/iperf_menu/example/main/iperf_menu_example.cpp b/components/iperf_menu/example/main/iperf_menu_example.cpp index f1eacb08f..cdf2e91a3 100644 --- a/components/iperf_menu/example/main/iperf_menu_example.cpp +++ b/components/iperf_menu/example/main/iperf_menu_example.cpp @@ -3,10 +3,6 @@ #include "sdkconfig.h" -#if CONFIG_ESP32_WIFI_NVS_ENABLED -#include "nvs_flash.h" -#endif - #include "logger.hpp" #include "task.hpp" #include "wifi_sta.hpp" @@ -20,16 +16,6 @@ extern "C" void app_main(void) { espp::Logger logger({.tag = "iperf_example", .level = espp::Logger::Verbosity::INFO}); logger.info("Starting example..."); -#if CONFIG_ESP32_WIFI_NVS_ENABLED - // Initialize NVS - esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - ESP_ERROR_CHECK(ret); -#endif - { //! [iperf menu example] espp::WifiSta wifi_sta({.ssid = "", // CONFIG_ESP_WIFI_SSID, diff --git a/components/ping/example/main/ping_example.cpp b/components/ping/example/main/ping_example.cpp index ab7ddfa48..6886ae0b3 100644 --- a/components/ping/example/main/ping_example.cpp +++ b/components/ping/example/main/ping_example.cpp @@ -10,16 +10,6 @@ extern "C" void app_main(void) { espp::Logger logger({.tag = "ping_example", .level = espp::Logger::Verbosity::INFO}); logger.info("Starting Ping example..."); -#if CONFIG_ESP32_WIFI_NVS_ENABLED - // Initialize NVS - esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - ESP_ERROR_CHECK(ret); -#endif - { //! [ping_cli_example] // Simple WiFi STA bring-up assumed configured via menu diff --git a/components/remote_debug/example/main/remote_debug_example.cpp b/components/remote_debug/example/main/remote_debug_example.cpp index 746eb0951..ad71bfa15 100644 --- a/components/remote_debug/example/main/remote_debug_example.cpp +++ b/components/remote_debug/example/main/remote_debug_example.cpp @@ -4,7 +4,6 @@ #include "file_system.hpp" #include "logger.hpp" -#include "nvs.hpp" #include "remote_debug.hpp" #include "timer.hpp" #include "wifi_sta.hpp" @@ -16,16 +15,6 @@ extern "C" void app_main(void) { logger.info("Starting Remote Debug Example"); -#if CONFIG_ESP32_WIFI_NVS_ENABLED - // Initialize NVS - esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - ESP_ERROR_CHECK(ret); -#endif - // Initialize filesystem logger.info("Initializing filesystem..."); auto &fs = espp::FileSystem::get(); diff --git a/components/rtsp/example/main/rtsp_example.cpp b/components/rtsp/example/main/rtsp_example.cpp index 0c125b94d..e14f100a9 100644 --- a/components/rtsp/example/main/rtsp_example.cpp +++ b/components/rtsp/example/main/rtsp_example.cpp @@ -6,10 +6,6 @@ #include "sdkconfig.h" -#if CONFIG_ESP32_WIFI_NVS_ENABLED -#include "nvs_flash.h" -#endif - #include "logger.hpp" #include "task.hpp" #include "wifi_sta.hpp" @@ -27,16 +23,6 @@ extern "C" void app_main(void) { logger.info("Starting RTSP example!"); -#if CONFIG_ESP32_WIFI_NVS_ENABLED - // Initialize NVS - esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - ESP_ERROR_CHECK(ret); -#endif - std::string ip_address; espp::WifiSta wifi_sta({.ssid = CONFIG_ESP_WIFI_SSID, .password = CONFIG_ESP_WIFI_PASSWORD, diff --git a/components/socket/example/main/socket_example.cpp b/components/socket/example/main/socket_example.cpp index 43693f094..187b36d93 100644 --- a/components/socket/example/main/socket_example.cpp +++ b/components/socket/example/main/socket_example.cpp @@ -4,10 +4,6 @@ #include #include -#if CONFIG_ESP32_WIFI_NVS_ENABLED -#include "nvs_flash.h" -#endif - #include "logger.hpp" #include "task.hpp" #include "tcp_socket.hpp" @@ -22,16 +18,6 @@ extern "C" void app_main(void) { auto test_duration = 3s; -#if CONFIG_ESP32_WIFI_NVS_ENABLED - // Initialize NVS - esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - ESP_ERROR_CHECK(ret); -#endif - // create a wifi access point here so that LwIP will be init for this example espp::WifiAp wifi_ap({.ssid = "SocketExample", .password = "", // no security