ESPHome  2024.11.0
wifi_component.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/defines.h"
4 #ifdef USE_WIFI
8 #include "esphome/core/helpers.h"
9 
10 #include <string>
11 #include <vector>
12 
13 #ifdef USE_ESP32_FRAMEWORK_ARDUINO
14 #include <WiFi.h>
15 #include <WiFiType.h>
16 #include <esp_wifi.h>
17 #endif
18 
19 #ifdef USE_LIBRETINY
20 #include <WiFi.h>
21 #endif
22 
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>
26 #else
27 #include <esp_wpa2.h>
28 #endif
29 #endif
30 
31 #ifdef USE_ESP8266
32 #include <ESP8266WiFi.h>
33 #include <ESP8266WiFiType.h>
34 
35 #if defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE < VERSION_CODE(2, 4, 0)
36 extern "C" {
37 #include <user_interface.h>
38 };
39 #endif
40 #endif
41 
42 #ifdef USE_RP2040
43 extern "C" {
44 #include "cyw43.h"
45 #include "cyw43_country.h"
46 #include "pico/cyw43_arch.h"
47 }
48 
49 #include <WiFi.h>
50 #endif
51 
52 namespace esphome {
53 namespace wifi {
54 
56  char ssid[33];
57  char password[65];
58 } PACKED; // NOLINT
59 
61  uint8_t bssid[6];
62  uint8_t channel;
63 } PACKED; // NOLINT
64 
86 };
87 
88 enum class WiFiSTAConnectStatus : int {
89  IDLE,
90  CONNECTING,
91  CONNECTED,
94 };
95 
97 struct ManualIP {
103 };
104 
105 #ifdef USE_WIFI_WPA2_EAP
106 struct EAPAuth {
107  std::string identity; // required for all auth types
108  std::string username;
109  std::string password;
110  const char *ca_cert; // optionally verify authentication server
111  // used for EAP-TLS
112  const char *client_cert;
113  const char *client_key;
114 // used for EAP-TTLS
115 #ifdef USE_ESP_IDF
116  esp_eap_ttls_phase2_types ttls_phase_2;
117 #endif
118 };
119 #endif // USE_WIFI_WPA2_EAP
120 
121 using bssid_t = std::array<uint8_t, 6>;
122 
123 class WiFiAP {
124  public:
125  void set_ssid(const std::string &ssid);
126  void set_bssid(bssid_t bssid);
127  void set_bssid(optional<bssid_t> bssid);
128  void set_password(const std::string &password);
129 #ifdef USE_WIFI_WPA2_EAP
130  void set_eap(optional<EAPAuth> eap_auth);
131 #endif // USE_WIFI_WPA2_EAP
132  void set_channel(optional<uint8_t> channel);
133  void set_priority(float priority) { priority_ = priority; }
134  void set_manual_ip(optional<ManualIP> manual_ip);
135  void set_hidden(bool hidden);
136  const std::string &get_ssid() const;
137  const optional<bssid_t> &get_bssid() const;
138  const std::string &get_password() const;
139 #ifdef USE_WIFI_WPA2_EAP
140  const optional<EAPAuth> &get_eap() const;
141 #endif // USE_WIFI_WPA2_EAP
142  const optional<uint8_t> &get_channel() const;
143  float get_priority() const { return priority_; }
144  const optional<ManualIP> &get_manual_ip() const;
145  bool get_hidden() const;
146 
147  protected:
148  std::string ssid_;
150  std::string password_;
151 #ifdef USE_WIFI_WPA2_EAP
153 #endif // USE_WIFI_WPA2_EAP
155  float priority_{0};
157  bool hidden_{false};
158 };
159 
161  public:
162  WiFiScanResult(const bssid_t &bssid, std::string ssid, uint8_t channel, int8_t rssi, bool with_auth, bool is_hidden);
163 
164  bool matches(const WiFiAP &config);
165 
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;
174  float get_priority() const { return priority_; }
175  void set_priority(float priority) { priority_ = priority; }
176 
177  bool operator==(const WiFiScanResult &rhs) const;
178 
179  protected:
180  bool matches_{false};
182  std::string ssid_;
183  uint8_t channel_;
184  int8_t rssi_;
187  float priority_{0.0f};
188 };
189 
192  float priority;
193 };
194 
199 };
200 
201 #ifdef USE_ESP_IDF
202 struct IDFWiFiEvent;
203 #endif
204 
206 class WiFiComponent : public Component {
207  public:
209  WiFiComponent();
210 
211  void set_sta(const WiFiAP &ap);
212  void add_sta(const WiFiAP &ap);
213  void clear_sta();
214 
215 #ifdef USE_WIFI_AP
216 
223  void set_ap(const WiFiAP &ap);
224  WiFiAP get_ap() { return this->ap_; }
225 #endif // USE_WIFI_AP
226 
227  void enable();
228  void disable();
229  bool is_disabled();
230  void start_scanning();
231  void check_scanning_finished();
232  void start_connecting(const WiFiAP &ap, bool two);
233  void set_fast_connect(bool fast_connect);
234  void set_ap_timeout(uint32_t ap_timeout) { ap_timeout_ = ap_timeout; }
235 
236  void check_connecting_finished();
237 
238  void retry_connect();
239 
240  bool can_proceed() override;
241 
242  void set_reboot_timeout(uint32_t reboot_timeout);
243 
244  bool is_connected();
245 
246  void set_power_save_mode(WiFiPowerSaveMode power_save);
247  void set_output_power(float output_power) { output_power_ = output_power; }
248 
249  void set_passive_scan(bool passive);
250 
251  void save_wifi_sta(const std::string &ssid, const std::string &password);
252  // ========== INTERNAL METHODS ==========
253  // (In most use cases you won't need these)
255  void setup() override;
256  void start();
257  void dump_config() override;
259  float get_setup_priority() const override;
260  float get_loop_priority() const override;
261 
263  void loop() override;
264 
265  bool has_sta() const;
266  bool has_ap() const;
267 
268 #ifdef USE_WIFI_11KV_SUPPORT
269  void set_btm(bool btm);
270  void set_rrm(bool rrm);
271 #endif
272 
273  network::IPAddress get_dns_address(int num);
275  std::string get_use_address() const;
276  void set_use_address(const std::string &use_address);
277 
278  const std::vector<WiFiScanResult> &get_scan_result() const { return scan_result_; }
279 
280  network::IPAddress wifi_soft_ap_ip();
281 
282  bool has_sta_priority(const bssid_t &bssid) {
283  for (auto &it : this->sta_priorities_) {
284  if (it.bssid == bssid)
285  return true;
286  }
287  return false;
288  }
289  float get_sta_priority(const bssid_t bssid) {
290  for (auto &it : this->sta_priorities_) {
291  if (it.bssid == bssid)
292  return it.priority;
293  }
294  return 0.0f;
295  }
296  void set_sta_priority(const bssid_t bssid, float priority) {
297  for (auto &it : this->sta_priorities_) {
298  if (it.bssid == bssid) {
299  it.priority = priority;
300  return;
301  }
302  }
303  this->sta_priorities_.push_back(WiFiSTAPriority{
304  .bssid = bssid,
305  .priority = priority,
306  });
307  }
308 
309  network::IPAddresses wifi_sta_ip_addresses();
310  std::string wifi_ssid();
311  bssid_t wifi_bssid();
312 
313  int8_t wifi_rssi();
314 
315  void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
316 
317  Trigger<> *get_connect_trigger() const { return this->connect_trigger_; };
318  Trigger<> *get_disconnect_trigger() const { return this->disconnect_trigger_; };
319 
320  protected:
321  static std::string format_mac_addr(const uint8_t mac[6]);
322 
323 #ifdef USE_WIFI_AP
324  void setup_ap_config_();
325 #endif // USE_WIFI_AP
326 
327  void print_connect_params_();
328 
329  void wifi_loop_();
330  bool wifi_mode_(optional<bool> sta, optional<bool> ap);
331  bool wifi_sta_pre_setup_();
332  bool wifi_apply_output_power_(float output_power);
333  bool wifi_apply_power_save_();
334  bool wifi_sta_ip_config_(optional<ManualIP> manual_ip);
335  bool wifi_apply_hostname_();
336  bool wifi_sta_connect_(const WiFiAP &ap);
337  void wifi_pre_setup_();
338  WiFiSTAConnectStatus wifi_sta_connect_status_();
339  bool wifi_scan_start_(bool passive);
340 
341 #ifdef USE_WIFI_AP
342  bool wifi_ap_ip_config_(optional<ManualIP> manual_ip);
343  bool wifi_start_ap_(const WiFiAP &ap);
344 #endif // USE_WIFI_AP
345 
346  bool wifi_disconnect_();
347  int32_t wifi_channel_();
348  network::IPAddress wifi_subnet_mask_();
349  network::IPAddress wifi_gateway_ip_();
350  network::IPAddress wifi_dns_ip_(int num);
351 
352  bool is_captive_portal_active_();
353  bool is_esp32_improv_active_();
354 
355  void load_fast_connect_settings_();
356  void save_fast_connect_settings_();
357 
358 #ifdef USE_ESP8266
359  static void wifi_event_callback(System_Event_t *event);
360  void wifi_scan_done_callback_(void *arg, STATUS status);
361  static void s_wifi_scan_done_callback(void *arg, STATUS status);
362 #endif
363 
364 #ifdef USE_ESP32_FRAMEWORK_ARDUINO
365  void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info);
366  void wifi_scan_done_callback_();
367 #endif
368 #ifdef USE_ESP_IDF
369  void wifi_process_event_(IDFWiFiEvent *data);
370 #endif
371 
372 #ifdef USE_RP2040
373  static int s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result);
374  void wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result);
375 #endif
376 
377 #ifdef USE_LIBRETINY
378  void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info);
379  void wifi_scan_done_callback_();
380 #endif
381 
382  std::string use_address_;
383  std::vector<WiFiAP> sta_;
384  std::vector<WiFiSTAPriority> sta_priorities_;
386  bool fast_connect_{false};
387  bool retry_hidden_{false};
388 
389  bool has_ap_{false};
392  bool handled_connected_state_{false};
393  uint32_t action_started_;
394  uint8_t num_retried_{0};
395  uint32_t last_connected_{0};
396  uint32_t reboot_timeout_{};
397  uint32_t ap_timeout_{};
399  bool error_from_callback_{false};
400  std::vector<WiFiScanResult> scan_result_;
401  bool scan_done_{false};
402  bool ap_setup_{false};
404  bool passive_scan_{false};
407  bool has_saved_wifi_settings_{false};
408 #ifdef USE_WIFI_11KV_SUPPORT
409  bool btm_{false};
410  bool rrm_{false};
411 #endif
413  bool got_ipv4_address_{false};
414 #if USE_NETWORK_IPV6
415  uint8_t num_ipv6_addresses_{0};
416 #endif /* USE_NETWORK_IPV6 */
417 
418  Trigger<> *connect_trigger_{new Trigger<>()};
419  Trigger<> *disconnect_trigger_{new Trigger<>()};
420 };
421 
422 extern WiFiComponent *global_wifi_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
423 
424 template<typename... Ts> class WiFiConnectedCondition : public Condition<Ts...> {
425  public:
426  bool check(Ts... x) override { return global_wifi_component->is_connected(); }
427 };
428 
429 template<typename... Ts> class WiFiEnabledCondition : public Condition<Ts...> {
430  public:
431  bool check(Ts... x) override { return !global_wifi_component->is_disabled(); }
432 };
433 
434 template<typename... Ts> class WiFiEnableAction : public Action<Ts...> {
435  public:
436  void play(Ts... x) override { global_wifi_component->enable(); }
437 };
438 
439 template<typename... Ts> class WiFiDisableAction : public Action<Ts...> {
440  public:
441  void play(Ts... x) override { global_wifi_component->disable(); }
442 };
443 
444 } // namespace wifi
445 } // namespace esphome
446 #endif
void setup()
Nothing has been initialized yet.
void loop()
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.
Definition: util.cpp:52
float get_priority() const
void set_output_power(float output_power)
Trigger * get_disconnect_trigger() const
const std::vector< WiFiScanResult > & get_scan_result() const
uint16_t x
Definition: tt21100.cpp:17
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.
void set_priority(float priority)
network::IPAddress static_ip
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition: util.cpp:15
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()
Definition: util.cpp:40
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.
Definition: automation.h:74
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
Definition: ip_address.h:141
optional< uint8_t > channel_
uint8_t priority
uint8_t status
Definition: bl0942.h:74
ESPPreferenceObject pref_
bool is_disabled()
Return whether the network is disabled (only wifi for now)
Definition: util.cpp:32
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)
Definition: optional.h:113
std::vector< WiFiAP > sta_
optional< float > output_power_
network::IPAddress subnet
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
ESPPreferenceObject fast_connect_pref_
void play(Ts... x) override
float get_sta_priority(const bssid_t bssid)
optional< EAPAuth > eap_
WiFi is in AP-only mode and internal AP is already enabled.