ESPHome  2023.8.3
wifi_component.cpp
Go to the documentation of this file.
1 #include "wifi_component.h"
2 #include <cinttypes>
3 
4 #if defined(USE_ESP32) || defined(USE_ESP_IDF)
5 #include <esp_wifi.h>
6 #endif
7 #ifdef USE_ESP8266
8 #include <user_interface.h>
9 #endif
10 
11 #include <utility>
12 #include <algorithm>
13 #include "lwip/err.h"
14 #include "lwip/dns.h"
15 
16 #include "esphome/core/helpers.h"
17 #include "esphome/core/log.h"
18 #include "esphome/core/hal.h"
19 #include "esphome/core/util.h"
21 
22 #ifdef USE_CAPTIVE_PORTAL
24 #endif
25 
26 #ifdef USE_IMPROV
28 #endif
29 
30 namespace esphome {
31 namespace wifi {
32 
33 static const char *const TAG = "wifi";
34 
36 
38  ESP_LOGCONFIG(TAG, "Setting up WiFi...");
39  this->wifi_pre_setup_();
40  if (this->enable_on_boot_) {
41  this->start();
42  } else {
44  }
45 }
46 
48  ESP_LOGCONFIG(TAG, "Starting WiFi...");
49  ESP_LOGCONFIG(TAG, " Local MAC: %s", get_mac_address_pretty().c_str());
50  this->last_connected_ = millis();
51 
52  uint32_t hash = this->has_sta() ? fnv1_hash(App.get_compilation_time()) : 88491487UL;
53 
55 
56  SavedWifiSettings save{};
57  if (this->pref_.load(&save)) {
58  ESP_LOGD(TAG, "Loaded saved wifi settings: %s", save.ssid);
59 
60  WiFiAP sta{};
61  sta.set_ssid(save.ssid);
62  sta.set_password(save.password);
63  this->set_sta(sta);
64  }
65 
66  if (this->has_sta()) {
67  this->wifi_sta_pre_setup_();
68  if (this->output_power_.has_value() && !this->wifi_apply_output_power_(*this->output_power_)) {
69  ESP_LOGV(TAG, "Setting Output Power Option failed!");
70  }
71 
72  if (!this->wifi_apply_power_save_()) {
73  ESP_LOGV(TAG, "Setting Power Save Option failed!");
74  }
75 
76  if (this->fast_connect_) {
77  this->selected_ap_ = this->sta_[0];
78  this->start_connecting(this->selected_ap_, false);
79  } else {
80  this->start_scanning();
81  }
82  } else if (this->has_ap()) {
83  this->setup_ap_config_();
84  if (this->output_power_.has_value() && !this->wifi_apply_output_power_(*this->output_power_)) {
85  ESP_LOGV(TAG, "Setting Output Power Option failed!");
86  }
87 #ifdef USE_CAPTIVE_PORTAL
89  this->wifi_sta_pre_setup_();
90  this->start_scanning();
92  }
93 #endif
94  }
95 #ifdef USE_IMPROV
97  if (this->wifi_mode_(true, {}))
99  }
100 #endif
101  this->wifi_apply_hostname_();
102 }
103 
105  this->wifi_loop_();
106  const uint32_t now = millis();
107 
108  if (this->has_sta()) {
109  switch (this->state_) {
111  this->status_set_warning();
112  if (millis() - this->action_started_ > 5000) {
113  if (this->fast_connect_) {
114  this->start_connecting(this->sta_[0], false);
115  } else {
116  this->start_scanning();
117  }
118  }
119  break;
120  }
122  this->status_set_warning();
123  this->check_scanning_finished();
124  break;
125  }
128  this->status_set_warning();
130  break;
131  }
132 
134  if (!this->is_connected()) {
135  ESP_LOGW(TAG, "WiFi Connection lost... Reconnecting...");
137  this->retry_connect();
138  } else {
139  this->status_clear_warning();
140  this->last_connected_ = now;
141  }
142  break;
143  }
146  break;
148  return;
149  }
150 
151  if (this->has_ap() && !this->ap_setup_) {
152  if (now - this->last_connected_ > this->ap_timeout_) {
153  ESP_LOGI(TAG, "Starting fallback AP!");
154  this->setup_ap_config_();
155 #ifdef USE_CAPTIVE_PORTAL
158 #endif
159  }
160  }
161 
162 #ifdef USE_IMPROV
163  if (esp32_improv::global_improv_component != nullptr) {
164  if (!this->is_connected()) {
165  if (this->wifi_mode_(true, {}))
167  }
168  }
169 
170 #endif
171 
172  if (!this->has_ap() && this->reboot_timeout_ != 0) {
173  if (now - this->last_connected_ > this->reboot_timeout_) {
174  ESP_LOGE(TAG, "Can't connect to WiFi, rebooting...");
175  App.reboot();
176  }
177  }
178  }
179 }
180 
182 
183 bool WiFiComponent::has_ap() const { return this->has_ap_; }
184 bool WiFiComponent::has_sta() const { return !this->sta_.empty(); }
185 void WiFiComponent::set_fast_connect(bool fast_connect) { this->fast_connect_ = fast_connect; }
186 #ifdef USE_WIFI_11KV_SUPPORT
187 void WiFiComponent::set_btm(bool btm) { this->btm_ = btm; }
188 void WiFiComponent::set_rrm(bool rrm) { this->rrm_ = rrm; }
189 #endif
191  if (this->has_sta())
192  return this->wifi_sta_ip();
193  if (this->has_ap())
194  return this->wifi_soft_ap_ip();
195  return {};
196 }
198  if (this->has_sta())
199  return this->wifi_dns_ip_(num);
200  return {};
201 }
202 std::string WiFiComponent::get_use_address() const {
203  if (this->use_address_.empty()) {
204  return App.get_name() + ".local";
205  }
206  return this->use_address_;
207 }
208 void WiFiComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; }
210  this->wifi_mode_({}, true);
211 
212  if (this->ap_setup_)
213  return;
214 
215  if (this->ap_.get_ssid().empty()) {
216  std::string name = App.get_name();
217  if (name.length() > 32) {
219  name.erase(name.begin() + 25, name.end() - 7); // Remove characters between 25 and the mac address
220  } else {
221  name = name.substr(0, 32);
222  }
223  }
224  this->ap_.set_ssid(name);
225  }
226 
227  ESP_LOGCONFIG(TAG, "Setting up AP...");
228 
229  ESP_LOGCONFIG(TAG, " AP SSID: '%s'", this->ap_.get_ssid().c_str());
230  ESP_LOGCONFIG(TAG, " AP Password: '%s'", this->ap_.get_password().c_str());
231  if (this->ap_.get_manual_ip().has_value()) {
232  auto manual = *this->ap_.get_manual_ip();
233  ESP_LOGCONFIG(TAG, " AP Static IP: '%s'", manual.static_ip.str().c_str());
234  ESP_LOGCONFIG(TAG, " AP Gateway: '%s'", manual.gateway.str().c_str());
235  ESP_LOGCONFIG(TAG, " AP Subnet: '%s'", manual.subnet.str().c_str());
236  }
237 
238  this->ap_setup_ = this->wifi_start_ap_(this->ap_);
239  ESP_LOGCONFIG(TAG, " IP Address: %s", this->wifi_soft_ap_ip().str().c_str());
240 
241  if (!this->has_sta()) {
243  }
244 }
245 
247  return 10.0f; // before other loop components
248 }
249 void WiFiComponent::set_ap(const WiFiAP &ap) {
250  this->ap_ = ap;
251  this->has_ap_ = true;
252 }
253 void WiFiComponent::add_sta(const WiFiAP &ap) { this->sta_.push_back(ap); }
255  this->clear_sta();
256  this->add_sta(ap);
257 }
258 void WiFiComponent::clear_sta() { this->sta_.clear(); }
259 void WiFiComponent::save_wifi_sta(const std::string &ssid, const std::string &password) {
260  SavedWifiSettings save{};
261  strncpy(save.ssid, ssid.c_str(), sizeof(save.ssid));
262  strncpy(save.password, password.c_str(), sizeof(save.password));
263  this->pref_.save(&save);
264  // ensure it's written immediately
266 
267  WiFiAP sta{};
268  sta.set_ssid(ssid);
269  sta.set_password(password);
270  this->set_sta(sta);
271 }
272 
273 void WiFiComponent::start_connecting(const WiFiAP &ap, bool two) {
274  ESP_LOGI(TAG, "WiFi Connecting to '%s'...", ap.get_ssid().c_str());
275 #ifdef ESPHOME_LOG_HAS_VERBOSE
276  ESP_LOGV(TAG, "Connection Params:");
277  ESP_LOGV(TAG, " SSID: '%s'", ap.get_ssid().c_str());
278  if (ap.get_bssid().has_value()) {
279  bssid_t b = *ap.get_bssid();
280  ESP_LOGV(TAG, " BSSID: %02X:%02X:%02X:%02X:%02X:%02X", b[0], b[1], b[2], b[3], b[4], b[5]);
281  } else {
282  ESP_LOGV(TAG, " BSSID: Not Set");
283  }
284 
285 #ifdef USE_WIFI_WPA2_EAP
286  if (ap.get_eap().has_value()) {
287  ESP_LOGV(TAG, " WPA2 Enterprise authentication configured:");
288  EAPAuth eap_config = ap.get_eap().value();
289  ESP_LOGV(TAG, " Identity: " LOG_SECRET("'%s'"), eap_config.identity.c_str());
290  ESP_LOGV(TAG, " Username: " LOG_SECRET("'%s'"), eap_config.username.c_str());
291  ESP_LOGV(TAG, " Password: " LOG_SECRET("'%s'"), eap_config.password.c_str());
292  bool ca_cert_present = eap_config.ca_cert != nullptr && strlen(eap_config.ca_cert);
293  bool client_cert_present = eap_config.client_cert != nullptr && strlen(eap_config.client_cert);
294  bool client_key_present = eap_config.client_key != nullptr && strlen(eap_config.client_key);
295  ESP_LOGV(TAG, " CA Cert: %s", ca_cert_present ? "present" : "not present");
296  ESP_LOGV(TAG, " Client Cert: %s", client_cert_present ? "present" : "not present");
297  ESP_LOGV(TAG, " Client Key: %s", client_key_present ? "present" : "not present");
298  } else {
299 #endif
300  ESP_LOGV(TAG, " Password: " LOG_SECRET("'%s'"), ap.get_password().c_str());
301 #ifdef USE_WIFI_WPA2_EAP
302  }
303 #endif
304  if (ap.get_channel().has_value()) {
305  ESP_LOGV(TAG, " Channel: %u", *ap.get_channel());
306  } else {
307  ESP_LOGV(TAG, " Channel: Not Set");
308  }
309  if (ap.get_manual_ip().has_value()) {
310  ManualIP m = *ap.get_manual_ip();
311  ESP_LOGV(TAG, " Manual IP: Static IP=%s Gateway=%s Subnet=%s DNS1=%s DNS2=%s", m.static_ip.str().c_str(),
312  m.gateway.str().c_str(), m.subnet.str().c_str(), m.dns1.str().c_str(), m.dns2.str().c_str());
313  } else {
314  ESP_LOGV(TAG, " Using DHCP IP");
315  }
316  ESP_LOGV(TAG, " Hidden: %s", YESNO(ap.get_hidden()));
317 #endif
318 
319  if (!this->wifi_sta_connect_(ap)) {
320  ESP_LOGE(TAG, "wifi_sta_connect_ failed!");
321  this->retry_connect();
322  return;
323  }
324 
325  if (!two) {
327  } else {
329  }
330  this->action_started_ = millis();
331 }
332 
333 const LogString *get_signal_bars(int8_t rssi) {
334  // LOWER ONE QUARTER BLOCK
335  // Unicode: U+2582, UTF-8: E2 96 82
336  // LOWER HALF BLOCK
337  // Unicode: U+2584, UTF-8: E2 96 84
338  // LOWER THREE QUARTERS BLOCK
339  // Unicode: U+2586, UTF-8: E2 96 86
340  // FULL BLOCK
341  // Unicode: U+2588, UTF-8: E2 96 88
342  if (rssi >= -50) {
343  return LOG_STR("\033[0;32m" // green
344  "\xe2\x96\x82"
345  "\xe2\x96\x84"
346  "\xe2\x96\x86"
347  "\xe2\x96\x88"
348  "\033[0m");
349  } else if (rssi >= -65) {
350  return LOG_STR("\033[0;33m" // yellow
351  "\xe2\x96\x82"
352  "\xe2\x96\x84"
353  "\xe2\x96\x86"
354  "\033[0;37m"
355  "\xe2\x96\x88"
356  "\033[0m");
357  } else if (rssi >= -85) {
358  return LOG_STR("\033[0;33m" // yellow
359  "\xe2\x96\x82"
360  "\xe2\x96\x84"
361  "\033[0;37m"
362  "\xe2\x96\x86"
363  "\xe2\x96\x88"
364  "\033[0m");
365  } else {
366  return LOG_STR("\033[0;31m" // red
367  "\xe2\x96\x82"
368  "\033[0;37m"
369  "\xe2\x96\x84"
370  "\xe2\x96\x86"
371  "\xe2\x96\x88"
372  "\033[0m");
373  }
374 }
375 
377  bssid_t bssid = wifi_bssid();
378 
379  ESP_LOGCONFIG(TAG, " Local MAC: %s", get_mac_address_pretty().c_str());
380  ESP_LOGCONFIG(TAG, " SSID: " LOG_SECRET("'%s'"), wifi_ssid().c_str());
381  ESP_LOGCONFIG(TAG, " IP Address: %s", wifi_sta_ip().str().c_str());
382  ESP_LOGCONFIG(TAG, " BSSID: " LOG_SECRET("%02X:%02X:%02X:%02X:%02X:%02X"), bssid[0], bssid[1], bssid[2], bssid[3],
383  bssid[4], bssid[5]);
384  ESP_LOGCONFIG(TAG, " Hostname: '%s'", App.get_name().c_str());
385  int8_t rssi = wifi_rssi();
386  ESP_LOGCONFIG(TAG, " Signal strength: %d dB %s", rssi, LOG_STR_ARG(get_signal_bars(rssi)));
387  if (this->selected_ap_.get_bssid().has_value()) {
388  ESP_LOGV(TAG, " Priority: %.1f", this->get_sta_priority(*this->selected_ap_.get_bssid()));
389  }
390  ESP_LOGCONFIG(TAG, " Channel: %" PRId32, wifi_channel_());
391  ESP_LOGCONFIG(TAG, " Subnet: %s", wifi_subnet_mask_().str().c_str());
392  ESP_LOGCONFIG(TAG, " Gateway: %s", wifi_gateway_ip_().str().c_str());
393  ESP_LOGCONFIG(TAG, " DNS1: %s", wifi_dns_ip_(0).str().c_str());
394  ESP_LOGCONFIG(TAG, " DNS2: %s", wifi_dns_ip_(1).str().c_str());
395 #ifdef USE_WIFI_11KV_SUPPORT
396  ESP_LOGCONFIG(TAG, " BTM: %s", this->btm_ ? "enabled" : "disabled");
397  ESP_LOGCONFIG(TAG, " RRM: %s", this->rrm_ ? "enabled" : "disabled");
398 #endif
399 }
400 
403  return;
404 
405  ESP_LOGD(TAG, "Enabling WIFI...");
406  this->error_from_callback_ = false;
408  this->start();
409 }
410 
413  return;
414 
415  ESP_LOGD(TAG, "Disabling WIFI...");
417  this->wifi_disconnect_();
418  this->wifi_mode_(false, false);
419 }
420 
422 
424  this->action_started_ = millis();
425  ESP_LOGD(TAG, "Starting scan...");
426  this->wifi_scan_start_(this->passive_scan_);
428 }
429 
431  if (!this->scan_done_) {
432  if (millis() - this->action_started_ > 30000) {
433  ESP_LOGE(TAG, "Scan timeout!");
434  this->retry_connect();
435  }
436  return;
437  }
438  this->scan_done_ = false;
439 
440  ESP_LOGD(TAG, "Found networks:");
441  if (this->scan_result_.empty()) {
442  ESP_LOGD(TAG, " No network found!");
443  this->retry_connect();
444  return;
445  }
446 
447  for (auto &res : this->scan_result_) {
448  for (auto &ap : this->sta_) {
449  if (res.matches(ap)) {
450  res.set_matches(true);
451  if (!this->has_sta_priority(res.get_bssid())) {
452  this->set_sta_priority(res.get_bssid(), ap.get_priority());
453  }
454  res.set_priority(this->get_sta_priority(res.get_bssid()));
455  break;
456  }
457  }
458  }
459 
460  std::stable_sort(this->scan_result_.begin(), this->scan_result_.end(),
461  [](const WiFiScanResult &a, const WiFiScanResult &b) {
462  // return true if a is better than b
463  if (a.get_matches() && !b.get_matches())
464  return true;
465  if (!a.get_matches() && b.get_matches())
466  return false;
467 
468  if (a.get_matches() && b.get_matches()) {
469  // if both match, check priority
470  if (a.get_priority() != b.get_priority())
471  return a.get_priority() > b.get_priority();
472  }
473 
474  return a.get_rssi() > b.get_rssi();
475  });
476 
477  for (auto &res : this->scan_result_) {
478  char bssid_s[18];
479  auto bssid = res.get_bssid();
480  sprintf(bssid_s, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
481 
482  if (res.get_matches()) {
483  ESP_LOGI(TAG, "- '%s' %s" LOG_SECRET("(%s) ") "%s", res.get_ssid().c_str(),
484  res.get_is_hidden() ? "(HIDDEN) " : "", bssid_s, LOG_STR_ARG(get_signal_bars(res.get_rssi())));
485  ESP_LOGD(TAG, " Channel: %u", res.get_channel());
486  ESP_LOGD(TAG, " RSSI: %d dB", res.get_rssi());
487  } else {
488  ESP_LOGD(TAG, "- " LOG_SECRET("'%s'") " " LOG_SECRET("(%s) ") "%s", res.get_ssid().c_str(), bssid_s,
489  LOG_STR_ARG(get_signal_bars(res.get_rssi())));
490  }
491  }
492 
493  if (!this->scan_result_[0].get_matches()) {
494  ESP_LOGW(TAG, "No matching network found!");
495  this->retry_connect();
496  return;
497  }
498 
499  WiFiAP connect_params;
500  WiFiScanResult scan_res = this->scan_result_[0];
501  for (auto &config : this->sta_) {
502  // search for matching STA config, at least one will match (from checks before)
503  if (!scan_res.matches(config)) {
504  continue;
505  }
506 
507  if (config.get_hidden()) {
508  // selected network is hidden, we use the data from the config
509  connect_params.set_hidden(true);
510  connect_params.set_ssid(config.get_ssid());
511  // don't set BSSID and channel, there might be multiple hidden networks
512  // but we can't know which one is the correct one. Rely on probe-req with just SSID.
513  } else {
514  // selected network is visible, we use the data from the scan
515  // limit the connect params to only connect to exactly this network
516  // (network selection is done during scan phase).
517  connect_params.set_hidden(false);
518  connect_params.set_ssid(scan_res.get_ssid());
519  connect_params.set_channel(scan_res.get_channel());
520  connect_params.set_bssid(scan_res.get_bssid());
521  }
522  // copy manual IP (if set)
523  connect_params.set_manual_ip(config.get_manual_ip());
524 
525 #ifdef USE_WIFI_WPA2_EAP
526  // copy EAP parameters (if set)
527  connect_params.set_eap(config.get_eap());
528 #endif
529 
530  // copy password (if set)
531  connect_params.set_password(config.get_password());
532 
533  break;
534  }
535 
536  yield();
537 
538  this->selected_ap_ = connect_params;
539  this->start_connecting(connect_params, false);
540 }
541 
543  ESP_LOGCONFIG(TAG, "WiFi:");
544  this->print_connect_params_();
545 }
546 
548  auto status = this->wifi_sta_connect_status_();
549 
551  if (wifi_ssid().empty()) {
552  ESP_LOGW(TAG, "Incomplete connection.");
553  this->retry_connect();
554  return;
555  }
556 
557  ESP_LOGI(TAG, "WiFi Connected!");
558  this->print_connect_params_();
559 
560  if (this->has_ap()) {
561 #ifdef USE_CAPTIVE_PORTAL
562  if (this->is_captive_portal_active_()) {
564  }
565 #endif
566  ESP_LOGD(TAG, "Disabling AP...");
567  this->wifi_mode_({}, false);
568  }
569 #ifdef USE_IMPROV
570  if (this->is_esp32_improv_active_()) {
572  }
573 #endif
574 
576  this->num_retried_ = 0;
577  return;
578  }
579 
580  uint32_t now = millis();
581  if (now - this->action_started_ > 30000) {
582  ESP_LOGW(TAG, "Timeout while connecting to WiFi.");
583  this->retry_connect();
584  return;
585  }
586 
587  if (this->error_from_callback_) {
588  ESP_LOGW(TAG, "Error while connecting to network.");
589  this->retry_connect();
590  return;
591  }
592 
594  return;
595  }
596 
598  ESP_LOGW(TAG, "WiFi network can not be found anymore.");
599  this->retry_connect();
600  return;
601  }
602 
604  ESP_LOGW(TAG, "Connecting to WiFi network failed. Are the credentials wrong?");
605  this->retry_connect();
606  return;
607  }
608 
609  ESP_LOGW(TAG, "WiFi Unknown connection status %d", (int) status);
610  this->retry_connect();
611 }
612 
614  if (this->selected_ap_.get_bssid()) {
615  auto bssid = *this->selected_ap_.get_bssid();
616  float priority = this->get_sta_priority(bssid);
617  this->set_sta_priority(bssid, priority - 1.0f);
618  }
619 
620  delay(10);
621  if (!this->is_captive_portal_active_() && !this->is_esp32_improv_active_() &&
622  (this->num_retried_ > 5 || this->error_from_callback_)) {
623  // If retry failed for more than 5 times, let's restart STA
624  ESP_LOGW(TAG, "Restarting WiFi adapter...");
625  this->wifi_mode_(false, {});
626  delay(100); // NOLINT
627  this->num_retried_ = 0;
628  } else {
629  this->num_retried_++;
630  }
631  this->error_from_callback_ = false;
633  yield();
635  this->start_connecting(this->selected_ap_, true);
636  return;
637  }
638 
640  this->action_started_ = millis();
641 }
642 
644  if (!this->has_sta() || this->state_ == WIFI_COMPONENT_STATE_DISABLED) {
645  return true;
646  }
647  return this->is_connected();
648 }
649 void WiFiComponent::set_reboot_timeout(uint32_t reboot_timeout) { this->reboot_timeout_ = reboot_timeout; }
651  return this->state_ == WIFI_COMPONENT_STATE_STA_CONNECTED &&
653 }
654 void WiFiComponent::set_power_save_mode(WiFiPowerSaveMode power_save) { this->power_save_ = power_save; }
655 
656 void WiFiComponent::set_passive_scan(bool passive) { this->passive_scan_ = passive; }
657 
658 std::string WiFiComponent::format_mac_addr(const uint8_t *mac) {
659  char buf[20];
660  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
661  return buf;
662 }
664 #ifdef USE_CAPTIVE_PORTAL
666 #else
667  return false;
668 #endif
669 }
671 #ifdef USE_IMPROV
673 #else
674  return false;
675 #endif
676 }
677 
678 void WiFiAP::set_ssid(const std::string &ssid) { this->ssid_ = ssid; }
679 void WiFiAP::set_bssid(bssid_t bssid) { this->bssid_ = bssid; }
680 void WiFiAP::set_bssid(optional<bssid_t> bssid) { this->bssid_ = bssid; }
681 void WiFiAP::set_password(const std::string &password) { this->password_ = password; }
682 #ifdef USE_WIFI_WPA2_EAP
683 void WiFiAP::set_eap(optional<EAPAuth> eap_auth) { this->eap_ = std::move(eap_auth); }
684 #endif
685 void WiFiAP::set_channel(optional<uint8_t> channel) { this->channel_ = channel; }
686 void WiFiAP::set_manual_ip(optional<ManualIP> manual_ip) { this->manual_ip_ = manual_ip; }
687 void WiFiAP::set_hidden(bool hidden) { this->hidden_ = hidden; }
688 const std::string &WiFiAP::get_ssid() const { return this->ssid_; }
689 const optional<bssid_t> &WiFiAP::get_bssid() const { return this->bssid_; }
690 const std::string &WiFiAP::get_password() const { return this->password_; }
691 #ifdef USE_WIFI_WPA2_EAP
692 const optional<EAPAuth> &WiFiAP::get_eap() const { return this->eap_; }
693 #endif
694 const optional<uint8_t> &WiFiAP::get_channel() const { return this->channel_; }
695 const optional<ManualIP> &WiFiAP::get_manual_ip() const { return this->manual_ip_; }
696 bool WiFiAP::get_hidden() const { return this->hidden_; }
697 
698 WiFiScanResult::WiFiScanResult(const bssid_t &bssid, std::string ssid, uint8_t channel, int8_t rssi, bool with_auth,
699  bool is_hidden)
700  : bssid_(bssid),
701  ssid_(std::move(ssid)),
702  channel_(channel),
703  rssi_(rssi),
704  with_auth_(with_auth),
705  is_hidden_(is_hidden) {}
706 bool WiFiScanResult::matches(const WiFiAP &config) {
707  if (config.get_hidden()) {
708  // User configured a hidden network, only match actually hidden networks
709  // don't match SSID
710  if (!this->is_hidden_)
711  return false;
712  } else if (!config.get_ssid().empty()) {
713  // check if SSID matches
714  if (config.get_ssid() != this->ssid_)
715  return false;
716  } else {
717  // network is configured without SSID - match other settings
718  }
719  // If BSSID configured, only match for correct BSSIDs
720  if (config.get_bssid().has_value() && *config.get_bssid() != this->bssid_)
721  return false;
722 
723 #ifdef USE_WIFI_WPA2_EAP
724  // BSSID requires auth but no PSK or EAP credentials given
725  if (this->with_auth_ && (config.get_password().empty() && !config.get_eap().has_value()))
726  return false;
727 
728  // BSSID does not require auth, but PSK or EAP credentials given
729  if (!this->with_auth_ && (!config.get_password().empty() || config.get_eap().has_value()))
730  return false;
731 #else
732  // If PSK given, only match for networks with auth (and vice versa)
733  if (config.get_password().empty() == this->with_auth_)
734  return false;
735 #endif
736 
737  // If channel configured, only match networks on that channel.
738  if (config.get_channel().has_value() && *config.get_channel() != this->channel_) {
739  return false;
740  }
741  return true;
742 }
743 bool WiFiScanResult::get_matches() const { return this->matches_; }
745 const bssid_t &WiFiScanResult::get_bssid() const { return this->bssid_; }
746 const std::string &WiFiScanResult::get_ssid() const { return this->ssid_; }
747 uint8_t WiFiScanResult::get_channel() const { return this->channel_; }
748 int8_t WiFiScanResult::get_rssi() const { return this->rssi_; }
749 bool WiFiScanResult::get_with_auth() const { return this->with_auth_; }
750 bool WiFiScanResult::get_is_hidden() const { return this->is_hidden_; }
751 
752 bool WiFiScanResult::operator==(const WiFiScanResult &rhs) const { return this->bssid_ == rhs.bssid_; }
753 
754 WiFiComponent *global_wifi_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
755 
756 } // namespace wifi
757 } // namespace esphome
Nothing has been initialized yet.
const char * name
Definition: stm32flash.h:78
This component is responsible for managing the ESP WiFi interface.
const std::string & get_ssid() const
std::array< uint8_t, 6 > bssid_t
const optional< EAPAuth > & get_eap() const
static std::string format_mac_addr(const uint8_t mac[6])
const std::string & get_password() const
WiFiPowerSaveMode power_save_
void save_wifi_sta(const std::string &ssid, const std::string &password)
network::IPAddress wifi_dns_ip_(int num)
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
std::string str() const
Definition: ip_address.h:28
bool wifi_apply_output_power_(float output_power)
void add_sta(const WiFiAP &ap)
WiFi is in STA(+AP) mode and currently connecting to an AP a second time.
STL namespace.
WiFi is in STA(+AP) mode and successfully connected.
bool has_value() const
Definition: optional.h:87
network::IPAddress static_ip
void set_ap(const WiFiAP &ap)
Setup an Access Point that should be created if no connection to a station can be made...
network::IPAddress get_ip_address()
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:27
const bssid_t & get_bssid() const
void set_channel(optional< uint8_t > channel)
bool save(const T *src)
Definition: preferences.h:21
network::IPAddress gateway
void start_connecting(const WiFiAP &ap, bool two)
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.
uint8_t m
Definition: bl0939.h:20
Struct for setting static IPs in WiFiComponent.
void set_power_save_mode(WiFiPowerSaveMode power_save)
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()
Definition: component.cpp:153
WiFi is in cooldown mode because something went wrong, scanning will begin after a short period of ti...
void set_ssid(const std::string &ssid)
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().
Definition: application.h:152
void status_set_warning()
Definition: component.cpp:145
network::IPAddress get_dns_address(int num)
void set_reboot_timeout(uint32_t reboot_timeout)
bool is_name_add_mac_suffix_enabled() const
Definition: application.h:159
ESP32ImprovComponent * global_improv_component
void loop() override
Reconnect WiFi if required.
uint8_t priority
bool operator==(const WiFiScanResult &rhs) const
uint8_t status
Definition: bl0942.h:23
ESPPreferenceObject pref_
network::IPAddress dns2
The second DNS server. 0.0.0.0 for default.
const char * client_cert
void IRAM_ATTR HOT yield()
Definition: core.cpp:26
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.
Definition: helpers.cpp:172
std::vector< WiFiAP > sta_
optional< float > output_power_
network::IPAddress subnet
void set_sta(const WiFiAP &ap)
void set_bssid(bssid_t bssid)
const std::string & get_ssid() const
void set_eap(optional< EAPAuth > eap_auth)
void set_hidden(bool hidden)
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.
Definition: helpers.cpp:565
virtual bool sync()=0
Commit pending writes to flash.
std::string get_use_address() const
std::string get_compilation_time() const
Definition: application.h:161
float get_sta_priority(const bssid_t bssid)
void set_password(const std::string &password)
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)
Definition: core.cpp:28
const LogString * get_signal_bars(int8_t rssi)