Skip to content

Sorting of scanned wifi networks #661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Wippersnapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ typedef enum {
FSM_NET_ESTABLISH_MQTT,
} fsm_net_t;

#define WS_WDT_TIMEOUT 60000 ///< WDT timeout
#define WS_MAX_ALT_WIFI_NETWORKS 3 ///< Maximum number of alternative networks
#define WS_WDT_TIMEOUT 60000 ///< WDT timeout
/* MQTT Configuration */
#define WS_KEEPALIVE_INTERVAL_MS \
5000 ///< Session keepalive interval time, in milliseconds
Expand Down
91 changes: 48 additions & 43 deletions src/Wippersnapper_Networking.h
Original file line number Diff line number Diff line change
@@ -1,44 +1,49 @@
/*!
* @file Wippersnapper_Networking.h
*
* This file includes network interfaces at compile-time.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Brent Rubell 2020-2021 for Adafruit Industries.
*
* BSD license, all text here must be included in any redistribution.
*
*/

#ifndef WIPPERSNAPPER_NETWORKING_H
#define WIPPERSNAPPER_NETWORKING_H

#if defined(ADAFRUIT_METRO_M4_EXPRESS) || \
defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) || defined(ADAFRUIT_PYPORTAL) || \
defined(ADAFRUIT_PYPORTAL_M4_TITANO) || defined(USE_AIRLIFT)
#include "network_interfaces/Wippersnapper_AIRLIFT.h"
/** Nina-FW (adafruit fork) networking class */
typedef Wippersnapper_AIRLIFT Wippersnapper_WiFi;
#elif defined(ARDUINO_ARCH_ESP8266)
#include "network_interfaces/Wippersnapper_ESP8266.h"
/** ESP8266's networking class */
typedef Wippersnapper_ESP8266 Wippersnapper_WiFi;
#elif defined(ARDUINO_ARCH_ESP32)
#include "network_interfaces/Wippersnapper_ESP32.h"
/** ESP32's networking class */
typedef Wippersnapper_ESP32 Wippersnapper_WiFi;
#elif defined(ARDUINO_ARCH_RP2040)
#include "network_interfaces/ws_networking_pico.h"
typedef ws_networking_pico Wippersnapper_WiFi;
#elif defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_SAMD_MKRWIFI1010)
/** Nina-FW (arduino) networking class */
#include "network_interfaces/Wippersnapper_WIFININA.h"
typedef Wippersnapper_WIFININA Wippersnapper_WiFi;
#else
#warning "Must define network interface in config.h!"
#endif

/*!
* @file Wippersnapper_Networking.h
*
* This file includes network interfaces at compile-time.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Brent Rubell 2020-2021 for Adafruit Industries.
*
* BSD license, all text here must be included in any redistribution.
*
*/

#ifndef WIPPERSNAPPER_NETWORKING_H
#define WIPPERSNAPPER_NETWORKING_H

#ifndef WL_MAC_ADDR_LENGTH
#define WL_MAC_ADDR_LENGTH 6 ///< MAC address length - from RP2040 BSP
#endif
#define WS_MAX_ALT_WIFI_NETWORKS 3 ///< Maximum number of alternative networks

#if defined(ADAFRUIT_METRO_M4_EXPRESS) || \
defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) || defined(ADAFRUIT_PYPORTAL) || \
defined(ADAFRUIT_PYPORTAL_M4_TITANO) || defined(USE_AIRLIFT)
#include "network_interfaces/Wippersnapper_AIRLIFT.h"
/** Nina-FW (adafruit fork) networking class */
typedef Wippersnapper_AIRLIFT Wippersnapper_WiFi;
#elif defined(ARDUINO_ARCH_ESP8266)
#include "network_interfaces/Wippersnapper_ESP8266.h"
/** ESP8266's networking class */
typedef Wippersnapper_ESP8266 Wippersnapper_WiFi;
#elif defined(ARDUINO_ARCH_ESP32)
#include "network_interfaces/Wippersnapper_ESP32.h"
/** ESP32's networking class */
typedef Wippersnapper_ESP32 Wippersnapper_WiFi;
#elif defined(ARDUINO_ARCH_RP2040)
#include "network_interfaces/ws_networking_pico.h"
typedef ws_networking_pico Wippersnapper_WiFi;
#elif defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_SAMD_MKRWIFI1010)
/** Nina-FW (arduino) networking class */
#include "network_interfaces/Wippersnapper_WIFININA.h"
typedef Wippersnapper_WIFININA Wippersnapper_WiFi;
#else
#warning "Must define network interface in config.h!"
#endif

#endif // WIPPERSNAPPER_NETWORKING_H
42 changes: 25 additions & 17 deletions src/network_interfaces/Wippersnapper_AIRLIFT.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ class Wippersnapper_AIRLIFT : public Wippersnapper {

/***********************************************************/
/*!
@brief Performs a scan of local WiFi networks.
@returns True if `_network_ssid` is found, False otherwise.
@brief Performs a scan of local WiFi networks.
@returns True if `_network_ssid` is found, False otherwise.
*/
/***********************************************************/
bool check_valid_ssid() {
Expand All @@ -119,26 +119,33 @@ class Wippersnapper_AIRLIFT : public Wippersnapper {
return false;
}

// Was the network within secrets.json found?
for (int i = 0; i < n; ++i) {
if (strcmp(_ssid, WiFi.SSID(i)) == 0) {
WS_DEBUG_PRINT("SSID found! RSSI: ");
WS_DEBUG_PRINTLN(WiFi.RSSI(i));
return true;
}
}
bool foundNetwork = false;

// User-set network not found, print scan results to serial console
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: ");
for (int i = 0; i < n; ++i) {
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks:");
for (uint8_t i = 0; i < n; i++) {
if (!foundNetwork && strcmp(WiFi.SSID(i), _ssid) == 0) {
foundNetwork = true;
}
WS_DEBUG_PRINT(WiFi.SSID(i));
WS_DEBUG_PRINT(" ");
WS_DEBUG_PRINT(" (");
uint8_t BSSID[WL_MAC_ADDR_LENGTH];
WiFi.BSSID(i, BSSID);
for (int m = 0; m < WL_MAC_ADDR_LENGTH; m++) {
if (m != 0)
WS_DEBUG_PRINT(":");
WS_DEBUG_PRINTHEX(BSSID[m]);
}
WS_DEBUG_PRINT(") ");
WS_DEBUG_PRINT(WiFi.RSSI(i));
WS_DEBUG_PRINTLN("dB");
WS_DEBUG_PRINT("dB (ch");
WS_DEBUG_PRINT(WiFi.channel(i))
WS_DEBUG_PRINTLN(")");
}

return false;
if (!foundNetwork) {
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
}
return foundNetwork;
}

/********************************************************/
Expand Down Expand Up @@ -323,6 +330,7 @@ class Wippersnapper_AIRLIFT : public Wippersnapper {

// validate co-processor's firmware version
if (!firmwareCheck()) {
// TODO: see if there's a way to add to bootlog without usb reattach
WS_DEBUG_PRINTLN("Please upgrade the firmware on the ESP module to the "
"latest version.");
}
Expand Down
48 changes: 24 additions & 24 deletions src/network_interfaces/Wippersnapper_ESP32.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,40 +120,40 @@ class Wippersnapper_ESP32 : public Wippersnapper {
return false;
}

// Was the network within secrets.json found?
for (int i = 0; i < n; ++i) {
if (strcmp(_ssid, WiFi.SSID(i).c_str()) == 0) {
WS_DEBUG_PRINT("SSID (");
WS_DEBUG_PRINT(_ssid);
WS_DEBUG_PRINT(") found! RSSI: ");
WS_DEBUG_PRINTLN(WiFi.RSSI(i));
return true;
}
if (WS._isWiFiMulti) {
bool foundNetwork = false;

WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks:");
for (uint8_t i = 0; i < n; i++) {
if (!foundNetwork && strcmp(WiFi.SSID(i).c_str(), _ssid) == 0) {
foundNetwork = true;
} else if (!foundNetwork && WS._isWiFiMulti) {
// multi network mode
for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) {
if (strcmp(WS._multiNetworks[j].ssid, WiFi.SSID(i).c_str()) == 0) {
WS_DEBUG_PRINT("SSID (");
WS_DEBUG_PRINT(WS._multiNetworks[j].ssid);
WS_DEBUG_PRINT(") found! RSSI: ");
WS_DEBUG_PRINTLN(WiFi.RSSI(i));
return true;
foundNetwork = true;
}
}
}
}

// User-set network not found, print scan results to serial console
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: ");
for (int i = 0; i < n; ++i) {
WS_DEBUG_PRINT(WiFi.SSID(i));
WS_DEBUG_PRINT(" ");
WS_DEBUG_PRINT(" (");
uint8_t BSSID[WL_MAC_ADDR_LENGTH];
WiFi.BSSID(i, BSSID);
for (int m = 0; m < WL_MAC_ADDR_LENGTH; m++) {
if (m != 0)
WS_DEBUG_PRINT(":");
WS_DEBUG_PRINTHEX(BSSID[m]);
}
WS_DEBUG_PRINT(") ");
WS_DEBUG_PRINT(WiFi.RSSI(i));
WS_DEBUG_PRINTLN("dB");
WS_DEBUG_PRINT("dB (ch");
WS_DEBUG_PRINT(WiFi.channel(i))
WS_DEBUG_PRINTLN(")");
}

return false;
if (!foundNetwork) {
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
}
return foundNetwork;
}

/********************************************************/
Expand Down
99 changes: 44 additions & 55 deletions src/network_interfaces/Wippersnapper_ESP8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,40 +133,40 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
return false;
}

// Was the network within secrets.json found?
for (int i = 0; i < n; ++i) {
if (strcmp(_ssid, WiFi.SSID(i).c_str()) == 0) {
WS_DEBUG_PRINT("SSID (");
WS_DEBUG_PRINT(_ssid);
WS_DEBUG_PRINT(") found! RSSI: ");
WS_DEBUG_PRINTLN(WiFi.RSSI(i));
return true;
}
if (WS._isWiFiMulti) {
bool foundNetwork = false;

WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks:");
for (uint8_t i = 0; i < n; i++) {
if (!foundNetwork && strcmp(WiFi.SSID(i).c_str(), _ssid) == 0) {
foundNetwork = true;
} else if (!foundNetwork && WS._isWiFiMulti) {
// multi network mode
for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) {
if (strcmp(WS._multiNetworks[j].ssid, WiFi.SSID(i).c_str()) == 0) {
WS_DEBUG_PRINT("SSID (");
WS_DEBUG_PRINT(WS._multiNetworks[j].ssid);
WS_DEBUG_PRINT(") found! RSSI: ");
WS_DEBUG_PRINTLN(WiFi.RSSI(i));
return true;
foundNetwork = true;
}
}
}
}

// User-set network not found, print scan results to serial console
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: ");
for (int i = 0; i < n; ++i) {
WS_DEBUG_PRINT(WiFi.SSID(i));
WS_DEBUG_PRINT(" ");
WS_DEBUG_PRINT(" (");
uint8_t BSSID[WL_MAC_ADDR_LENGTH];
memcpy(BSSID, WiFi.BSSID(i), WL_MAC_ADDR_LENGTH);
for (int m = 0; m < WL_MAC_ADDR_LENGTH; m++) {
if (m != 0)
WS_DEBUG_PRINT(":");
WS_DEBUG_PRINTHEX(BSSID[m]);
}
WS_DEBUG_PRINT(") ");
WS_DEBUG_PRINT(WiFi.RSSI(i));
WS_DEBUG_PRINTLN("dB");
WS_DEBUG_PRINT("dB (ch");
WS_DEBUG_PRINT(WiFi.channel(i))
WS_DEBUG_PRINTLN(")");
}

return false;
if (!foundNetwork) {
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
}
return foundNetwork;
}

/********************************************************/
Expand Down Expand Up @@ -260,7 +260,6 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
delay(100);
// ESP8266 MUST be in STA mode to avoid device acting as client/server
WiFi.mode(WIFI_STA);
WiFi.begin(_ssid, _pass);
_status = WS_NET_DISCONNECTED;
delay(100);

Expand All @@ -274,38 +273,28 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
WS._multiNetworks[i].pass);
}
}
// add default network
if (_wifiMulti.existsAP(_ssid) == false) {
_wifiMulti.addAP(_ssid, _pass);
}
long startRetry = millis();
WS_DEBUG_PRINTLN("CONNECTING");
while (_wifiMulti.run(5000) != WL_CONNECTED &&
millis() - startRetry < 10000) {
// ESP8266 WDT requires yield() during a busy-loop so it doesn't bite
yield();
}
if (WiFi.status() == WL_CONNECTED) {
_status = WS_NET_CONNECTED;
} else {
_status = WS_NET_DISCONNECTED;
}
}

// add default network
if (_wifiMulti.existsAP(_ssid) == false) {
_wifiMulti.addAP(_ssid, _pass);
}

long startRetry = millis();
WS_DEBUG_PRINTLN("CONNECTING");

while (_wifiMulti.run(5000) != WL_CONNECTED &&
millis() - startRetry < 10000) {
// ESP8266 WDT requires yield() during a busy-loop so it doesn't bite
yield();
}

if (WiFi.status() == WL_CONNECTED) {
_status = WS_NET_CONNECTED;
} else {
// single network mode

// wait for a connection to be established
long startRetry = millis();
WS_DEBUG_PRINTLN("CONNECTING");
while (WiFi.status() != WL_CONNECTED && millis() - startRetry < 10000) {
// ESP8266 WDT requires yield() during a busy-loop so it doesn't bite
yield();
}
if (WiFi.status() == WL_CONNECTED) {
_status = WS_NET_CONNECTED;
} else {
_status = WS_NET_DISCONNECTED;
}
_status = WS_NET_DISCONNECTED;
}

WS.feedWDT();
}
}
Expand Down
Loading
Loading