ESPHome  2024.4.0
wifi_component.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include "esphome/core/defines.h"
7 #include "esphome/core/helpers.h"
8 
9 #include <string>
10 #include <vector>
11 
12 #ifdef USE_ESP32_FRAMEWORK_ARDUINO
13 #include <WiFi.h>
14 #include <WiFiType.h>
15 #include <esp_wifi.h>
16 #endif
17 
18 #ifdef USE_LIBRETINY
19 #include <WiFi.h>
20 #endif
21 
22 #ifdef USE_ESP8266
23 #include <ESP8266WiFi.h>
24 #include <ESP8266WiFiType.h>
25 
26 #if defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE < VERSION_CODE(2, 4, 0)
27 extern "C" {
28 #include <user_interface.h>
29 };
30 #endif
31 #endif
32 
33 #ifdef USE_RP2040
34 extern "C" {
35 #include "cyw43.h"
36 #include "cyw43_country.h"
37 #include "pico/cyw43_arch.h"
38 }
39 
40 #include <WiFi.h>
41 #endif
42 
43 namespace esphome {
44 namespace wifi {
45 
47  char ssid[33];
48  char password[65];
49 } PACKED; // NOLINT
50 
52  uint8_t bssid[6];
53  uint8_t channel;
54 } PACKED; // NOLINT
55 
77 };
78 
79 enum class WiFiSTAConnectStatus : int {
80  IDLE,
81  CONNECTING,
82  CONNECTED,
85 };
86 
88 struct ManualIP {
94 };
95 
96 #ifdef USE_WIFI_WPA2_EAP
97 struct EAPAuth {
98  std::string identity; // required for all auth types
99  std::string username;
100  std::string password;
101  const char *ca_cert; // optionally verify authentication server
102  // used for EAP-TLS
103  const char *client_cert;
104  const char *client_key;
105 };
106 #endif // USE_WIFI_WPA2_EAP
107 
108 using bssid_t = std::array<uint8_t, 6>;
109 
110 class WiFiAP {
111  public:
112  void set_ssid(const std::string &ssid);
113  void set_bssid(bssid_t bssid);
114  void set_bssid(optional<bssid_t> bssid);
115  void set_password(const std::string &password);
116 #ifdef USE_WIFI_WPA2_EAP
117  void set_eap(optional<EAPAuth> eap_auth);
118 #endif // USE_WIFI_WPA2_EAP
119  void set_channel(optional<uint8_t> channel);
120  void set_priority(float priority) { priority_ = priority; }
121  void set_manual_ip(optional<ManualIP> manual_ip);
122  void set_hidden(bool hidden);
123  const std::string &get_ssid() const;
124  const optional<bssid_t> &get_bssid() const;
125  const std::string &get_password() const;
126 #ifdef USE_WIFI_WPA2_EAP
127  const optional<EAPAuth> &get_eap() const;
128 #endif // USE_WIFI_WPA2_EAP
129  const optional<uint8_t> &get_channel() const;
130  float get_priority() const { return priority_; }
131  const optional<ManualIP> &get_manual_ip() const;
132  bool get_hidden() const;
133 
134  protected:
135  std::string ssid_;
137  std::string password_;
138 #ifdef USE_WIFI_WPA2_EAP
140 #endif // USE_WIFI_WPA2_EAP
142  float priority_{0};
144  bool hidden_{false};
145 };
146 
148  public:
149  WiFiScanResult(const bssid_t &bssid, std::string ssid, uint8_t channel, int8_t rssi, bool with_auth, bool is_hidden);
150 
151  bool matches(const WiFiAP &config);
152 
153  bool get_matches() const;
154  void set_matches(bool matches);
155  const bssid_t &get_bssid() const;
156  const std::string &get_ssid() const;
157  uint8_t get_channel() const;
158  int8_t get_rssi() const;
159  bool get_with_auth() const;
160  bool get_is_hidden() const;
161  float get_priority() const { return priority_; }
162  void set_priority(float priority) { priority_ = priority; }
163 
164  bool operator==(const WiFiScanResult &rhs) const;
165 
166  protected:
167  bool matches_{false};
169  std::string ssid_;
170  uint8_t channel_;
171  int8_t rssi_;
174  float priority_{0.0f};
175 };
176 
179  float priority;
180 };
181 
186 };
187 
188 #ifdef USE_ESP_IDF
189 struct IDFWiFiEvent;
190 #endif
191 
193 class WiFiComponent : public Component {
194  public:
196  WiFiComponent();
197 
198  void set_sta(const WiFiAP &ap);
199  void add_sta(const WiFiAP &ap);
200  void clear_sta();
201 
202 #ifdef USE_WIFI_AP
203 
210  void set_ap(const WiFiAP &ap);
211  WiFiAP get_ap() { return this->ap_; }
212 #endif // USE_WIFI_AP
213 
214  void enable();
215  void disable();
216  bool is_disabled();
217  void start_scanning();
218  void check_scanning_finished();
219  void start_connecting(const WiFiAP &ap, bool two);
220  void set_fast_connect(bool fast_connect);
221  void set_ap_timeout(uint32_t ap_timeout) { ap_timeout_ = ap_timeout; }
222 
223  void check_connecting_finished();
224 
225  void retry_connect();
226 
227  bool can_proceed() override;
228 
229  void set_reboot_timeout(uint32_t reboot_timeout);
230 
231  bool is_connected();
232 
233  void set_power_save_mode(WiFiPowerSaveMode power_save);
234  void set_output_power(float output_power) { output_power_ = output_power; }
235 
236  void set_passive_scan(bool passive);
237 
238  void save_wifi_sta(const std::string &ssid, const std::string &password);
239  // ========== INTERNAL METHODS ==========
240  // (In most use cases you won't need these)
242  void setup() override;
243  void start();
244  void dump_config() override;
246  float get_setup_priority() const override;
247  float get_loop_priority() const override;
248 
250  void loop() override;
251 
252  bool has_sta() const;
253  bool has_ap() const;
254 
255 #ifdef USE_WIFI_11KV_SUPPORT
256  void set_btm(bool btm);
257  void set_rrm(bool rrm);
258 #endif
259 
260  network::IPAddress get_dns_address(int num);
262  std::string get_use_address() const;
263  void set_use_address(const std::string &use_address);
264 
265  const std::vector<WiFiScanResult> &get_scan_result() const { return scan_result_; }
266 
267  network::IPAddress wifi_soft_ap_ip();
268 
269  bool has_sta_priority(const bssid_t &bssid) {
270  for (auto &it : this->sta_priorities_) {
271  if (it.bssid == bssid)
272  return true;
273  }
274  return false;
275  }
276  float get_sta_priority(const bssid_t bssid) {
277  for (auto &it : this->sta_priorities_) {
278  if (it.bssid == bssid)
279  return it.priority;
280  }
281  return 0.0f;
282  }
283  void set_sta_priority(const bssid_t bssid, float priority) {
284  for (auto &it : this->sta_priorities_) {
285  if (it.bssid == bssid) {
286  it.priority = priority;
287  return;
288  }
289  }
290  this->sta_priorities_.push_back(WiFiSTAPriority{
291  .bssid = bssid,
292  .priority = priority,
293  });
294  }
295 
296  network::IPAddresses wifi_sta_ip_addresses();
297  std::string wifi_ssid();
298  bssid_t wifi_bssid();
299 
300  int8_t wifi_rssi();
301 
302  void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
303 
304  Trigger<> *get_connect_trigger() const { return this->connect_trigger_; };
305  Trigger<> *get_disconnect_trigger() const { return this->disconnect_trigger_; };
306 
307  protected:
308  static std::string format_mac_addr(const uint8_t mac[6]);
309 
310 #ifdef USE_WIFI_AP
311  void setup_ap_config_();
312 #endif // USE_WIFI_AP
313 
314  void print_connect_params_();
315 
316  void wifi_loop_();
317  bool wifi_mode_(optional<bool> sta, optional<bool> ap);
318  bool wifi_sta_pre_setup_();
319  bool wifi_apply_output_power_(float output_power);
320  bool wifi_apply_power_save_();
321  bool wifi_sta_ip_config_(optional<ManualIP> manual_ip);
322  bool wifi_apply_hostname_();
323  bool wifi_sta_connect_(const WiFiAP &ap);
324  void wifi_pre_setup_();
325  WiFiSTAConnectStatus wifi_sta_connect_status_();
326  bool wifi_scan_start_(bool passive);
327 
328 #ifdef USE_WIFI_AP
329  bool wifi_ap_ip_config_(optional<ManualIP> manual_ip);
330  bool wifi_start_ap_(const WiFiAP &ap);
331 #endif // USE_WIFI_AP
332 
333  bool wifi_disconnect_();
334  int32_t wifi_channel_();
335  network::IPAddress wifi_subnet_mask_();
336  network::IPAddress wifi_gateway_ip_();
337  network::IPAddress wifi_dns_ip_(int num);
338 
339  bool is_captive_portal_active_();
340  bool is_esp32_improv_active_();
341 
342  void load_fast_connect_settings_();
343  void save_fast_connect_settings_();
344 
345 #ifdef USE_ESP8266
346  static void wifi_event_callback(System_Event_t *event);
347  void wifi_scan_done_callback_(void *arg, STATUS status);
348  static void s_wifi_scan_done_callback(void *arg, STATUS status);
349 #endif
350 
351 #ifdef USE_ESP32_FRAMEWORK_ARDUINO
352  void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info);
353  void wifi_scan_done_callback_();
354 #endif
355 #ifdef USE_ESP_IDF
356  void wifi_process_event_(IDFWiFiEvent *data);
357 #endif
358 
359 #ifdef USE_RP2040
360  static int s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result);
361  void wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result);
362 #endif
363 
364 #ifdef USE_LIBRETINY
365  void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info);
366  void wifi_scan_done_callback_();
367 #endif
368 
369  std::string use_address_;
370  std::vector<WiFiAP> sta_;
371  std::vector<WiFiSTAPriority> sta_priorities_;
373  bool fast_connect_{false};
374 
375  bool has_ap_{false};
378  bool handled_connected_state_{false};
379  uint32_t action_started_;
380  uint8_t num_retried_{0};
381  uint32_t last_connected_{0};
382  uint32_t reboot_timeout_{};
383  uint32_t ap_timeout_{};
385  bool error_from_callback_{false};
386  std::vector<WiFiScanResult> scan_result_;
387  bool scan_done_{false};
388  bool ap_setup_{false};
390  bool passive_scan_{false};
393  bool has_saved_wifi_settings_{false};
394 #ifdef USE_WIFI_11KV_SUPPORT
395  bool btm_{false};
396  bool rrm_{false};
397 #endif
399  bool got_ipv4_address_{false};
400 #if USE_NETWORK_IPV6
401  uint8_t num_ipv6_addresses_{0};
402 #endif /* USE_NETWORK_IPV6 */
403 
404  Trigger<> *connect_trigger_{new Trigger<>()};
405  Trigger<> *disconnect_trigger_{new Trigger<>()};
406 };
407 
408 extern WiFiComponent *global_wifi_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
409 
410 template<typename... Ts> class WiFiConnectedCondition : public Condition<Ts...> {
411  public:
412  bool check(Ts... x) override { return global_wifi_component->is_connected(); }
413 };
414 
415 template<typename... Ts> class WiFiEnabledCondition : public Condition<Ts...> {
416  public:
417  bool check(Ts... x) override { return !global_wifi_component->is_disabled(); }
418 };
419 
420 template<typename... Ts> class WiFiEnableAction : public Action<Ts...> {
421  public:
422  void play(Ts... x) override { global_wifi_component->enable(); }
423 };
424 
425 template<typename... Ts> class WiFiDisableAction : public Action<Ts...> {
426  public:
427  void play(Ts... x) override { global_wifi_component->disable(); }
428 };
429 
430 } // namespace wifi
431 } // namespace esphome
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_
Trigger * get_connect_trigger() const
WiFiComponent * global_wifi_component
std::array< IPAddress, 5 > IPAddresses
Definition: ip_address.h:139
optional< uint8_t > channel_
uint8_t priority
uint8_t status
Definition: bl0942.h:23
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:114
std::vector< WiFiAP > sta_
optional< float > output_power_
network::IPAddress subnet
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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.