diff --git a/components/wifi/example/main/wifi_example.cpp b/components/wifi/example/main/wifi_example.cpp index 8df1b3b97..c3b6fb4b5 100644 --- a/components/wifi/example/main/wifi_example.cpp +++ b/components/wifi/example/main/wifi_example.cpp @@ -28,6 +28,7 @@ extern "C" void app_main(void) { #endif { + fmt::print("Starting WiFi STA example...\n"); //! [wifi sta example] espp::WifiSta wifi_sta({.ssid = CONFIG_ESP_WIFI_SSID, .password = CONFIG_ESP_WIFI_PASSWORD, @@ -44,14 +45,17 @@ extern "C" void app_main(void) { //! [wifi sta example] std::this_thread::sleep_for(num_seconds_to_run * 1s); + fmt::print("WiFi STA example complete!\n"); } { + fmt::print("Starting WiFi AP example...\n"); //! [wifi ap example] espp::WifiAp wifi_ap({.ssid = CONFIG_ESP_WIFI_SSID, .password = CONFIG_ESP_WIFI_PASSWORD}); //! [wifi ap example] std::this_thread::sleep_for(num_seconds_to_run * 1s); + fmt::print("WiFi AP example complete!\n"); } fmt::print("WiFi example complete!\n"); diff --git a/components/wifi/include/wifi_ap.hpp b/components/wifi/include/wifi_ap.hpp index b3f572688..deb7ed20a 100644 --- a/components/wifi/include/wifi_ap.hpp +++ b/components/wifi/include/wifi_ap.hpp @@ -54,8 +54,12 @@ class WifiAp : public BaseComponent { if (err != ESP_OK) { logger_.error("Could not create default event loop: {}", err); } + logger_.debug("Creating default WiFi AP"); - esp_netif_create_default_wifi_ap(); + netif_ = esp_netif_create_default_wifi_ap(); + if (netif_ == nullptr) { + logger_.error("Could not create default WiFi AP"); + } // NOTE: Configure phase wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); @@ -129,6 +133,9 @@ class WifiAp : public BaseComponent { logger_.error("Could not deinit WiFiAp: {}", err); } logger_.info("WiFi stopped"); + // destroy (free the memory) + logger_.debug("Destroying default WiFi AP"); + esp_netif_destroy_default_wifi(netif_); } protected: @@ -150,6 +157,7 @@ class WifiAp : public BaseComponent { } } + esp_netif_t *netif_{nullptr}; ///< Pointer to the default WiFi AP netif. esp_event_handler_instance_t *event_handler_instance_{nullptr}; }; } // namespace espp diff --git a/components/wifi/include/wifi_sta.hpp b/components/wifi/include/wifi_sta.hpp index edd511c92..a6b578796 100644 --- a/components/wifi/include/wifi_sta.hpp +++ b/components/wifi/include/wifi_sta.hpp @@ -83,7 +83,12 @@ class WifiSta : public BaseComponent { if (err != ESP_OK) { logger_.error("Could not create default event loop: {}", err); } - esp_netif_create_default_wifi_sta(); + + // Create default WiFi STA + netif_ = esp_netif_create_default_wifi_sta(); + if (netif_ == nullptr) { + logger_.error("Could not create default WiFi STA"); + } wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); err = esp_wifi_init(&cfg); @@ -164,6 +169,9 @@ class WifiSta : public BaseComponent { logger_.error("Could not deinit WiFiSta: {}", err); } logger_.info("WiFi stopped"); + // destroy (free the memory) + logger_.debug("Destroying default WiFi STA"); + esp_netif_destroy_default_wifi(netif_); } /** @@ -213,6 +221,7 @@ class WifiSta : public BaseComponent { size_t attempts_{0}; size_t num_retries_{0}; + esp_netif_t *netif_{nullptr}; connect_callback connect_callback_{nullptr}; disconnect_callback disconnect_callback_{nullptr}; ip_callback ip_callback_{nullptr};