ESPHome  2022.1.2
wifi_component.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/macros.h"
5 #include "esphome/core/defines.h"
7 #include "esphome/core/helpers.h"
9 #include <string>
10 
11 #ifdef USE_ESP32_FRAMEWORK_ARDUINO
12 #include <esp_wifi.h>
13 #include <WiFiType.h>
14 #include <WiFi.h>
15 #endif
16 
17 #ifdef USE_ESP8266
18 #include <ESP8266WiFi.h>
19 #include <ESP8266WiFiType.h>
20 
21 #if defined(USE_ESP8266) && ARDUINO_VERSION_CODE < VERSION_CODE(2, 4, 0)
22 extern "C" {
23 #include <user_interface.h>
24 };
25 #endif
26 #endif
27 
28 namespace esphome {
29 namespace wifi {
30 
32  char ssid[33];
33  char password[65];
34 } PACKED; // NOLINT
35 
55 };
56 
57 enum class WiFiSTAConnectStatus : int {
58  IDLE,
59  CONNECTING,
60  CONNECTED,
63 };
64 
66 struct ManualIP {
72 };
73 
74 #ifdef USE_WIFI_WPA2_EAP
75 struct EAPAuth {
76  std::string identity; // required for all auth types
77  std::string username;
78  std::string password;
79  const char *ca_cert; // optionally verify authentication server
80  // used for EAP-TLS
81  const char *client_cert;
82  const char *client_key;
83 };
84 #endif // USE_WIFI_WPA2_EAP
85 
86 using bssid_t = std::array<uint8_t, 6>;
87 
88 class WiFiAP {
89  public:
90  void set_ssid(const std::string &ssid);
91  void set_bssid(bssid_t bssid);
92  void set_bssid(optional<bssid_t> bssid);
93  void set_password(const std::string &password);
94 #ifdef USE_WIFI_WPA2_EAP
95  void set_eap(optional<EAPAuth> eap_auth);
96 #endif // USE_WIFI_WPA2_EAP
97  void set_channel(optional<uint8_t> channel);
98  void set_priority(float priority) { priority_ = priority; }
99  void set_manual_ip(optional<ManualIP> manual_ip);
100  void set_hidden(bool hidden);
101  const std::string &get_ssid() const;
102  const optional<bssid_t> &get_bssid() const;
103  const std::string &get_password() const;
104 #ifdef USE_WIFI_WPA2_EAP
105  const optional<EAPAuth> &get_eap() const;
106 #endif // USE_WIFI_WPA2_EAP
107  const optional<uint8_t> &get_channel() const;
108  float get_priority() const { return priority_; }
109  const optional<ManualIP> &get_manual_ip() const;
110  bool get_hidden() const;
111 
112  protected:
113  std::string ssid_;
115  std::string password_;
116 #ifdef USE_WIFI_WPA2_EAP
118 #endif // USE_WIFI_WPA2_EAP
120  float priority_{0};
122  bool hidden_{false};
123 };
124 
126  public:
127  WiFiScanResult(const bssid_t &bssid, std::string ssid, uint8_t channel, int8_t rssi, bool with_auth, bool is_hidden);
128 
129  bool matches(const WiFiAP &config);
130 
131  bool get_matches() const;
132  void set_matches(bool matches);
133  const bssid_t &get_bssid() const;
134  const std::string &get_ssid() const;
135  uint8_t get_channel() const;
136  int8_t get_rssi() const;
137  bool get_with_auth() const;
138  bool get_is_hidden() const;
139  float get_priority() const { return priority_; }
140  void set_priority(float priority) { priority_ = priority; }
141 
142  protected:
143  bool matches_{false};
145  std::string ssid_;
146  uint8_t channel_;
147  int8_t rssi_;
150  float priority_{0.0f};
151 };
152 
155  float priority;
156 };
157 
162 };
163 
164 #ifdef USE_ESP_IDF
165 struct IDFWiFiEvent;
166 #endif
167 
169 class WiFiComponent : public Component {
170  public:
172  WiFiComponent();
173 
174  void set_sta(const WiFiAP &ap);
175  void add_sta(const WiFiAP &ap);
176  void clear_sta();
177 
185  void set_ap(const WiFiAP &ap);
186 
187  void start_scanning();
188  void check_scanning_finished();
189  void start_connecting(const WiFiAP &ap, bool two);
190  void set_fast_connect(bool fast_connect);
191  void set_ap_timeout(uint32_t ap_timeout) { ap_timeout_ = ap_timeout; }
192 
193  void check_connecting_finished();
194 
195  void retry_connect();
196 
197  bool can_proceed() override;
198 
199  void set_reboot_timeout(uint32_t reboot_timeout);
200 
201  bool is_connected();
202 
203  void set_power_save_mode(WiFiPowerSaveMode power_save);
204  void set_output_power(float output_power) { output_power_ = output_power; }
205 
206  void save_wifi_sta(const std::string &ssid, const std::string &password);
207  // ========== INTERNAL METHODS ==========
208  // (In most use cases you won't need these)
210  void setup() override;
211  void dump_config() override;
213  float get_setup_priority() const override;
214  float get_loop_priority() const override;
215 
217  void loop() override;
218 
219  bool has_sta() const;
220  bool has_ap() const;
221 
223  std::string get_use_address() const;
224  void set_use_address(const std::string &use_address);
225 
226  const std::vector<WiFiScanResult> &get_scan_result() const { return scan_result_; }
227 
228  network::IPAddress wifi_soft_ap_ip();
229 
230  bool has_sta_priority(const bssid_t &bssid) {
231  for (auto &it : this->sta_priorities_)
232  if (it.bssid == bssid)
233  return true;
234  return false;
235  }
236  float get_sta_priority(const bssid_t bssid) {
237  for (auto &it : this->sta_priorities_)
238  if (it.bssid == bssid)
239  return it.priority;
240  return 0.0f;
241  }
242  void set_sta_priority(const bssid_t bssid, float priority) {
243  for (auto &it : this->sta_priorities_)
244  if (it.bssid == bssid) {
245  it.priority = priority;
246  return;
247  }
248  this->sta_priorities_.push_back(WiFiSTAPriority{
249  .bssid = bssid,
250  .priority = priority,
251  });
252  }
253 
254  network::IPAddress wifi_sta_ip();
255  std::string wifi_ssid();
256  bssid_t wifi_bssid();
257 
258  int8_t wifi_rssi();
259 
260  protected:
261  static std::string format_mac_addr(const uint8_t mac[6]);
262  void setup_ap_config_();
263  void print_connect_params_();
264 
265  void wifi_loop_();
266  bool wifi_mode_(optional<bool> sta, optional<bool> ap);
267  bool wifi_sta_pre_setup_();
268  bool wifi_apply_output_power_(float output_power);
269  bool wifi_apply_power_save_();
270  bool wifi_sta_ip_config_(optional<ManualIP> manual_ip);
271  bool wifi_apply_hostname_();
272  bool wifi_sta_connect_(const WiFiAP &ap);
273  void wifi_pre_setup_();
274  WiFiSTAConnectStatus wifi_sta_connect_status_();
275  bool wifi_scan_start_();
276  bool wifi_ap_ip_config_(optional<ManualIP> manual_ip);
277  bool wifi_start_ap_(const WiFiAP &ap);
278  bool wifi_disconnect_();
279  int32_t wifi_channel_();
280  network::IPAddress wifi_subnet_mask_();
281  network::IPAddress wifi_gateway_ip_();
282  network::IPAddress wifi_dns_ip_(int num);
283 
284  bool is_captive_portal_active_();
285  bool is_esp32_improv_active_();
286 
287 #ifdef USE_ESP8266
288  static void wifi_event_callback(System_Event_t *event);
289  void wifi_scan_done_callback_(void *arg, STATUS status);
290  static void s_wifi_scan_done_callback(void *arg, STATUS status);
291 #endif
292 
293 #ifdef USE_ESP32_FRAMEWORK_ARDUINO
294 #if ESP_IDF_VERSION_MAJOR >= 4
295  void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info);
296 #else
297  void wifi_event_callback_(system_event_id_t event, system_event_info_t info);
298 #endif
299  void wifi_scan_done_callback_();
300 #endif
301 #ifdef USE_ESP_IDF
302  void wifi_process_event_(IDFWiFiEvent *);
303 #endif
304 
305  std::string use_address_;
306  std::vector<WiFiAP> sta_;
307  std::vector<WiFiSTAPriority> sta_priorities_;
309  bool fast_connect_{false};
310 
311  bool has_ap_{false};
314  uint32_t action_started_;
315  uint8_t num_retried_{0};
316  uint32_t last_connected_{0};
317  uint32_t reboot_timeout_{};
318  uint32_t ap_timeout_{};
320  bool error_from_callback_{false};
321  std::vector<WiFiScanResult> scan_result_;
322  bool scan_done_{false};
323  bool ap_setup_{false};
326  bool has_saved_wifi_settings_{false};
327 };
328 
329 extern WiFiComponent *global_wifi_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
330 
331 template<typename... Ts> class WiFiConnectedCondition : public Condition<Ts...> {
332  public:
333  bool check(Ts... x) override;
334 };
335 
336 template<typename... Ts> bool WiFiConnectedCondition<Ts...>::check(Ts... x) {
337  return global_wifi_component->is_connected();
338 }
339 
340 } // namespace wifi
341 } // 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:69
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
ESPPreferenceObject pref_
network::IPAddress dns2
The second DNS server. 0.0.0.0 for default.
const char * client_cert
std::vector< WiFiSTAPriority > sta_priorities_
std::vector< WiFiAP > sta_
optional< float > output_power_
network::IPAddress subnet
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
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