1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-03 03:42:20 +01:00

WiFi networks priority (#658)

* WiFi networks priority

Fixes https://github.com/esphome/feature-requests/issues/136

* Print priority
This commit is contained in:
Otto Winter
2019-07-03 20:42:46 +02:00
committed by GitHub
parent 6516a6ff7e
commit 1876c21e3e
3 changed files with 60 additions and 1 deletions

View File

@@ -273,6 +273,9 @@ void WiFiComponent::print_connect_params_() {
int8_t rssi = WiFi.RSSI();
print_signal_bars(rssi, signal_bars);
ESP_LOGCONFIG(TAG, " Signal strength: %d dB %s", rssi, signal_bars);
if (this->selected_ap_.get_bssid().has_value()) {
ESP_LOGV(TAG, " Priority: %.1f", this->get_sta_priority(*this->selected_ap_.get_bssid()));
}
ESP_LOGCONFIG(TAG, " Channel: %d", WiFi.channel());
ESP_LOGCONFIG(TAG, " Subnet: %s", WiFi.subnetMask().toString().c_str());
ESP_LOGCONFIG(TAG, " Gateway: %s", WiFi.gatewayIP().toString().c_str());
@@ -308,6 +311,10 @@ void WiFiComponent::check_scanning_finished() {
for (auto &ap : this->sta_) {
if (res.matches(ap)) {
res.set_matches(true);
if (!this->has_sta_priority(res.get_bssid())) {
this->set_sta_priority(res.get_bssid(), ap.get_priority());
}
res.set_priority(this->get_sta_priority(res.get_bssid()));
break;
}
}
@@ -315,11 +322,18 @@ void WiFiComponent::check_scanning_finished() {
std::stable_sort(this->scan_result_.begin(), this->scan_result_.end(),
[](const WiFiScanResult &a, const WiFiScanResult &b) {
// return true if a is better than b
if (a.get_matches() && !b.get_matches())
return true;
if (!a.get_matches() && b.get_matches())
return false;
if (a.get_matches() && b.get_matches()) {
// if both match, check priority
if (a.get_priority() != b.get_priority())
return a.get_priority() > b.get_priority();
}
return a.get_rssi() > b.get_rssi();
});
@@ -443,6 +457,12 @@ void WiFiComponent::check_connecting_finished() {
}
void WiFiComponent::retry_connect() {
if (this->selected_ap_.get_bssid()) {
auto bssid = *this->selected_ap_.get_bssid();
float priority = this->get_sta_priority(bssid);
this->set_sta_priority(bssid, priority - 1.0f);
}
delay(10);
if (!this->is_captive_portal_active_() && (this->num_retried_ > 5 || this->error_from_callback_)) {
// If retry failed for more than 5 times, let's restart STA