mirror of
https://github.com/esphome/esphome.git
synced 2025-10-20 10:43:48 +01:00
[wifi] Convert fast_connect to compile-time define, save 608-1024 bytes flash
This commit is contained in:
@@ -407,7 +407,8 @@ async def to_code(config):
|
|||||||
|
|
||||||
cg.add(var.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT]))
|
cg.add(var.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT]))
|
||||||
cg.add(var.set_power_save_mode(config[CONF_POWER_SAVE_MODE]))
|
cg.add(var.set_power_save_mode(config[CONF_POWER_SAVE_MODE]))
|
||||||
cg.add(var.set_fast_connect(config[CONF_FAST_CONNECT]))
|
if config[CONF_FAST_CONNECT]:
|
||||||
|
cg.add_define("USE_WIFI_FAST_CONNECT")
|
||||||
cg.add(var.set_passive_scan(config[CONF_PASSIVE_SCAN]))
|
cg.add(var.set_passive_scan(config[CONF_PASSIVE_SCAN]))
|
||||||
if CONF_OUTPUT_POWER in config:
|
if CONF_OUTPUT_POWER in config:
|
||||||
cg.add(var.set_output_power(config[CONF_OUTPUT_POWER]))
|
cg.add(var.set_output_power(config[CONF_OUTPUT_POWER]))
|
||||||
|
@@ -84,9 +84,9 @@ void WiFiComponent::start() {
|
|||||||
uint32_t hash = this->has_sta() ? fnv1_hash(App.get_compilation_time()) : 88491487UL;
|
uint32_t hash = this->has_sta() ? fnv1_hash(App.get_compilation_time()) : 88491487UL;
|
||||||
|
|
||||||
this->pref_ = global_preferences->make_preference<wifi::SavedWifiSettings>(hash, true);
|
this->pref_ = global_preferences->make_preference<wifi::SavedWifiSettings>(hash, true);
|
||||||
if (this->fast_connect_) {
|
#ifdef USE_WIFI_FAST_CONNECT
|
||||||
this->fast_connect_pref_ = global_preferences->make_preference<wifi::SavedWifiFastConnectSettings>(hash + 1, false);
|
this->fast_connect_pref_ = global_preferences->make_preference<wifi::SavedWifiFastConnectSettings>(hash + 1, false);
|
||||||
}
|
#endif
|
||||||
|
|
||||||
SavedWifiSettings save{};
|
SavedWifiSettings save{};
|
||||||
if (this->pref_.load(&save)) {
|
if (this->pref_.load(&save)) {
|
||||||
@@ -108,16 +108,16 @@ void WiFiComponent::start() {
|
|||||||
ESP_LOGV(TAG, "Setting Power Save Option failed");
|
ESP_LOGV(TAG, "Setting Power Save Option failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->fast_connect_) {
|
#ifdef USE_WIFI_FAST_CONNECT
|
||||||
this->trying_loaded_ap_ = this->load_fast_connect_settings_();
|
this->trying_loaded_ap_ = this->load_fast_connect_settings_();
|
||||||
if (!this->trying_loaded_ap_) {
|
if (!this->trying_loaded_ap_) {
|
||||||
this->ap_index_ = 0;
|
this->ap_index_ = 0;
|
||||||
this->selected_ap_ = this->sta_[this->ap_index_];
|
this->selected_ap_ = this->sta_[this->ap_index_];
|
||||||
}
|
}
|
||||||
this->start_connecting(this->selected_ap_, false);
|
this->start_connecting(this->selected_ap_, false);
|
||||||
} else {
|
#else
|
||||||
this->start_scanning();
|
this->start_scanning();
|
||||||
}
|
#endif
|
||||||
#ifdef USE_WIFI_AP
|
#ifdef USE_WIFI_AP
|
||||||
} else if (this->has_ap()) {
|
} else if (this->has_ap()) {
|
||||||
this->setup_ap_config_();
|
this->setup_ap_config_();
|
||||||
@@ -168,13 +168,19 @@ void WiFiComponent::loop() {
|
|||||||
case WIFI_COMPONENT_STATE_COOLDOWN: {
|
case WIFI_COMPONENT_STATE_COOLDOWN: {
|
||||||
this->status_set_warning(LOG_STR("waiting to reconnect"));
|
this->status_set_warning(LOG_STR("waiting to reconnect"));
|
||||||
if (millis() - this->action_started_ > 5000) {
|
if (millis() - this->action_started_ > 5000) {
|
||||||
if (this->fast_connect_ || this->retry_hidden_) {
|
#ifdef USE_WIFI_FAST_CONNECT
|
||||||
if (!this->selected_ap_.get_bssid().has_value())
|
if (!this->selected_ap_.get_bssid().has_value())
|
||||||
this->selected_ap_ = this->sta_[0];
|
this->selected_ap_ = this->sta_[0];
|
||||||
this->start_connecting(this->selected_ap_, false);
|
this->start_connecting(this->selected_ap_, false);
|
||||||
} else {
|
#else
|
||||||
|
if (!this->retry_hidden_) {
|
||||||
this->start_scanning();
|
this->start_scanning();
|
||||||
|
} else {
|
||||||
|
if (!this->selected_ap_.get_bssid().has_value())
|
||||||
|
this->selected_ap_ = this->sta_[0];
|
||||||
|
this->start_connecting(this->selected_ap_, false);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -244,7 +250,6 @@ WiFiComponent::WiFiComponent() { global_wifi_component = this; }
|
|||||||
|
|
||||||
bool WiFiComponent::has_ap() const { return this->has_ap_; }
|
bool WiFiComponent::has_ap() const { return this->has_ap_; }
|
||||||
bool WiFiComponent::has_sta() const { return !this->sta_.empty(); }
|
bool WiFiComponent::has_sta() const { return !this->sta_.empty(); }
|
||||||
void WiFiComponent::set_fast_connect(bool fast_connect) { this->fast_connect_ = fast_connect; }
|
|
||||||
#ifdef USE_WIFI_11KV_SUPPORT
|
#ifdef USE_WIFI_11KV_SUPPORT
|
||||||
void WiFiComponent::set_btm(bool btm) { this->btm_ = btm; }
|
void WiFiComponent::set_btm(bool btm) { this->btm_ = btm; }
|
||||||
void WiFiComponent::set_rrm(bool rrm) { this->rrm_ = rrm; }
|
void WiFiComponent::set_rrm(bool rrm) { this->rrm_ = rrm; }
|
||||||
@@ -719,9 +724,9 @@ void WiFiComponent::check_connecting_finished() {
|
|||||||
this->scan_result_.shrink_to_fit();
|
this->scan_result_.shrink_to_fit();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->fast_connect_) {
|
#ifdef USE_WIFI_FAST_CONNECT
|
||||||
this->save_fast_connect_settings_();
|
this->save_fast_connect_settings_();
|
||||||
}
|
#endif
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -769,7 +774,7 @@ void WiFiComponent::retry_connect() {
|
|||||||
delay(10);
|
delay(10);
|
||||||
if (!this->is_captive_portal_active_() && !this->is_esp32_improv_active_() &&
|
if (!this->is_captive_portal_active_() && !this->is_esp32_improv_active_() &&
|
||||||
(this->num_retried_ > 3 || this->error_from_callback_)) {
|
(this->num_retried_ > 3 || this->error_from_callback_)) {
|
||||||
if (this->fast_connect_) {
|
#ifdef USE_WIFI_FAST_CONNECT
|
||||||
if (this->trying_loaded_ap_) {
|
if (this->trying_loaded_ap_) {
|
||||||
this->trying_loaded_ap_ = false;
|
this->trying_loaded_ap_ = false;
|
||||||
this->ap_index_ = 0; // Retry from the first configured AP
|
this->ap_index_ = 0; // Retry from the first configured AP
|
||||||
@@ -783,7 +788,7 @@ void WiFiComponent::retry_connect() {
|
|||||||
}
|
}
|
||||||
this->num_retried_ = 0;
|
this->num_retried_ = 0;
|
||||||
this->selected_ap_ = this->sta_[this->ap_index_];
|
this->selected_ap_ = this->sta_[this->ap_index_];
|
||||||
} else {
|
#else
|
||||||
if (this->num_retried_ > 5) {
|
if (this->num_retried_ > 5) {
|
||||||
// If retry failed for more than 5 times, let's restart STA
|
// If retry failed for more than 5 times, let's restart STA
|
||||||
this->restart_adapter();
|
this->restart_adapter();
|
||||||
@@ -793,7 +798,7 @@ void WiFiComponent::retry_connect() {
|
|||||||
this->retry_hidden_ = true;
|
this->retry_hidden_ = true;
|
||||||
this->num_retried_++;
|
this->num_retried_++;
|
||||||
}
|
}
|
||||||
}
|
#endif
|
||||||
} else {
|
} else {
|
||||||
this->num_retried_++;
|
this->num_retried_++;
|
||||||
}
|
}
|
||||||
@@ -839,6 +844,7 @@ bool WiFiComponent::is_esp32_improv_active_() {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef USE_WIFI_FAST_CONNECT
|
||||||
bool WiFiComponent::load_fast_connect_settings_() {
|
bool WiFiComponent::load_fast_connect_settings_() {
|
||||||
SavedWifiFastConnectSettings fast_connect_save{};
|
SavedWifiFastConnectSettings fast_connect_save{};
|
||||||
|
|
||||||
@@ -873,6 +879,7 @@ void WiFiComponent::save_fast_connect_settings_() {
|
|||||||
ESP_LOGD(TAG, "Saved fast_connect settings");
|
ESP_LOGD(TAG, "Saved fast_connect settings");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void WiFiAP::set_ssid(const std::string &ssid) { this->ssid_ = ssid; }
|
void WiFiAP::set_ssid(const std::string &ssid) { this->ssid_ = ssid; }
|
||||||
void WiFiAP::set_bssid(bssid_t bssid) { this->bssid_ = bssid; }
|
void WiFiAP::set_bssid(bssid_t bssid) { this->bssid_ = bssid; }
|
||||||
|
@@ -240,7 +240,6 @@ class WiFiComponent : public Component {
|
|||||||
void start_scanning();
|
void start_scanning();
|
||||||
void check_scanning_finished();
|
void check_scanning_finished();
|
||||||
void start_connecting(const WiFiAP &ap, bool two);
|
void start_connecting(const WiFiAP &ap, bool two);
|
||||||
void set_fast_connect(bool fast_connect);
|
|
||||||
void set_ap_timeout(uint32_t ap_timeout) { ap_timeout_ = ap_timeout; }
|
void set_ap_timeout(uint32_t ap_timeout) { ap_timeout_ = ap_timeout; }
|
||||||
|
|
||||||
void check_connecting_finished();
|
void check_connecting_finished();
|
||||||
@@ -364,8 +363,10 @@ class WiFiComponent : public Component {
|
|||||||
bool is_captive_portal_active_();
|
bool is_captive_portal_active_();
|
||||||
bool is_esp32_improv_active_();
|
bool is_esp32_improv_active_();
|
||||||
|
|
||||||
|
#ifdef USE_WIFI_FAST_CONNECT
|
||||||
bool load_fast_connect_settings_();
|
bool load_fast_connect_settings_();
|
||||||
void save_fast_connect_settings_();
|
void save_fast_connect_settings_();
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef USE_ESP8266
|
#ifdef USE_ESP8266
|
||||||
static void wifi_event_callback(System_Event_t *event);
|
static void wifi_event_callback(System_Event_t *event);
|
||||||
@@ -399,7 +400,9 @@ class WiFiComponent : public Component {
|
|||||||
WiFiAP ap_;
|
WiFiAP ap_;
|
||||||
optional<float> output_power_;
|
optional<float> output_power_;
|
||||||
ESPPreferenceObject pref_;
|
ESPPreferenceObject pref_;
|
||||||
|
#ifdef USE_WIFI_FAST_CONNECT
|
||||||
ESPPreferenceObject fast_connect_pref_;
|
ESPPreferenceObject fast_connect_pref_;
|
||||||
|
#endif
|
||||||
|
|
||||||
// Group all 32-bit integers together
|
// Group all 32-bit integers together
|
||||||
uint32_t action_started_;
|
uint32_t action_started_;
|
||||||
@@ -417,8 +420,9 @@ class WiFiComponent : public Component {
|
|||||||
#endif /* USE_NETWORK_IPV6 */
|
#endif /* USE_NETWORK_IPV6 */
|
||||||
|
|
||||||
// Group all boolean values together
|
// Group all boolean values together
|
||||||
bool fast_connect_{false};
|
#ifdef USE_WIFI_FAST_CONNECT
|
||||||
bool trying_loaded_ap_{false};
|
bool trying_loaded_ap_{false};
|
||||||
|
#endif
|
||||||
bool retry_hidden_{false};
|
bool retry_hidden_{false};
|
||||||
bool has_ap_{false};
|
bool has_ap_{false};
|
||||||
bool handled_connected_state_{false};
|
bool handled_connected_state_{false};
|
||||||
|
@@ -199,6 +199,7 @@
|
|||||||
#define USE_WEBSERVER_PORT 80 // NOLINT
|
#define USE_WEBSERVER_PORT 80 // NOLINT
|
||||||
#define USE_WEBSERVER_SORTING
|
#define USE_WEBSERVER_SORTING
|
||||||
#define USE_WIFI_11KV_SUPPORT
|
#define USE_WIFI_11KV_SUPPORT
|
||||||
|
#define USE_WIFI_FAST_CONNECT
|
||||||
#define USB_HOST_MAX_REQUESTS 16
|
#define USB_HOST_MAX_REQUESTS 16
|
||||||
|
|
||||||
#ifdef USE_ARDUINO
|
#ifdef USE_ARDUINO
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
wifi:
|
wifi:
|
||||||
|
fast_connect: true
|
||||||
networks:
|
networks:
|
||||||
- ssid: MySSID
|
- ssid: MySSID
|
||||||
eap:
|
eap:
|
||||||
|
Reference in New Issue
Block a user