13 #ifdef USE_ESP32_FRAMEWORK_ARDUINO 23 #if defined(USE_ESP_IDF) && defined(USE_WIFI_WPA2_EAP) 24 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 25 #include <esp_eap_client.h> 32 #include <ESP8266WiFi.h> 33 #include <ESP8266WiFiType.h> 35 #if defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE < VERSION_CODE(2, 4, 0) 37 #include <user_interface.h> 45 #include "cyw43_country.h" 46 #include "pico/cyw43_arch.h" 105 #ifdef USE_WIFI_WPA2_EAP 119 #endif // USE_WIFI_WPA2_EAP 125 void set_ssid(
const std::string &
ssid);
128 void set_password(
const std::string &
password);
129 #ifdef USE_WIFI_WPA2_EAP 131 #endif // USE_WIFI_WPA2_EAP 135 void set_hidden(
bool hidden);
136 const std::string &get_ssid()
const;
138 const std::string &get_password()
const;
139 #ifdef USE_WIFI_WPA2_EAP 141 #endif // USE_WIFI_WPA2_EAP 145 bool get_hidden()
const;
151 #ifdef USE_WIFI_WPA2_EAP 153 #endif // USE_WIFI_WPA2_EAP 164 bool matches(
const WiFiAP &config);
166 bool get_matches()
const;
167 void set_matches(
bool matches);
168 const bssid_t &get_bssid()
const;
169 const std::string &get_ssid()
const;
170 uint8_t get_channel()
const;
171 int8_t get_rssi()
const;
172 bool get_with_auth()
const;
173 bool get_is_hidden()
const;
180 bool matches_{
false};
187 float priority_{0.0f};
211 void set_sta(
const WiFiAP &ap);
213 void add_sta(
const WiFiAP &ap);
224 void set_ap(
const WiFiAP &ap);
226 #endif // USE_WIFI_AP 231 void start_scanning();
232 void check_scanning_finished();
233 void start_connecting(
const WiFiAP &ap,
bool two);
234 void set_fast_connect(
bool fast_connect);
237 void check_connecting_finished();
239 void retry_connect();
241 bool can_proceed()
override;
243 void set_reboot_timeout(uint32_t reboot_timeout);
250 void set_passive_scan(
bool passive);
252 void save_wifi_sta(
const std::string &
ssid,
const std::string &
password);
256 void setup()
override;
258 void dump_config()
override;
260 float get_setup_priority()
const override;
261 float get_loop_priority()
const override;
264 void loop()
override;
266 bool has_sta()
const;
269 #ifdef USE_WIFI_11KV_SUPPORT 270 void set_btm(
bool btm);
271 void set_rrm(
bool rrm);
277 void set_use_address(
const std::string &use_address);
284 for (
auto &it : this->sta_priorities_) {
285 if (it.bssid == bssid)
291 for (
auto &it : this->sta_priorities_) {
292 if (it.bssid == bssid)
298 for (
auto &it : this->sta_priorities_) {
299 if (it.bssid == bssid) {
311 std::string wifi_ssid();
321 int32_t get_wifi_channel();
324 static std::string format_mac_addr(
const uint8_t mac[6]);
327 void setup_ap_config_();
328 #endif // USE_WIFI_AP 330 void print_connect_params_();
334 bool wifi_sta_pre_setup_();
335 bool wifi_apply_output_power_(
float output_power);
336 bool wifi_apply_power_save_();
338 bool wifi_apply_hostname_();
339 bool wifi_sta_connect_(
const WiFiAP &ap);
340 void wifi_pre_setup_();
342 bool wifi_scan_start_(
bool passive);
346 bool wifi_start_ap_(
const WiFiAP &ap);
347 #endif // USE_WIFI_AP 349 bool wifi_disconnect_();
355 bool is_captive_portal_active_();
356 bool is_esp32_improv_active_();
358 void load_fast_connect_settings_();
359 void save_fast_connect_settings_();
362 static void wifi_event_callback(System_Event_t *event);
363 void wifi_scan_done_callback_(
void *arg,
STATUS status);
364 static void s_wifi_scan_done_callback(
void *arg,
STATUS status);
367 #ifdef USE_ESP32_FRAMEWORK_ARDUINO 368 void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info);
369 void wifi_scan_done_callback_();
372 void wifi_process_event_(IDFWiFiEvent *data);
376 static int s_wifi_scan_result(
void *env,
const cyw43_ev_scan_result_t *result);
377 void wifi_scan_result(
void *env,
const cyw43_ev_scan_result_t *result);
381 void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info);
382 void wifi_scan_done_callback_();
389 bool fast_connect_{
false};
390 bool retry_hidden_{
false};
395 bool handled_connected_state_{
false};
397 uint8_t num_retried_{0};
398 uint32_t last_connected_{0};
399 uint32_t reboot_timeout_{};
400 uint32_t ap_timeout_{};
402 bool error_from_callback_{
false};
404 bool scan_done_{
false};
405 bool ap_setup_{
false};
407 bool passive_scan_{
false};
410 bool has_saved_wifi_settings_{
false};
411 #ifdef USE_WIFI_11KV_SUPPORT 416 bool got_ipv4_address_{
false};
418 uint8_t num_ipv6_addresses_{0};
449 TEMPLATABLE_VALUE(std::string,
ssid)
450 TEMPLATABLE_VALUE(std::string,
password)
451 TEMPLATABLE_VALUE(
bool, save)
452 TEMPLATABLE_VALUE(uint32_t, connection_timeout)
454 void play(Ts...
x)
override {
455 auto ssid = this->ssid_.value(
x...);
456 auto password = this->password_.value(
x...);
458 if (this->connecting_)
463 this->connect_trigger_->trigger();
467 this->new_sta_.set_ssid(
ssid);
468 this->new_sta_.set_password(
password);
470 this->old_sta_ = global_wifi_component->
get_sta();
472 global_wifi_component->
disable();
474 this->connecting_ =
true;
478 if (this->save_.value(
x...)) {
479 global_wifi_component->
save_wifi_sta(new_sta_.get_ssid(), new_sta_.get_password());
481 global_wifi_component->
set_sta(new_sta_);
484 global_wifi_component->
enable();
486 this->set_timeout(
"wifi-connect-timeout", this->connection_timeout_.value(
x...), [
this]() {
487 this->connecting_ =
false;
489 global_wifi_component->
disable();
490 global_wifi_component->
save_wifi_sta(old_sta_.get_ssid(), old_sta_.get_password());
491 global_wifi_component->
enable();
493 this->error_trigger_->trigger();
497 Trigger<> *get_connect_trigger()
const {
return this->connect_trigger_; }
498 Trigger<> *get_error_trigger()
const {
return this->error_trigger_; }
500 void loop()
override {
501 if (!this->connecting_)
505 this->cancel_timeout(
"wifi-connect-timeout");
506 this->connecting_ =
false;
507 if (global_wifi_component->
wifi_ssid() == this->new_sta_.get_ssid()) {
509 this->connect_trigger_->trigger();
512 this->error_trigger_->trigger();
518 bool connecting_{
false};
Nothing has been initialized yet.
This component is responsible for managing the ESP WiFi interface.
void set_enable_on_boot(bool enable_on_boot)
void set_priority(float priority)
std::array< uint8_t, 6 > bssid_t
std::string get_use_address()
Get the active network hostname.
float get_priority() const
void save_wifi_sta(const std::string &ssid, const std::string &password)
void set_output_power(float output_power)
Trigger * get_disconnect_trigger() const
const std::vector< WiFiScanResult > & get_scan_result() const
void set_sta_priority(const bssid_t bssid, float priority)
optional< ManualIP > manual_ip_
WiFi is in STA(+AP) mode and currently connecting to an AP a second time.
void play(Ts... x) override
WiFi is in STA(+AP) mode and successfully connected.
bool check(Ts... x) override
void set_priority(float priority)
network::IPAddress static_ip
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
network::IPAddress gateway
void set_ap_timeout(uint32_t ap_timeout)
std::vector< WiFiScanResult > scan_result_
WiFi is in STA-only mode and currently scanning for APs.
network::IPAddresses get_ip_addresses()
Struct for setting static IPs in WiFiComponent.
network::IPAddress dns1
The first DNS server. 0.0.0.0 for default.
Base class for all automation conditions.
WiFi is in STA(+AP) mode and currently connecting to an AP.
bool has_sta_priority(const bssid_t &bssid)
WiFi is in cooldown mode because something went wrong, scanning will begin after a short period of ti...
optional< bssid_t > bssid_
esp_eap_ttls_phase2_types ttls_phase_2
Trigger * get_connect_trigger() const
WiFiComponent * global_wifi_component
std::array< IPAddress, 5 > IPAddresses
optional< uint8_t > channel_
float get_priority() const
ESPPreferenceObject pref_
bool is_disabled()
Return whether the network is disabled (only wifi for now)
network::IPAddress dns2
The second DNS server. 0.0.0.0 for default.
struct esphome::wifi::SavedWifiSettings PACKED
std::vector< WiFiSTAPriority > sta_priorities_
bool operator==(optional< T > const &x, optional< U > const &y)
std::vector< WiFiAP > sta_
optional< float > output_power_
network::IPAddress subnet
void set_sta(const WiFiAP &ap)
Implementation of SPI Controller mode.
ESPPreferenceObject fast_connect_pref_
bool check(Ts... x) override
void play(Ts... x) override
float get_sta_priority(const bssid_t bssid)
WiFi is in AP-only mode and internal AP is already enabled.