Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions components/wifi/example/main/wifi_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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");
Expand Down
10 changes: 9 additions & 1 deletion components/wifi/include/wifi_ap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}", err);
}

// NOTE: Configure phase
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
Expand Down Expand Up @@ -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:
Expand All @@ -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
11 changes: 10 additions & 1 deletion components/wifi/include/wifi_sta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}", err);
}

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
err = esp_wifi_init(&cfg);
Expand Down Expand Up @@ -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_);
}

/**
Expand Down Expand Up @@ -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};
Expand Down