WiFi Station (STA)
The WiFi station enables the ESP to scan for and connect to an exising WiFi access point.
API Reference
Header File
Classes
-
class WifiSta : public espp::BaseComponent
WiFi Station (STA)
WiFi Station Example
espp::WifiSta wifi_sta({.ssid = CONFIG_ESP_WIFI_SSID, .password = CONFIG_ESP_WIFI_PASSWORD, .num_connect_retries = CONFIG_ESP_MAXIMUM_RETRY, .on_connected = nullptr, .on_disconnected = nullptr, .on_got_ip = [](ip_event_got_ip_t *eventdata) { fmt::print("got IP: {}.{}.{}.{}\n", IP2STR(&eventdata->ip_info.ip)); }}); while (!wifi_sta.is_connected()) { std::this_thread::sleep_for(100ms); }
Note
If CONFIG_ESP32_WIFI_NVS_ENABLED is set to
y
(which is the default), then you must ensure that you callnvs_flash_init()
prior to creating the WiFi Station.Public Types
-
typedef std::function<void(void)> connect_callback
called when the WiFi station connects to an access point.
-
typedef std::function<void(void)> disconnect_callback
Called when the WiFi station is disconnected from the access point and has exceeded the configured Config::num_connect_retries.
-
typedef std::function<void(ip_event_got_ip_t *ip_evt)> ip_callback
Called whe nthe WiFi station has gotten an IP from the access point.
- Param ip_evt
Pointer to IP Event data structure (contains ip address).
Public Functions
-
inline explicit WifiSta(const Config &config)
Initialize the WiFi Station (STA)
- Parameters
config – WifiSta::Config structure with initialization information.
-
inline ~WifiSta()
Stop the WiFi station and deinit the wifi subystem.
-
inline bool is_connected() const
Whether the station is connected to an access point.
- Returns
true if it is currently connected, false otherwise.
-
inline const std::string &get_name() const
Get the name of the component
Note
This is the tag of the logger
- Returns
A const reference to the name of the component
-
inline void set_log_tag(const std::string_view &tag)
Set the tag for the logger
- Parameters
tag – The tag to use for the logger
-
inline espp::Logger::Verbosity get_log_level() const
Get the log level for the logger
See also
See also
- Returns
The verbosity level of the logger
-
inline void set_log_level(espp::Logger::Verbosity level)
Set the log level for the logger
See also
See also
- Parameters
level – The verbosity level to use for the logger
-
inline void set_log_verbosity(espp::Logger::Verbosity level)
Set the log verbosity for the logger
See also
See also
See also
Note
This is a convenience method that calls set_log_level
- Parameters
level – The verbosity level to use for the logger
-
inline espp::Logger::Verbosity get_log_verbosity() const
Get the log verbosity for the logger
See also
See also
See also
Note
This is a convenience method that calls get_log_level
- Returns
The verbosity level of the logger
-
inline void set_log_rate_limit(std::chrono::duration<float> rate_limit)
Set the rate limit for the logger
See also
Note
Only calls to the logger that have _rate_limit suffix will be rate limited
- Parameters
rate_limit – The rate limit to use for the logger
-
struct Config
Public Members
-
std::string ssid
SSID for the access point.
-
std::string password
Password for the access point. If empty, the AP will be open / have no security.
-
size_t num_connect_retries{0}
Number of times to retry connecting to the AP before stopping. After this many retries, on_disconnected will be called.
-
connect_callback on_connected{nullptr}
Called when the station connects, or fails to connect.
-
disconnect_callback on_disconnected = {nullptr}
Called when the station disconnects.
-
ip_callback on_got_ip = {nullptr}
Called when the station gets an IP address.
-
uint8_t channel = {0}
Channel of target AP; set to 0 for unknown.
-
bool set_ap_mac = {false}
Whether to check MAC address of the AP (generally no). If yes, provide ap_mac.
-
uint8_t ap_mac[6] = {0}
MAC address of the AP to check if set_ap_mac is set to true.
-
std::string ssid
-
typedef std::function<void(void)> connect_callback