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