6 #if (ESP_IDF_VERSION_MAJOR >= 5 && ESP_IDF_VERSION_MINOR >= 1) 7 #include <esp_eap_client.h> 13 #if defined(USE_ESP32) || defined(USE_ESP_IDF) 17 #include <user_interface.h> 31 #ifdef USE_CAPTIVE_PORTAL 42 static const char *
const TAG =
"wifi";
47 ESP_LOGCONFIG(TAG,
"Setting up WiFi...");
60 ESP_LOGCONFIG(TAG,
"Starting WiFi...");
72 if (this->pref_.load(&save)) {
73 ESP_LOGD(TAG,
"Loaded saved wifi settings: %s", save.ssid);
77 sta.set_password(save.password);
84 ESP_LOGV(TAG,
"Setting Output Power Option failed!");
88 ESP_LOGV(TAG,
"Setting Power Save Option failed!");
99 }
else if (this->
has_ap()) {
102 ESP_LOGV(TAG,
"Setting Output Power Option failed!");
104 #ifdef USE_CAPTIVE_PORTAL 111 #endif // USE_WIFI_AP 124 const uint32_t now =
millis();
162 ESP_LOGW(TAG,
"WiFi Connection lost... Reconnecting...");
181 ESP_LOGI(TAG,
"Starting fallback AP!");
183 #ifdef USE_CAPTIVE_PORTAL 189 #endif // USE_WIFI_AP 203 ESP_LOGE(TAG,
"Can't connect to WiFi, rebooting...");
215 #ifdef USE_WIFI_11KV_SUPPORT 226 #endif // USE_WIFI_AP 252 if (name.length() > 32) {
254 name.erase(name.begin() + 25, name.end() - 7);
256 name = name.substr(0, 32);
262 ESP_LOGCONFIG(TAG,
"Setting up AP...");
264 ESP_LOGCONFIG(TAG,
" AP SSID: '%s'", this->
ap_.
get_ssid().c_str());
265 ESP_LOGCONFIG(TAG,
" AP Password: '%s'", this->
ap_.
get_password().c_str());
268 ESP_LOGCONFIG(TAG,
" AP Static IP: '%s'", manual.static_ip.str().c_str());
269 ESP_LOGCONFIG(TAG,
" AP Gateway: '%s'", manual.gateway.str().c_str());
270 ESP_LOGCONFIG(TAG,
" AP Subnet: '%s'", manual.subnet.str().c_str());
274 ESP_LOGCONFIG(TAG,
" IP Address: %s", this->
wifi_soft_ap_ip().str().c_str());
285 #endif // USE_WIFI_AP 299 strncpy(save.ssid, ssid.c_str(),
sizeof(save.ssid));
300 strncpy(save.password, password.c_str(),
sizeof(save.password));
307 sta.set_password(password);
312 ESP_LOGI(TAG,
"WiFi Connecting to '%s'...", ap.
get_ssid().c_str());
313 #ifdef ESPHOME_LOG_HAS_VERBOSE 314 ESP_LOGV(TAG,
"Connection Params:");
315 ESP_LOGV(TAG,
" SSID: '%s'", ap.
get_ssid().c_str());
318 ESP_LOGV(TAG,
" BSSID: %02X:%02X:%02X:%02X:%02X:%02X", b[0], b[1], b[2], b[3], b[4], b[5]);
320 ESP_LOGV(TAG,
" BSSID: Not Set");
323 #ifdef USE_WIFI_WPA2_EAP 324 if (ap.
get_eap().has_value()) {
325 ESP_LOGV(TAG,
" WPA2 Enterprise authentication configured:");
327 ESP_LOGV(TAG,
" Identity: " LOG_SECRET(
"'%s'"), eap_config.
identity.c_str());
328 ESP_LOGV(TAG,
" Username: " LOG_SECRET(
"'%s'"), eap_config.
username.c_str());
329 ESP_LOGV(TAG,
" Password: " LOG_SECRET(
"'%s'"), eap_config.
password.c_str());
331 #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE 332 std::map<esp_eap_ttls_phase2_types, std::string> phase2types = {{ESP_EAP_TTLS_PHASE2_PAP,
"pap"},
333 {ESP_EAP_TTLS_PHASE2_CHAP,
"chap"},
334 {ESP_EAP_TTLS_PHASE2_MSCHAP,
"mschap"},
335 {ESP_EAP_TTLS_PHASE2_MSCHAPV2,
"mschapv2"},
336 {ESP_EAP_TTLS_PHASE2_EAP,
"eap"}};
337 ESP_LOGV(TAG,
" TTLS Phase 2: " LOG_SECRET(
"'%s'"), phase2types[eap_config.
ttls_phase_2].c_str());
340 bool ca_cert_present = eap_config.
ca_cert !=
nullptr && strlen(eap_config.
ca_cert);
343 ESP_LOGV(TAG,
" CA Cert: %s", ca_cert_present ?
"present" :
"not present");
344 ESP_LOGV(TAG,
" Client Cert: %s", client_cert_present ?
"present" :
"not present");
345 ESP_LOGV(TAG,
" Client Key: %s", client_key_present ?
"present" :
"not present");
348 ESP_LOGV(TAG,
" Password: " LOG_SECRET(
"'%s'"), ap.
get_password().c_str());
349 #ifdef USE_WIFI_WPA2_EAP 355 ESP_LOGV(TAG,
" Channel: Not Set");
359 ESP_LOGV(TAG,
" Manual IP: Static IP=%s Gateway=%s Subnet=%s DNS1=%s DNS2=%s", m.
static_ip.
str().c_str(),
362 ESP_LOGV(TAG,
" Using DHCP IP");
364 ESP_LOGV(TAG,
" Hidden: %s", YESNO(ap.
get_hidden()));
368 ESP_LOGE(TAG,
"wifi_sta_connect_ failed!");
391 return LOG_STR(
"\033[0;32m" 397 }
else if (rssi >= -65) {
398 return LOG_STR(
"\033[0;33m" 405 }
else if (rssi >= -85) {
406 return LOG_STR(
"\033[0;33m" 414 return LOG_STR(
"\033[0;31m" 429 ESP_LOGCONFIG(TAG,
" WiFi is disabled!");
432 ESP_LOGCONFIG(TAG,
" SSID: " LOG_SECRET(
"'%s'"),
wifi_ssid().c_str());
435 ESP_LOGCONFIG(TAG,
" IP Address: %s", ip.str().c_str());
438 ESP_LOGCONFIG(TAG,
" BSSID: " LOG_SECRET(
"%02X:%02X:%02X:%02X:%02X:%02X"), bssid[0], bssid[1], bssid[2], bssid[3],
440 ESP_LOGCONFIG(TAG,
" Hostname: '%s'",
App.
get_name().c_str());
442 ESP_LOGCONFIG(TAG,
" Signal strength: %d dB %s", rssi, LOG_STR_ARG(
get_signal_bars(rssi)));
449 ESP_LOGCONFIG(TAG,
" DNS1: %s",
wifi_dns_ip_(0).str().c_str());
450 ESP_LOGCONFIG(TAG,
" DNS2: %s",
wifi_dns_ip_(1).str().c_str());
451 #ifdef USE_WIFI_11KV_SUPPORT 452 ESP_LOGCONFIG(TAG,
" BTM: %s", this->
btm_ ?
"enabled" :
"disabled");
453 ESP_LOGCONFIG(TAG,
" RRM: %s", this->
rrm_ ?
"enabled" :
"disabled");
461 ESP_LOGD(TAG,
"Enabling WIFI...");
471 ESP_LOGD(TAG,
"Disabling WIFI...");
481 ESP_LOGD(TAG,
"Starting scan...");
489 ESP_LOGE(TAG,
"Scan timeout!");
496 ESP_LOGD(TAG,
"Found networks:");
498 ESP_LOGD(TAG,
" No network found!");
504 for (
auto &ap : this->
sta_) {
505 if (res.matches(ap)) {
506 res.set_matches(
true);
516 std::stable_sort(this->scan_result_.begin(), this->scan_result_.end(),
533 for (
auto &res : this->scan_result_) {
535 auto bssid = res.get_bssid();
536 sprintf(bssid_s,
"%02X:%02X:%02X:%02X:%02X:%02X", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
538 if (res.get_matches()) {
539 ESP_LOGI(TAG,
"- '%s' %s" LOG_SECRET(
"(%s) ")
"%s", res.get_ssid().c_str(),
540 res.get_is_hidden() ?
"(HIDDEN) " :
"", bssid_s, LOG_STR_ARG(
get_signal_bars(res.get_rssi())));
541 ESP_LOGD(TAG,
" Channel: %u", res.get_channel());
542 ESP_LOGD(TAG,
" RSSI: %d dB", res.get_rssi());
544 ESP_LOGD(TAG,
"- " LOG_SECRET(
"'%s'")
" " LOG_SECRET(
"(%s) ")
"%s", res.get_ssid().c_str(), bssid_s,
549 if (!this->scan_result_[0].get_matches()) {
550 ESP_LOGW(TAG,
"No matching network found!");
557 for (
auto &config : this->
sta_) {
559 if (!scan_res.
matches(config)) {
563 if (config.get_hidden()) {
566 connect_params.
set_ssid(config.get_ssid());
581 #ifdef USE_WIFI_WPA2_EAP 583 connect_params.
set_eap(config.get_eap());
599 ESP_LOGCONFIG(TAG,
"WiFi:");
608 ESP_LOGW(TAG,
"Incomplete connection.");
616 ESP_LOGI(TAG,
"WiFi Connected!");
620 #ifdef USE_CAPTIVE_PORTAL 625 ESP_LOGD(TAG,
"Disabling AP...");
646 ESP_LOGW(TAG,
"Timeout while connecting to WiFi.");
652 ESP_LOGW(TAG,
"Error while connecting to network.");
662 ESP_LOGW(TAG,
"WiFi network can not be found anymore.");
668 ESP_LOGW(TAG,
"Connecting to WiFi network failed. Are the credentials wrong?");
673 ESP_LOGW(TAG,
"WiFi Unknown connection status %d", (
int)
status);
689 ESP_LOGW(TAG,
"Restarting WiFi adapter...");
696 ESP_LOGD(TAG,
"Retrying with hidden networks...");
732 sprintf(buf,
"%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
736 #ifdef USE_CAPTIVE_PORTAL 755 std::copy(fast_connect_save.bssid, fast_connect_save.bssid + 6, bssid.begin());
759 ESP_LOGD(TAG,
"Loaded saved fast_connect wifi settings");
770 memcpy(fast_connect_save.bssid, bssid.data(), 6);
771 fast_connect_save.
channel = channel;
775 ESP_LOGD(TAG,
"Saved fast_connect wifi settings");
783 #ifdef USE_WIFI_WPA2_EAP 792 #ifdef USE_WIFI_WPA2_EAP 802 ssid_(
std::move(ssid)),
805 with_auth_(with_auth),
806 is_hidden_(is_hidden) {}
813 }
else if (!config.
get_ssid().empty()) {
824 #ifdef USE_WIFI_WPA2_EAP
Nothing has been initialized yet.
This component is responsible for managing the ESP WiFi interface.
WiFiSTAConnectStatus wifi_sta_connect_status_()
const std::string & get_ssid() const
void check_scanning_finished()
std::array< uint8_t, 6 > bssid_t
const optional< EAPAuth > & get_eap() const
static std::string format_mac_addr(const uint8_t mac[6])
network::IPAddresses get_ip_addresses()
const std::string & get_password() const
WiFiPowerSaveMode power_save_
void status_set_warning(const char *message="unspecified")
WiFiComponentState state_
bool get_is_hidden() const
void save_wifi_sta(const std::string &ssid, const std::string &password)
network::IPAddress wifi_dns_ip_(int num)
network::IPAddresses wifi_sta_ip_addresses()
void set_sta_priority(const bssid_t bssid, float priority)
bool wifi_mode_(optional< bool > sta, optional< bool > ap)
const optional< bssid_t > & get_bssid() const
bool wifi_apply_output_power_(float output_power)
void add_sta(const WiFiAP &ap)
uint8_t get_channel() const
WiFi is in STA(+AP) mode and currently connecting to an AP a second time.
void print_connect_params_()
WiFi is in STA(+AP) mode and successfully connected.
network::IPAddress wifi_subnet_mask_()
network::IPAddress static_ip
bool get_with_auth() const
void set_ap(const WiFiAP &ap)
Setup an Access Point that should be created if no connection to a station can be made...
uint32_t IRAM_ATTR HOT millis()
const bssid_t & get_bssid() const
void set_channel(optional< uint8_t > channel)
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
network::IPAddress gateway
bool is_esp32_improv_active_()
bool wifi_apply_hostname_()
void start_connecting(const WiFiAP &ap, bool two)
bool error_from_callback_
WiFiComponent()
Construct a WiFiComponent.
CaptivePortal * global_captive_portal
void set_passive_scan(bool passive)
std::vector< WiFiScanResult > scan_result_
WiFi is in STA-only mode and currently scanning for APs.
Struct for setting static IPs in WiFiComponent.
void set_power_save_mode(WiFiPowerSaveMode power_save)
bool wifi_start_ap_(const WiFiAP &ap)
network::IPAddress dns1
The first DNS server. 0.0.0.0 for default.
WiFi is in STA(+AP) mode and currently connecting to an AP.
bool has_sta_priority(const bssid_t &bssid)
const optional< ManualIP > & get_manual_ip() const
ESPPreferences * global_preferences
const optional< uint8_t > & get_channel() const
void status_clear_warning()
WiFi is in cooldown mode because something went wrong, scanning will begin after a short period of ti...
esp_eap_ttls_phase2_types ttls_phase_2
void set_ssid(const std::string &ssid)
bool wifi_apply_power_save_()
void check_connecting_finished()
Application App
Global storage of Application pointer - only one Application can exist.
bool matches(const WiFiAP &config)
WiFiComponent * global_wifi_component
const std::string & get_name() const
Get the name of this Application set by pre_setup().
network::IPAddress get_dns_address(int num)
void save_fast_connect_settings_()
void set_reboot_timeout(uint32_t reboot_timeout)
std::array< IPAddress, 5 > IPAddresses
bool is_captive_portal_active_()
void set_matches(bool matches)
bool is_name_add_mac_suffix_enabled() const
network::IPAddress wifi_soft_ap_ip()
ESP32ImprovComponent * global_improv_component
void loop() override
Reconnect WiFi if required.
bool can_proceed() override
bool operator==(const WiFiScanResult &rhs) const
float get_priority() const
ESPPreferenceObject pref_
bool wifi_sta_pre_setup_()
network::IPAddress dns2
The second DNS server. 0.0.0.0 for default.
Trigger * disconnect_trigger_
void IRAM_ATTR HOT yield()
void set_fast_connect(bool fast_connect)
void set_manual_ip(optional< ManualIP > manual_ip)
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
uint32_t fnv1_hash(const std::string &str)
Calculate a FNV-1 hash of str.
std::vector< WiFiAP > sta_
bool wifi_scan_start_(bool passive)
network::IPAddress wifi_gateway_ip_()
optional< float > output_power_
network::IPAddress subnet
void set_sta(const WiFiAP &ap)
bool handled_connected_state_
Implementation of SPI Controller mode.
void set_bssid(bssid_t bssid)
const std::string & get_ssid() const
void load_fast_connect_settings_()
void set_eap(optional< EAPAuth > eap_auth)
void set_hidden(bool hidden)
ESPPreferenceObject fast_connect_pref_
float get_loop_priority() const override
void set_use_address(const std::string &use_address)
WiFiScanResult(const bssid_t &bssid, std::string ssid, uint8_t channel, int8_t rssi, bool with_auth, bool is_hidden)
std::string get_mac_address_pretty()
Get the device MAC address as a string, in colon-separated uppercase hex notation.
virtual bool sync()=0
Commit pending writes to flash.
std::string get_use_address() const
std::string get_compilation_time() const
Trigger * connect_trigger_
float get_sta_priority(const bssid_t bssid)
void set_password(const std::string &password)
bool wifi_sta_connect_(const WiFiAP &ap)
void setup() override
Setup WiFi interface.
float get_setup_priority() const override
WIFI setup_priority.
WiFi is in AP-only mode and internal AP is already enabled.
void IRAM_ATTR HOT delay(uint32_t ms)
const LogString * get_signal_bars(int8_t rssi)
void dump_config() override