20#if defined(USE_ESP32) && defined(USE_WIFI_WPA2_EAP)
21#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
22#include <esp_eap_client.h>
29#include <ESP8266WiFi.h>
30#include <ESP8266WiFiType.h>
32#if defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE < VERSION_CODE(2, 4, 0)
34#include <user_interface.h>
42#include "cyw43_country.h"
43#include "pico/cyw43_arch.h"
49#if defined(USE_ESP32) && defined(SOC_WIFI_SUPPORT_5G)
50#include <esp_wifi_types.h>
53#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
54#include <freertos/FreeRTOS.h>
55#include <freertos/semphr.h>
61static constexpr int8_t WIFI_RSSI_DISCONNECTED = -127;
64static constexpr size_t SSID_BUFFER_SIZE = 33;
106#ifdef USE_WIFI_FAST_CONNECT
151#ifdef USE_WIFI_WPA2_EAP
170static constexpr size_t WIFI_SCAN_RESULT_FILTERED_RESERVE = 8;
174#if defined(USE_RP2040) || defined(USE_ESP32)
208 bool operator!=(
const char *other)
const {
return !(*
this == other); }
213 std::memcpy(&ptr, this->
storage_,
sizeof(ptr));
226static_assert(
sizeof(
CompactString) == 20,
"CompactString must be exactly 20 bytes");
233static_assert(std::is_standard_layout<CompactString>::value,
"CompactString must be standard layout");
234static_assert(!std::is_polymorphic<CompactString>::value,
"CompactString must not have vtable");
241 void set_ssid(
const std::string &ssid);
249#ifdef USE_WIFI_WPA2_EAP
255#ifdef USE_WIFI_MANUAL_IP
263#ifdef USE_WIFI_WPA2_EAP
269#ifdef USE_WIFI_MANUAL_IP
277#ifdef USE_WIFI_WPA2_EAP
280#ifdef USE_WIFI_MANUAL_IP
294 WiFiScanResult(
const bssid_t &bssid,
const char *ssid,
size_t ssid_len, uint8_t channel, int8_t rssi,
bool with_auth,
451#if defined(USE_ESP32) && defined(SOC_WIFI_SUPPORT_5G)
457 void save_wifi_sta(
const std::string &ssid,
const std::string &password);
464 void setup()
override;
470#ifdef USE_LOOP_PRIORITY
475 void loop()
override;
481#ifdef USE_WIFI_11KV_SUPPORT
497 if (it.bssid == bssid)
504 if (it.bssid == bssid)
513 ESPDEPRECATED(
"Use wifi_ssid_to() instead. Removed in 2026.9.0",
"2026.3.0")
514 std::
string wifi_ssid();
517 const
char *
wifi_ssid_to(std::span<
char, SSID_BUFFER_SIZE> buffer);
526#ifdef USE_WIFI_CONNECT_TRIGGER
529#ifdef USE_WIFI_DISCONNECT_TRIGGER
535#ifdef USE_WIFI_IP_STATE_LISTENERS
541#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
547#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
555#ifdef USE_WIFI_POWER_SAVE_LISTENERS
562#ifdef USE_WIFI_RUNTIME_POWER_SAVE
654 if (this->
sta_.empty())
656 for (
const auto &ap : this->
sta_) {
673#if defined(USE_ESP32) && defined(SOC_WIFI_SUPPORT_5G)
697#ifdef USE_WIFI_FAST_CONNECT
710#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
716#ifdef USE_WIFI_IP_STATE_LISTENERS
720#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
753#ifdef USE_WIFI_IP_STATE_LISTENERS
756#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
759#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
762#ifdef USE_WIFI_POWER_SAVE_LISTENERS
766#ifdef USE_WIFI_FAST_CONNECT
769#ifdef USE_WIFI_CONNECT_TRIGGER
772#ifdef USE_WIFI_DISCONNECT_TRIGGER
775#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
798#if defined(USE_ESP32) && defined(SOC_WIFI_SUPPORT_5G)
815#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
822#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
831#if defined(USE_ESP8266) && defined(USE_WIFI_IP_STATE_LISTENERS)
834#if defined(USE_ESP8266) && defined(USE_WIFI_SCAN_RESULTS_LISTENERS)
839#if defined(USE_WIFI_CONNECT_TRIGGER) || defined(USE_WIFI_DISCONNECT_TRIGGER)
847#ifdef USE_WIFI_11KV_SUPPORT
858#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
865 const char *use_address_{
""};
BedjetMode mode
BedJet operating mode.
ESPDEPRECATED("Use mark_failed(LOG_STR(\"static string literal\")) instead. Do NOT use .c_str() from temporary " "strings. Will stop working in 2026.6.0", "2025.12.0") void mark_failed(const char *message)
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Minimal static vector - saves memory by avoiding std::vector overhead.
StringRef is a reference to a string owned by something else.
constexpr const char * c_str() const
constexpr size_type size() const
20-byte string: 18 chars inline + null, heap for longer.
const char * data() const
StringRef ref() const
Return a StringRef view of this string (zero-copy)
bool operator!=(const CompactString &other) const
CompactString & operator=(const CompactString &other)
bool operator==(const CompactString &other) const
static constexpr uint8_t INLINE_CAPACITY
bool operator==(const char *other) const
bool operator!=(const StringRef &other) const
const char * c_str() const
char storage_[INLINE_CAPACITY+1]
static constexpr uint8_t MAX_LENGTH
bool operator!=(const char *other) const
void set_heap_ptr_(char *ptr)
char * get_heap_ptr_() const
uint8_t get_channel() const
StringRef get_ssid() const
void set_ssid(const std::string &ssid)
const optional< EAPAuth > & get_eap() const
void set_ssid(StringRef ssid)
void set_bssid(const bssid_t &bssid)
void set_channel(uint8_t channel)
StringRef get_password() const
optional< ManualIP > manual_ip_
void set_eap(optional< EAPAuth > eap_auth)
void set_password(const std::string &password)
void set_manual_ip(optional< ManualIP > manual_ip)
const optional< ManualIP > & get_manual_ip() const
int8_t get_priority() const
void set_hidden(bool hidden)
const bssid_t & get_bssid() const
void set_priority(int8_t priority)
void set_password(StringRef password)
This component is responsible for managing the ESP WiFi interface.
void notify_scan_results_listeners_()
Notify scan results listeners with current scan results.
bool is_captive_portal_active_()
bool error_from_callback_
void add_sta(const WiFiAP &ap)
bool load_fast_connect_settings_(WiFiAP ¶ms)
void add_connect_state_listener(WiFiConnectStateListener *listener)
Add a listener for WiFi connection state changes.
bool wifi_apply_power_save_()
void set_ap(const WiFiAP &ap)
Setup an Access Point that should be created if no connection to a station can be made.
bool request_high_performance()
Request high-performance mode (no power saving) for improved WiFi latency.
ESPPreferenceObject pref_
void set_sta(const WiFiAP &ap)
WiFiPowerSaveMode power_save_
bool has_sta_priority(const bssid_t &bssid)
const WiFiAP * get_selected_sta_() const
WiFiSTAConnectStatus wifi_sta_connect_status_() const
uint32_t roaming_last_check_
void set_band_mode(wifi_band_mode_t band_mode)
int8_t get_sta_priority(const bssid_t bssid)
void log_and_adjust_priority_for_failed_connect_()
Log failed connection and decrease BSSID priority to avoid repeated attempts.
void init_sta(size_t count)
void save_wifi_sta(const std::string &ssid, const std::string &password)
void notify_connect_state_listeners_()
Notify connect state listeners (called after state machine reaches STA_CONNECTED)
struct esphome::wifi::WiFiComponent::@175 pending_
WiFiComponentState state_
wifi_scan_vector_t< WiFiScanResult > scan_result_
bool skip_cooldown_next_cycle_
WiFiPowerSaveMode configured_power_save_
bool wifi_apply_band_mode_()
void set_sta_priority(bssid_t bssid, int8_t priority)
StaticVector< WiFiScanResultsListener *, ESPHOME_WIFI_SCAN_RESULTS_LISTENERS > scan_results_listeners_
void print_connect_params_()
void loop() override
Reconnect WiFi if required.
void notify_ip_state_listeners_()
Notify IP state listeners with current addresses.
void start_connecting(const WiFiAP &ap)
void set_enable_on_boot(bool enable_on_boot)
void advance_to_next_target_or_increment_retry_()
Advance to next target (AP/SSID) within current phase, or increment retry counter Called when staying...
void add_power_save_listener(WiFiPowerSaveListener *listener)
Add a listener for WiFi power save mode changes.
void check_roaming_(uint32_t now)
bool wifi_sta_ip_config_(const optional< ManualIP > &manual_ip)
static constexpr uint32_t ROAMING_CHECK_INTERVAL
RoamingState roaming_state_
void check_scanning_finished()
void process_roaming_scan_()
static int s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result)
SemaphoreHandle_t high_performance_semaphore_
network::IPAddress get_dns_address(int num)
Trigger * get_disconnect_trigger()
static void wifi_event_callback(System_Event_t *event)
WiFiComponent()
Construct a WiFiComponent.
wifi_band_mode_t band_mode_
void wifi_process_event_(IDFWiFiEvent *data)
std::vector< WiFiSTAPriority > sta_priorities_
int8_t selected_sta_index_
static constexpr int8_t ROAMING_GOOD_RSSI
void save_fast_connect_settings_()
void notify_disconnect_state_listeners_()
Notify connect state listeners of disconnection.
void set_min_auth_mode(WifiMinAuthMode min_auth_mode)
Trigger * get_connect_trigger()
bool wifi_start_ap_(const WiFiAP &ap)
StaticVector< WiFiConnectStateListener *, ESPHOME_WIFI_CONNECT_STATE_LISTENERS > connect_state_listeners_
void log_discarded_scan_result_(const char *ssid, const uint8_t *bssid, int8_t rssi, uint8_t channel)
Log a discarded scan result at VERBOSE level (skipped during roaming scans to avoid log overflow)
int32_t get_wifi_channel()
ESPDEPRECATED("Use wifi_ssid_to() instead. Removed in 2026.9.0", "2026.3.0") std const char * wifi_ssid_to(std::span< char, SSID_BUFFER_SIZE > buffer)
Write SSID to buffer without heap allocation.
void start_connecting(const WiFiAP &ap, bool)
network::IPAddress wifi_subnet_mask_()
bool has_saved_wifi_settings_
static constexpr uint8_t ROAMING_MAX_ATTEMPTS
network::IPAddress wifi_soft_ap_ip()
void set_passive_scan(bool passive)
uint8_t roaming_attempts_
void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info)
network::IPAddress wifi_gateway_ip_()
static void s_wifi_scan_done_callback(void *arg, STATUS status)
void set_power_save_mode(WiFiPowerSaveMode power_save)
bool is_ap_active() const
int8_t find_next_hidden_sta_(int8_t start_index)
Find next SSID that wasn't in scan results (might be hidden) Returns index of next potentially hidden...
void add_ip_state_listener(WiFiIPStateListener *listener)
Add a listener for IP state changes.
ESPPreferenceObject fast_connect_pref_
void clear_priorities_if_all_min_()
Clear BSSID priority tracking if all priorities are at minimum (saves memory)
void wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result)
WiFiRetryPhase determine_next_phase_()
Determine next retry phase based on current state and failure conditions.
network::IPAddress wifi_dns_ip_(int num)
float get_loop_priority() const override
bool wifi_apply_hostname_()
bool wifi_sta_pre_setup_()
network::IPAddresses get_ip_addresses()
static constexpr int8_t ROAMING_MIN_IMPROVEMENT
bool matches_configured_network_(const char *ssid, const uint8_t *bssid) const
Check if network matches any configured network (for scan result filtering) Matches by SSID when conf...
float get_setup_priority() const override
WIFI setup_priority.
void set_output_power(float output_power)
bool is_esp32_improv_active_()
StaticVector< WiFiIPStateListener *, ESPHOME_WIFI_IP_STATE_LISTENERS > ip_state_listeners_
FixedVector< WiFiAP > sta_
int8_t find_first_non_hidden_index_() const
Find the index of the first non-hidden network Returns where EXPLICIT_HIDDEN phase would have stopped...
void release_scan_results_()
Free scan results memory unless a component needs them.
bool wifi_ap_ip_config_(const optional< ManualIP > &manual_ip)
bool needs_scan_results_() const
Check if we need valid scan results for the current phase but don't have any Returns true if the phas...
void add_scan_results_listener(WiFiScanResultsListener *listener)
Add a listener for WiFi scan results.
WiFiRetryPhase retry_phase_
bool transition_to_phase_(WiFiRetryPhase new_phase)
Transition to a new retry phase with logging Returns true if a scan was started (caller should wait),...
bool needs_full_scan_results_() const
Check if full scan results are needed (captive portal active, improv, listeners)
WiFiAP build_params_for_current_phase_()
bool is_high_performance_mode_
void dump_config() override
void clear_roaming_state_()
void set_ap_timeout(uint32_t ap_timeout)
bool release_high_performance()
Release a high-performance mode request.
RetryHiddenMode retry_hidden_mode_
StaticVector< WiFiPowerSaveListener *, ESPHOME_WIFI_POWER_SAVE_LISTENERS > power_save_listeners_
WifiMinAuthMode min_auth_mode_
bool wifi_apply_output_power_(float output_power)
bool has_completed_scan_after_captive_portal_start_
void set_post_connect_roaming(bool enabled)
const char * get_use_address() const
bool is_connected() const
bool wifi_sta_connect_(const WiFiAP &ap)
bool went_through_explicit_hidden_phase_() const
Check if we went through EXPLICIT_HIDDEN phase (first network is marked hidden) Used in RETRY_HIDDEN ...
bool wifi_mode_(optional< bool > sta, optional< bool > ap)
void set_reboot_timeout(uint32_t reboot_timeout)
bool can_proceed() override
network::IPAddresses wifi_sta_ip_addresses()
void check_connecting_finished(uint32_t now)
uint8_t num_ipv6_addresses_
void set_keep_scan_results(bool keep_scan_results)
void start_initial_connection_()
Start initial connection - either scan or connect directly to hidden networks.
bool wifi_scan_start_(bool passive)
bool ssid_was_seen_in_scan_(const CompactString &ssid) const
Check if an SSID was seen in the most recent scan results Used to skip hidden mode for SSIDs we know ...
bool all_networks_hidden_() const
bool handled_connected_state_
void save_wifi_sta(StringRef ssid, StringRef password)
void wifi_scan_done_callback_()
void setup() override
Setup WiFi interface.
void clear_all_bssid_priorities_()
Clear all BSSID priority penalties after successful connection (stale after disconnect)
void set_use_address(const char *use_address)
bool post_connect_roaming_
void process_pending_callbacks_()
void reset_selected_ap_to_first_if_invalid_()
const wifi_scan_vector_t< WiFiScanResult > & get_scan_result() const
Trigger disconnect_trigger_
Listener interface for WiFi connection state changes.
virtual void on_wifi_connect_state(StringRef ssid, std::span< const uint8_t, 6 > bssid)=0
Listener interface for WiFi IP state changes.
virtual void on_ip_state(const network::IPAddresses &ips, const network::IPAddress &dns1, const network::IPAddress &dns2)=0
Listener interface for WiFi power save mode changes.
virtual void on_wifi_power_save(WiFiPowerSaveMode mode)=0
int8_t get_priority() const
uint8_t get_channel() const
bool get_with_auth() const
WiFiScanResult(const bssid_t &bssid, const char *ssid, size_t ssid_len, uint8_t channel, int8_t rssi, bool with_auth, bool is_hidden)
const bssid_t & get_bssid() const
StringRef get_ssid() const
bool matches(const WiFiAP &config) const
void set_matches(bool matches)
void set_priority(int8_t priority)
bool get_is_hidden() const
bool operator==(const WiFiScanResult &rhs) const
Listener interface for WiFi scan results.
virtual void on_wifi_scan_results(const wifi_scan_vector_t< WiFiScanResult > &results)=0
std::array< IPAddress, 5 > IPAddresses
std::array< uint8_t, 6 > bssid_t
@ WIFI_MIN_AUTH_MODE_WPA2
@ WIFI_MIN_AUTH_MODE_WPA3
RetryHiddenMode
Controls how RETRY_HIDDEN phase selects networks to try.
@ BLIND_RETRY
Blind retry mode: scanning disabled (captive portal/improv active), try ALL configured networks seque...
@ SCAN_BASED
Normal mode: scan completed, only try networks NOT visible in scan results (truly hidden networks tha...
std::vector< T > wifi_scan_vector_t
struct esphome::wifi::SavedWifiSettings PACKED
@ ERROR_NETWORK_NOT_FOUND
WiFiRetryPhase
Tracks the current retry strategy/phase for WiFi connection attempts.
@ RETRY_HIDDEN
Retry networks not found in scan (might be hidden)
@ RESTARTING_ADAPTER
Restarting WiFi adapter to clear stuck state.
@ INITIAL_CONNECT
Initial connection attempt (varies based on fast_connect setting)
@ EXPLICIT_HIDDEN
Explicitly hidden networks (user marked as hidden, try before scanning)
@ FAST_CONNECT_CYCLING_APS
Fast connect mode: cycling through configured APs (config-only, no scan)
@ SCAN_CONNECTING
Scan-based: connecting to best AP from scan results.
WiFiComponent * global_wifi_component
RoamingState
Tracks post-connect roaming state machine.
@ SCANNING
Scanning for better AP.
@ IDLE
Not roaming, waiting for next check interval.
@ RECONNECTING
Roam connection failed, reconnecting to any available AP.
@ WIFI_COMPONENT_STATE_DISABLED
WiFi is disabled.
@ WIFI_COMPONENT_STATE_AP
WiFi is in AP-only mode and internal AP is already enabled.
@ WIFI_COMPONENT_STATE_STA_CONNECTING
WiFi is in STA(+AP) mode and currently connecting to an AP.
@ WIFI_COMPONENT_STATE_OFF
Nothing has been initialized yet.
@ WIFI_COMPONENT_STATE_STA_SCANNING
WiFi is in STA-only mode and currently scanning for APs.
@ WIFI_COMPONENT_STATE_COOLDOWN
WiFi is in cooldown mode because something went wrong, scanning will begin after a short period of ti...
@ WIFI_COMPONENT_STATE_STA_CONNECTED
WiFi is in STA(+AP) mode and successfully connected.
esp_eap_ttls_phase2_types ttls_phase_2
Struct for setting static IPs in WiFiComponent.
network::IPAddress static_ip
network::IPAddress dns1
The first DNS server. 0.0.0.0 for default.
network::IPAddress gateway
network::IPAddress dns2
The second DNS server. 0.0.0.0 for default.
network::IPAddress subnet