diff --git a/src/main.cpp b/src/main.cpp index 4ee9018f..727f373a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -99,6 +99,56 @@ void extraLoop() #endif } +#ifdef ARDUINO_ARCH_ESP8266 +void get_wifi_bssid(const char *ssid, uint8_t *bssid, uint32_t *wifi_channel) +{ + bssid = nullptr; + int n = WiFi.scanNetworks(false, true); + + if (n < 1) // no networks found + return; + + // sort networks on RSSI value + int indices[n]; + for (int i = 0; i < n; i++) + { + indices[i] = i; + } + + for (int i = 0; i < n; i++) + { + for (int j = i + 1; j < n; j++) + { + if (WiFi.RSSI(indices[j]) > WiFi.RSSI(indices[i])) + { + std::swap(indices[i], indices[j]); + } + } + } + + // loop through result and match highest RSSI SSID + for (int i = 0; i < n; i++) + { + char scan_ssid[33]; // ssid can be up to 32chars, => plus null term + strlcpy(scan_ssid, WiFi.SSID(indices[i]).c_str(), sizeof(scan_ssid)); + + if (strcmp(ssid, scan_ssid) == 0) + { + if (WiFi.BSSID(indices[i]) != 0) + { + bssid = WiFi.BSSID(indices[i]); + *wifi_channel = WiFi.channel(); + return; + } + else + { + return; + } + } + } +} +#endif + void checkWifi() { int i = 0; @@ -142,7 +192,23 @@ void setup_wifi() } #endif - WiFi.begin(WIFI_SSID, WIFI_PWD, 0,0,true); + uint8_t *bssid = nullptr; + uint32_t wifi_channel = 0; +#ifdef ARDUINO_ARCH_ESP8266 + get_wifi_bssid(WIFI_SSID, bssid, &wifi_channel); +#else //assume ESP32 + WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL); + WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN); +#endif + + if (bssid != nullptr) + { + WiFi.begin(WIFI_SSID, WIFI_PWD, wifi_channel, bssid); + } + else + { + WiFi.begin(WIFI_SSID, WIFI_PWD, 0, 0, true); + } checkWifi(); mqttSerial.printf("Connected. IP Address: %s\n", WiFi.localIP().toString().c_str()); }