|
| 1 | +/* |
| 2 | + WiFiMulti.cpp - Choose best RSSI and connect |
| 3 | + Copyright (c) 2022 Earle F. Philhower, III |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +
|
| 19 | + Modified by Ivan Grokhotkov, January 2015 - esp8266 support |
| 20 | +*/ |
| 21 | + |
| 22 | +#include "WiFi.h" |
| 23 | +#include <string.h> |
| 24 | +#include <algorithm> |
| 25 | + |
| 26 | +WiFiMulti::WiFiMulti() { |
| 27 | +} |
| 28 | + |
| 29 | +WiFiMulti::~WiFiMulti() { |
| 30 | + while (!_list.empty()) { |
| 31 | + struct _AP ap = _list.front(); |
| 32 | + _list.pop_front(); |
| 33 | + free(ap.ssid); |
| 34 | + free(ap.pass); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +bool WiFiMulti::addAP(const char *ssid, const char *pass) { |
| 39 | + struct _AP ap; |
| 40 | + ap.ssid = strdup(ssid); |
| 41 | + if (!ap.ssid) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + if (pass) { |
| 45 | + ap.pass = strdup(pass); |
| 46 | + if (!ap.pass) { |
| 47 | + free(ap.ssid); |
| 48 | + return false; |
| 49 | + } |
| 50 | + } else { |
| 51 | + ap.pass = nullptr; |
| 52 | + } |
| 53 | + _list.push_front(ap); |
| 54 | + return true; |
| 55 | +} |
| 56 | + |
| 57 | +uint8_t WiFiMulti::run(uint32_t to) { |
| 58 | + int cnt = WiFi.scanNetworks(); |
| 59 | + if (!cnt) { |
| 60 | + return WL_DISCONNECTED; |
| 61 | + } |
| 62 | + |
| 63 | + // Find the highest RSSI network in our list. Probably more efficient searches, but the list |
| 64 | + // of APs will have < 5 in > 99% of the cases so it's a don't care. |
| 65 | + int maxRSSID = -999; |
| 66 | + std::list<struct _AP>::iterator hit; |
| 67 | + bool found = false; |
| 68 | + for (int i = 0; i < cnt; i++) { |
| 69 | + if (WiFi.RSSI(i) > maxRSSID) { |
| 70 | + for (auto j = _list.begin(); j != _list.end(); j++) { |
| 71 | + if (!strcmp(j->ssid, WiFi.SSID(i))) { |
| 72 | + hit = j; |
| 73 | + maxRSSID = WiFi.RSSI(i); |
| 74 | + found = true; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + if (!found) { |
| 80 | + return WL_DISCONNECTED; |
| 81 | + } |
| 82 | + |
| 83 | + // Connect! |
| 84 | + uint32_t start = millis(); |
| 85 | + WiFi.begin(hit->ssid, hit->pass); |
| 86 | + while (!WiFi.connected() && (millis() - start < to)) { |
| 87 | + delay(5); |
| 88 | + } |
| 89 | + return WiFi.status(); |
| 90 | +} |
0 commit comments