ESPHome  2022.12.8
wifi_component.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/defines.h"
6 #include "esphome/core/helpers.h"
8 
9 #include <string>
10 #include <vector>
11 
12 #ifdef USE_ESP32_FRAMEWORK_ARDUINO
13 #include <esp_wifi.h>
14 #include <WiFiType.h>
15 #include <WiFi.h>
16 #endif
17 
18 #ifdef USE_ESP8266
19 #include <ESP8266WiFi.h>
20 #include <ESP8266WiFiType.h>
21 
22 #if defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE < VERSION_CODE(2, 4, 0)
23 extern "C" {
24 #include <user_interface.h>
25 };
26 #endif
27 #endif
28 
29 #ifdef USE_RP2040
30 extern "C" {
31 #include "cyw43.h"
32 #include "cyw43_country.h"
33 #include "pico/cyw43_arch.h"
34 }
35 
36 #include <WiFi.h>
37 #endif
38 
39 namespace esphome {
40 namespace wifi {
41 
43  char ssid[33];
44  char password[65];
45 } PACKED; // NOLINT
46 
66 };
67 
68 enum class WiFiSTAConnectStatus : int {
69  IDLE,
70  CONNECTING,
71  CONNECTED,
74 };
75 
77 struct ManualIP {
83 };
84 
85 #ifdef USE_WIFI_WPA2_EAP
86 struct EAPAuth {
87  std::string identity; // required for all auth types
88  std::string username;
89  std::string password;
90  const char *ca_cert; // optionally verify authentication server
91  // used for EAP-TLS
92  const char *client_cert;
93  const char *client_key;
94 };
95 #endif // USE_WIFI_WPA2_EAP
96 
97 using bssid_t = std::array<uint8_t, 6>;
98 
99 class WiFiAP {
100  public:
101  void set_ssid(const std::string &ssid);
102  void set_bssid(bssid_t bssid);
103  void set_bssid(optional<bssid_t> bssid);
104  void set_password(const std::string &password);
105 #ifdef USE_WIFI_WPA2_EAP
106  void set_eap(optional<EAPAuth> eap_auth);
107 #endif // USE_WIFI_WPA2_EAP
108  void set_channel(optional<uint8_t> channel);
109  void set_priority(float priority) { priority_ = priority; }
110  void set_manual_ip(optional<ManualIP> manual_ip);
111  void set_hidden(bool hidden);
112  const std::string &get_ssid() const;
113  const optional<bssid_t> &get_bssid() const;
114  const std::string &get_password() const;
115 #ifdef USE_WIFI_WPA2_EAP
116  const optional<EAPAuth> &get_eap() const;
117 #endif // USE_WIFI_WPA2_EAP
118  const optional<uint8_t> &get_channel() const;
119  float get_priority() const { return priority_; }
120  const optional<ManualIP> &get_manual_ip() const;
121  bool get_hidden() const;
122 
123  protected:
124  std::string ssid_;
126  std::string password_;
127 #ifdef USE_WIFI_WPA2_EAP
129 #endif // USE_WIFI_WPA2_EAP
131  float priority_{0};
133  bool hidden_{false};
134 };
135 
137  public:
138  WiFiScanResult(const bssid_t &bssid, std::string ssid, uint8_t channel, int8_t rssi, bool with_auth, bool is_hidden);
139 
140  bool matches(const WiFiAP &config);
141 
142  bool get_matches() const;
143  void set_matches(bool matches);
144  const bssid_t &get_bssid() const;
145  const std::string &get_ssid() const;
146  uint8_t get_channel() const;
147  int8_t get_rssi() const;
148  bool get_with_auth() const;
149  bool get_is_hidden() const;
150  float get_priority() const { return priority_; }
151  void set_priority(float priority) { priority_ = priority; }
152 
153  bool operator==(const WiFiScanResult &rhs) const;
154 
155  protected:
156  bool matches_{false};
158  std::string ssid_;
159  uint8_t channel_;
160  int8_t rssi_;
163  float priority_{0.0f};
164 };
165 
168  float priority;
169 };
170 
175 };
176 
177 #ifdef USE_ESP_IDF
178 struct IDFWiFiEvent;
179 #endif
180 
182 class WiFiComponent : public Component {
183  public:
185  WiFiComponent();
186 
187  void set_sta(const WiFiAP &ap);
188  void add_sta(const WiFiAP &ap);
189  void clear_sta();
190 
198  void set_ap(const WiFiAP &ap);
199  WiFiAP get_ap() { return this->ap_; }
200 
201  void start_scanning();
202  void check_scanning_finished();
203  void start_connecting(const WiFiAP &ap, bool two);
204  void set_fast_connect(bool fast_connect);
205  void set_ap_timeout(uint32_t ap_timeout) { ap_timeout_ = ap_timeout; }
206 
207  void check_connecting_finished();
208 
209  void retry_connect();
210 
211  bool can_proceed() override;
212 
213  void set_reboot_timeout(uint32_t reboot_timeout);
214 
215  bool is_connected();
216 
217  void set_power_save_mode(WiFiPowerSaveMode power_save);
218  void set_output_power(float output_power) { output_power_ = output_power; }
219 
220  void save_wifi_sta(const std::string &ssid, const std::string &password);
221  // ========== INTERNAL METHODS ==========
222  // (In most use cases you won't need these)
224  void setup() override;
225  void dump_config() override;
227  float get_setup_priority() const override;
228  float get_loop_priority() const override;
229 
231  void loop() override;
232 
233  bool has_sta() const;
234  bool has_ap() const;
235 
236 #ifdef USE_WIFI_11KV_SUPPORT
237  void set_btm(bool btm);
238  void set_rrm(bool rrm);
239 #endif
240 
242  std::string get_use_address() const;
243  void set_use_address(const std::string &use_address);
244 
245  const std::vector<WiFiScanResult> &get_scan_result() const { return scan_result_; }
246 
247  network::IPAddress wifi_soft_ap_ip();
248 
249  bool has_sta_priority(const bssid_t &bssid) {
250  for (auto &it : this->sta_priorities_) {
251  if (it.bssid == bssid)
252  return true;
253  }
254  return false;
255  }
256  float get_sta_priority(const bssid_t bssid) {
257  for (auto &it : this->sta_priorities_) {
258  if (it.bssid == bssid)
259  return it.priority;
260  }
261  return 0.0f;
262  }
263  void set_sta_priority(const bssid_t bssid, float priority) {
264  for (auto &it : this->sta_priorities_) {
265  if (it.bssid == bssid) {
266  it.priority = priority;
267  return;
268  }
269  }
270  this->sta_priorities_.push_back(WiFiSTAPriority{
271  .bssid = bssid,
272  .priority = priority,
273  });
274  }
275 
276  network::IPAddress wifi_sta_ip();
277  std::string wifi_ssid();
278  bssid_t wifi_bssid();
279 
280  int8_t wifi_rssi();
281 
282  protected:
283  static std::string format_mac_addr(const uint8_t mac[6]);
284  void setup_ap_config_();
285  void print_connect_params_();
286 
287  void wifi_loop_();
288  bool wifi_mode_(optional<bool> sta, optional<bool> ap);
289  bool wifi_sta_pre_setup_();
290  bool wifi_apply_output_power_(float output_power);
291  bool wifi_apply_power_save_();
292  bool wifi_sta_ip_config_(optional<ManualIP> manual_ip);
293  bool wifi_apply_hostname_();
294  bool wifi_sta_connect_(const WiFiAP &ap);
295  void wifi_pre_setup_();
296  WiFiSTAConnectStatus wifi_sta_connect_status_();
297  bool wifi_scan_start_();
298  bool wifi_ap_ip_config_(optional<ManualIP> manual_ip);
299  bool wifi_start_ap_(const WiFiAP &ap);
300  bool wifi_disconnect_();
301  int32_t wifi_channel_();
302  network::IPAddress wifi_subnet_mask_();
303  network::IPAddress wifi_gateway_ip_();
304  network::IPAddress wifi_dns_ip_(int num);
305 
306  bool is_captive_portal_active_();
307  bool is_esp32_improv_active_();
308 
309 #ifdef USE_ESP8266
310  static void wifi_event_callback(System_Event_t *event);
311  void wifi_scan_done_callback_(void *arg, STATUS status);
312  static void s_wifi_scan_done_callback(void *arg, STATUS status);
313 #endif
314 
315 #ifdef USE_ESP32_FRAMEWORK_ARDUINO
316 #if ESP_IDF_VERSION_MAJOR >= 4
317  void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info);
318 #else
319  void wifi_event_callback_(system_event_id_t event, system_event_info_t info);
320 #endif
321  void wifi_scan_done_callback_();
322 #endif
323 #ifdef USE_ESP_IDF
324  void wifi_process_event_(IDFWiFiEvent *data);
325 #endif
326 
327 #ifdef USE_RP2040
328  static int s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result);
329  void wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result);
330 #endif
331 
332  std::string use_address_;
333  std::vector<WiFiAP> sta_;
334  std::vector<WiFiSTAPriority> sta_priorities_;
336  bool fast_connect_{false};
337 
338  bool has_ap_{false};
341  uint32_t action_started_;
342  uint8_t num_retried_{0};
343  uint32_t last_connected_{0};
344  uint32_t reboot_timeout_{};
345  uint32_t ap_timeout_{};
347  bool error_from_callback_{false};
348  std::vector<WiFiScanResult> scan_result_;
349  bool scan_done_{false};
350  bool ap_setup_{false};
353  bool has_saved_wifi_settings_{false};
354 #ifdef USE_WIFI_11KV_SUPPORT
355  bool btm_{false};
356  bool rrm_{false};
357 #endif
358 };
359 
360 extern WiFiComponent *global_wifi_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
361 
362 template<typename... Ts> class WiFiConnectedCondition : public Condition<Ts...> {
363  public:
364  bool check(Ts... x) override;
365 };
366 
367 template<typename... Ts> bool WiFiConnectedCondition<Ts...>::check(Ts... x) {
368  return global_wifi_component->is_connected();
369 }
370 
371 } // namespace wifi
372 } // namespace esphome
void setup()
Nothing has been initialized yet.
void loop()
This component is responsible for managing the ESP WiFi interface.
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:41
float get_priority() const
void set_output_power(float output_power)
const std::vector< WiFiScanResult > & get_scan_result() const
void set_sta_priority(const bssid_t bssid, float priority)
optional< ManualIP > manual_ip_
struct esphome::wifi::SavedWifiSettings PACKED
WiFi is in STA(+AP) mode and currently connecting to an AP a second time.
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.
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_
WiFiComponent * global_wifi_component
optional< uint8_t > channel_
uint8_t priority
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
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
Definition: a4988.cpp:4
float get_sta_priority(const bssid_t bssid)
optional< EAPAuth > eap_
WiFi is in AP-only mode and internal AP is already enabled.
network::IPAddress get_ip_address()
Definition: util.cpp:29