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