ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
wifi_component.h
Go to the documentation of this file.
1#pragma once
2
4#ifdef USE_WIFI
10
11#include <span>
12#include <string>
13#include <vector>
14
15#ifdef USE_ESP32_FRAMEWORK_ARDUINO
16#include <WiFi.h>
17#include <WiFiType.h>
18#include <esp_wifi.h>
19#endif
20
21#ifdef USE_LIBRETINY
22#include <WiFi.h>
23#endif
24
25#if defined(USE_ESP32) && defined(USE_WIFI_WPA2_EAP)
26#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
27#include <esp_eap_client.h>
28#else
29#include <esp_wpa2.h>
30#endif
31#endif
32
33#ifdef USE_ESP8266
34#include <ESP8266WiFi.h>
35#include <ESP8266WiFiType.h>
36
37#if defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE < VERSION_CODE(2, 4, 0)
38extern "C" {
39#include <user_interface.h>
40};
41#endif
42#endif
43
44#ifdef USE_RP2040
45extern "C" {
46#include "cyw43.h"
47#include "cyw43_country.h"
48#include "pico/cyw43_arch.h"
49}
50
51#include <WiFi.h>
52#endif
53
54#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
55#include <freertos/FreeRTOS.h>
56#include <freertos/semphr.h>
57#endif
58
59namespace esphome::wifi {
60
62static constexpr int8_t WIFI_RSSI_DISCONNECTED = -127;
63
65static constexpr size_t SSID_BUFFER_SIZE = 33;
66
68 char ssid[33];
69 char password[65];
70} PACKED; // NOLINT
71
73 uint8_t bssid[6];
74 uint8_t channel;
75 int8_t ap_index;
76} PACKED; // NOLINT
77
94
102
104enum class WiFiRetryPhase : uint8_t {
107#ifdef USE_WIFI_FAST_CONNECT
110#endif
119};
120
129
130#ifdef USE_WIFI_WPA2_EAP
131struct EAPAuth {
132 std::string identity; // required for all auth types
133 std::string username;
134 std::string password;
135 const char *ca_cert; // optionally verify authentication server
136 // used for EAP-TLS
137 const char *client_cert;
138 const char *client_key;
139// used for EAP-TTLS
140#ifdef USE_ESP32
141 esp_eap_ttls_phase2_types ttls_phase_2;
142#endif
143};
144#endif // USE_WIFI_WPA2_EAP
145
146using bssid_t = std::array<uint8_t, 6>;
147
148// Use std::vector for RP2040 since scan count is unknown (callback-based)
149// Use FixedVector for other platforms where count is queried first
150#ifdef USE_RP2040
151template<typename T> using wifi_scan_vector_t = std::vector<T>;
152#else
153template<typename T> using wifi_scan_vector_t = FixedVector<T>;
154#endif
155
156class WiFiAP {
157 public:
158 void set_ssid(const std::string &ssid);
159 void set_bssid(const bssid_t &bssid);
160 void clear_bssid();
161 void set_password(const std::string &password);
162#ifdef USE_WIFI_WPA2_EAP
163 void set_eap(optional<EAPAuth> eap_auth);
164#endif // USE_WIFI_WPA2_EAP
165 void set_channel(uint8_t channel);
166 void clear_channel();
168#ifdef USE_WIFI_MANUAL_IP
169 void set_manual_ip(optional<ManualIP> manual_ip);
170#endif
171 void set_hidden(bool hidden);
172 const std::string &get_ssid() const;
173 const bssid_t &get_bssid() const;
174 bool has_bssid() const;
175 const std::string &get_password() const;
176#ifdef USE_WIFI_WPA2_EAP
177 const optional<EAPAuth> &get_eap() const;
178#endif // USE_WIFI_WPA2_EAP
179 uint8_t get_channel() const;
180 bool has_channel() const;
181 int8_t get_priority() const { return priority_; }
182#ifdef USE_WIFI_MANUAL_IP
183 const optional<ManualIP> &get_manual_ip() const;
184#endif
185 bool get_hidden() const;
186
187 protected:
188 std::string ssid_;
189 std::string password_;
190#ifdef USE_WIFI_WPA2_EAP
192#endif // USE_WIFI_WPA2_EAP
193#ifdef USE_WIFI_MANUAL_IP
195#endif
196 // Group small types together to minimize padding
197 bssid_t bssid_{}; // 6 bytes, all zeros = any/not set
198 uint8_t channel_{0}; // 1 byte, 0 = auto/not set
199 int8_t priority_{0}; // 1 byte
200 bool hidden_{false}; // 1 byte (+ 3 bytes end padding to 4-byte align)
201};
202
204 public:
205 WiFiScanResult(const bssid_t &bssid, std::string ssid, uint8_t channel, int8_t rssi, bool with_auth, bool is_hidden);
206
207 bool matches(const WiFiAP &config) const;
208
209 bool get_matches() const;
210 void set_matches(bool matches);
211 const bssid_t &get_bssid() const;
212 const std::string &get_ssid() const;
213 uint8_t get_channel() const;
214 int8_t get_rssi() const;
215 bool get_with_auth() const;
216 bool get_is_hidden() const;
217 int8_t get_priority() const { return priority_; }
219
220 bool operator==(const WiFiScanResult &rhs) const;
221
222 protected:
224 uint8_t channel_;
225 int8_t rssi_;
226 std::string ssid_;
227 int8_t priority_{0};
228 bool matches_{false};
231};
232
237
243
249
250#ifdef USE_ESP32
251struct IDFWiFiEvent;
252#endif
253
260 public:
261 virtual void on_ip_state(const network::IPAddresses &ips, const network::IPAddress &dns1,
262 const network::IPAddress &dns2) = 0;
263};
264
271 public:
273};
274
281 public:
282 virtual void on_wifi_connect_state(StringRef ssid, std::span<const uint8_t, 6> bssid) = 0;
283};
284
291 public:
293};
294
296class WiFiComponent : public Component {
297 public:
300
301 void set_sta(const WiFiAP &ap);
302 // Returns a copy of the currently selected AP configuration
303 WiFiAP get_sta() const;
304 void init_sta(size_t count);
305 void add_sta(const WiFiAP &ap);
306 void clear_sta() {
307 this->sta_.clear();
308 this->selected_sta_index_ = -1;
309 }
310
311#ifdef USE_WIFI_AP
319 void set_ap(const WiFiAP &ap);
320 WiFiAP get_ap() { return this->ap_; }
321 void set_ap_timeout(uint32_t ap_timeout) { ap_timeout_ = ap_timeout; }
322#endif // USE_WIFI_AP
323
324 void enable();
325 void disable();
326 bool is_disabled();
327 void start_scanning();
329 void start_connecting(const WiFiAP &ap);
330 // Backward compatibility overload - ignores 'two' parameter
331 void start_connecting(const WiFiAP &ap, bool /* two */) { this->start_connecting(ap); }
332
334
335 void retry_connect();
336
337#ifdef USE_RP2040
338 bool can_proceed() override;
339#endif
340
341 void set_reboot_timeout(uint32_t reboot_timeout);
342
343 bool is_connected();
344
345 void set_power_save_mode(WiFiPowerSaveMode power_save);
346 void set_min_auth_mode(WifiMinAuthMode min_auth_mode) { min_auth_mode_ = min_auth_mode; }
347 void set_output_power(float output_power) { output_power_ = output_power; }
348
349 void set_passive_scan(bool passive);
350
351 void save_wifi_sta(const std::string &ssid, const std::string &password);
352
353 // ========== INTERNAL METHODS ==========
354 // (In most use cases you won't need these)
356 void setup() override;
357 void start();
358 void dump_config() override;
359 void restart_adapter();
361 float get_setup_priority() const override;
362 float get_loop_priority() const override;
363
365 void loop() override;
366
367 bool has_sta() const;
368 bool has_ap() const;
369 bool is_ap_active() const;
370
371#ifdef USE_WIFI_11KV_SUPPORT
372 void set_btm(bool btm);
373 void set_rrm(bool rrm);
374#endif
375
378 const char *get_use_address() const;
379 void set_use_address(const char *use_address);
380
382
384
385 bool has_sta_priority(const bssid_t &bssid) {
386 for (auto &it : this->sta_priorities_) {
387 if (it.bssid == bssid)
388 return true;
389 }
390 return false;
391 }
392 int8_t get_sta_priority(const bssid_t bssid) {
393 for (auto &it : this->sta_priorities_) {
394 if (it.bssid == bssid)
395 return it.priority;
396 }
397 return 0;
398 }
399 void set_sta_priority(const bssid_t bssid, int8_t priority) {
400 for (auto &it : this->sta_priorities_) {
401 if (it.bssid == bssid) {
402 it.priority = priority;
403 return;
404 }
405 }
406 this->sta_priorities_.push_back(WiFiSTAPriority{
407 .bssid = bssid,
408 .priority = priority,
409 });
410 }
411
413 std::string wifi_ssid();
416 const char *wifi_ssid_to(std::span<char, SSID_BUFFER_SIZE> buffer);
418
419 int8_t wifi_rssi();
420
421 void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
422 void set_keep_scan_results(bool keep_scan_results) { this->keep_scan_results_ = keep_scan_results; }
423
426
427 int32_t get_wifi_channel();
428
429#ifdef USE_WIFI_LISTENERS
433 void add_ip_state_listener(WiFiIPStateListener *listener) { this->ip_state_listeners_.push_back(listener); }
436 this->scan_results_listeners_.push_back(listener);
437 }
442 this->connect_state_listeners_.push_back(listener);
443 }
447 void add_power_save_listener(WiFiPowerSaveListener *listener) { this->power_save_listeners_.push_back(listener); }
448#endif // USE_WIFI_LISTENERS
449
450#ifdef USE_WIFI_RUNTIME_POWER_SAVE
466
479#endif // USE_WIFI_RUNTIME_POWER_SAVE
480
481 protected:
482#ifdef USE_WIFI_AP
483 void setup_ap_config_();
484#endif // USE_WIFI_AP
485
488
493 bool transition_to_phase_(WiFiRetryPhase new_phase);
496 bool needs_scan_results_() const;
502 int8_t find_first_non_hidden_index_() const;
505 bool ssid_was_seen_in_scan_(const std::string &ssid) const;
509 int8_t find_next_hidden_sta_(int8_t start_index);
519 const WiFiAP *get_selected_sta_() const {
520 if (this->selected_sta_index_ >= 0 && static_cast<size_t>(this->selected_sta_index_) < this->sta_.size()) {
521 return &this->sta_[this->selected_sta_index_];
522 }
523 return nullptr;
524 }
525
527 if (this->selected_sta_index_ < 0 || static_cast<size_t>(this->selected_sta_index_) >= this->sta_.size()) {
528 this->selected_sta_index_ = this->sta_.empty() ? -1 : 0;
529 }
530 }
531
532 bool all_networks_hidden_() const {
533 if (this->sta_.empty())
534 return false;
535 for (const auto &ap : this->sta_) {
536 if (!ap.get_hidden())
537 return false;
538 }
539 return true;
540 }
541
542 void connect_soon_();
543
544 void wifi_loop_();
546 bool wifi_sta_pre_setup_();
547 bool wifi_apply_output_power_(float output_power);
549 bool wifi_sta_ip_config_(const optional<ManualIP> &manual_ip);
551 bool wifi_sta_connect_(const WiFiAP &ap);
552 void wifi_pre_setup_();
554 bool wifi_scan_start_(bool passive);
555
556#ifdef USE_WIFI_AP
557 bool wifi_ap_ip_config_(const optional<ManualIP> &manual_ip);
558 bool wifi_start_ap_(const WiFiAP &ap);
559#endif // USE_WIFI_AP
560
561 bool wifi_disconnect_();
562
566
569
570#ifdef USE_WIFI_FAST_CONNECT
573#endif
574
575#ifdef USE_ESP8266
576 static void wifi_event_callback(System_Event_t *event);
577 void wifi_scan_done_callback_(void *arg, STATUS status);
578 static void s_wifi_scan_done_callback(void *arg, STATUS status);
579#endif
580
581#ifdef USE_ESP32_FRAMEWORK_ARDUINO
582 void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info);
584#endif
585#ifdef USE_ESP32
586 void wifi_process_event_(IDFWiFiEvent *data);
587#endif
588
589#ifdef USE_RP2040
590 static int s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result);
591 void wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result);
592#endif
593
594#ifdef USE_LIBRETINY
595 void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info);
597#endif
598
600 std::vector<WiFiSTAPriority> sta_priorities_;
602#ifdef USE_WIFI_AP
604#endif
605 float output_power_{NAN};
606#ifdef USE_WIFI_LISTENERS
607 std::vector<WiFiIPStateListener *> ip_state_listeners_;
608 std::vector<WiFiScanResultsListener *> scan_results_listeners_;
609 std::vector<WiFiConnectStateListener *> connect_state_listeners_;
610 std::vector<WiFiPowerSaveListener *> power_save_listeners_;
611#endif // USE_WIFI_LISTENERS
613#ifdef USE_WIFI_FAST_CONNECT
615#endif
616
617 // Group all 32-bit integers together
619 uint32_t last_connected_{0};
620 uint32_t reboot_timeout_{};
621#ifdef USE_WIFI_AP
622 uint32_t ap_timeout_{};
623#endif
624
625 // Group all 8-bit values together
630 uint8_t num_retried_{0};
631 // Index into sta_ array for the currently selected AP configuration (-1 = none selected)
632 // Used to access password, manual_ip, priority, EAP settings, and hidden flag
633 // int8_t limits to 127 APs (enforced in __init__.py via MAX_WIFI_NETWORKS)
635
636#if USE_NETWORK_IPV6
638#endif /* USE_NETWORK_IPV6 */
639
640 // Group all boolean values together
641 bool has_ap_{false};
644 bool scan_done_{false};
645 bool ap_setup_{false};
646 bool ap_started_{false};
647 bool passive_scan_{false};
649#ifdef USE_WIFI_11KV_SUPPORT
650 bool btm_{false};
651 bool rrm_{false};
652#endif
653 bool enable_on_boot_{true};
654 bool got_ipv4_address_{false};
658#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
661
662 SemaphoreHandle_t high_performance_semaphore_{nullptr};
663#endif
664
665 // Pointers at the end (naturally aligned)
668
669 private:
670 // Stores a pointer to a string literal (static storage duration).
671 // ONLY set from Python-generated code with string literals - never dynamic strings.
672 const char *use_address_{""};
673};
674
675extern WiFiComponent *global_wifi_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
676
677} // namespace esphome::wifi
678#endif
BedjetMode mode
BedJet operating mode.
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:184
StringRef is a reference to a string owned by something else.
Definition string_ref.h:22
uint8_t get_channel() const
const std::string & get_ssid() const
void set_ssid(const std::string &ssid)
const optional< EAPAuth > & get_eap() const
const std::string & get_password() const
void set_bssid(const bssid_t &bssid)
void set_channel(uint8_t channel)
optional< EAPAuth > eap_
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)
This component is responsible for managing the ESP WiFi interface.
Trigger * get_connect_trigger() const
void add_sta(const WiFiAP &ap)
bool load_fast_connect_settings_(WiFiAP &params)
void add_connect_state_listener(WiFiConnectStateListener *listener)
Add a listener for WiFi connection state changes.
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.
void set_sta(const WiFiAP &ap)
std::vector< WiFiIPStateListener * > ip_state_listeners_
bool has_sta_priority(const bssid_t &bssid)
const WiFiAP * get_selected_sta_() const
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.
std::vector< WiFiScanResultsListener * > scan_results_listeners_
void save_wifi_sta(const std::string &ssid, const std::string &password)
wifi_scan_vector_t< WiFiScanResult > scan_result_
WiFiPowerSaveMode configured_power_save_
void set_sta_priority(const bssid_t bssid, int8_t priority)
void loop() override
Reconnect WiFi if required.
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.
bool wifi_sta_ip_config_(const optional< ManualIP > &manual_ip)
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)
static void wifi_event_callback(System_Event_t *event)
WiFiComponent()
Construct a WiFiComponent.
void wifi_process_event_(IDFWiFiEvent *data)
std::vector< WiFiSTAPriority > sta_priorities_
void set_min_auth_mode(WifiMinAuthMode min_auth_mode)
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)
void set_passive_scan(bool passive)
void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info)
static void s_wifi_scan_done_callback(void *arg, STATUS status)
void set_power_save_mode(WiFiPowerSaveMode power_save)
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
network::IPAddresses get_ip_addresses()
float get_setup_priority() const override
WIFI setup_priority.
void set_output_power(float output_power)
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...
bool ssid_was_seen_in_scan_(const std::string &ssid) const
Check if an SSID was seen in the most recent scan results Used to skip hidden mode for SSIDs we know ...
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.
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),...
Trigger * get_disconnect_trigger() const
void set_ap_timeout(uint32_t ap_timeout)
bool release_high_performance()
Release a high-performance mode request.
bool wifi_apply_output_power_(float output_power)
const char * get_use_address() const
WiFiSTAConnectStatus wifi_sta_connect_status_()
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)
network::IPAddresses wifi_sta_ip_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.
void setup() override
Setup WiFi interface.
void set_use_address(const char *use_address)
std::vector< WiFiConnectStateListener * > connect_state_listeners_
std::vector< WiFiPowerSaveListener * > power_save_listeners_
const wifi_scan_vector_t< WiFiScanResult > & get_scan_result() const
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
const std::string & get_ssid() const
const bssid_t & get_bssid() const
WiFiScanResult(const bssid_t &bssid, std::string ssid, uint8_t channel, int8_t rssi, bool with_auth, bool is_hidden)
bool matches(const WiFiAP &config) const
void set_priority(int8_t priority)
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
uint8_t priority
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:158
std::array< uint8_t, 6 > bssid_t
std::vector< T > wifi_scan_vector_t
struct esphome::wifi::SavedWifiSettings PACKED
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
@ 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