Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
Add WiFi fast connect mode (#385)
Browse files Browse the repository at this point in the history
Fixes #369
  • Loading branch information
OttoWinter committed Jan 13, 2019
1 parent ee75262 commit 371a9f8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/esphomelib/util.cpp
Expand Up @@ -96,10 +96,10 @@ void network_setup_mdns(const std::string &hostname) {
IPAddress network_get_address() {
#ifdef USE_ETHERNET
if (global_eth_component != nullptr)
global_eth_component->get_ip_address();
return global_eth_component->get_ip_address();
#endif
if (global_wifi_component != nullptr)
global_wifi_component->get_ip_address();
return global_wifi_component->get_ip_address();
return IPAddress();
}

Expand Down
16 changes: 14 additions & 2 deletions src/esphomelib/wifi_component.cpp
Expand Up @@ -43,7 +43,12 @@ void WiFiComponent::setup() {
delay(10);

this->wifi_apply_power_save_();
this->start_scanning();

if (this->fast_connect_) {
this->start_connecting(this->sta_[0], false);
} else {
this->start_scanning();
}
} else if (this->has_ap()) {
this->setup_ap_config();
}
Expand All @@ -59,7 +64,11 @@ void WiFiComponent::loop() {
case WIFI_COMPONENT_STATE_COOLDOWN: {
this->status_set_warning();
if (millis() - this->action_started_ > 5000) {
this->start_scanning();
if (this->fast_connect_) {
this->start_connecting(this->sta_[0], false);
} else {
this->start_scanning();
}
}
break;
}
Expand Down Expand Up @@ -109,6 +118,9 @@ bool WiFiComponent::has_ap() const {
bool WiFiComponent::has_sta() const {
return !this->sta_.empty();
}
void WiFiComponent::set_fast_connect(bool fast_connect) {
this->fast_connect_ = fast_connect;
}
IPAddress WiFiComponent::get_ip_address() {
if (this->has_sta())
return this->wifi_sta_ip_();
Expand Down
4 changes: 3 additions & 1 deletion src/esphomelib/wifi_component.h
Expand Up @@ -36,7 +36,7 @@ enum WiFiComponentState {
* */
WIFI_COMPONENT_STATE_STA_CONNECTING_2,
/** WiFi is in STA(+AP) mode and successfully connected. */
WIFI_COMPONENT_STATE_STA_CONNECTED,
WIFI_COMPONENT_STATE_STA_CONNECTED,
// Any state below here is a valid state for continuing
/** WiFi is in AP-only mode and internal AP is already enabled. */
WIFI_COMPONENT_STATE_AP,
Expand Down Expand Up @@ -134,6 +134,7 @@ class WiFiComponent : public Component {
void start_scanning();
void check_scanning_finished();
void start_connecting(const WiFiAP &ap, bool two);
void set_fast_connect(bool fast_connect);

void check_connecting_finished();

Expand Down Expand Up @@ -211,6 +212,7 @@ class WiFiComponent : public Component {

std::vector<WiFiAP> sta_;
WiFiAP selected_ap_;
bool fast_connect_{false};

WiFiAP ap_;
WiFiComponentState state_{WIFI_COMPONENT_STATE_OFF};
Expand Down

0 comments on commit 371a9f8

Please sign in to comment.