From 513de11231dc2c98289893d516724176bf47c84d Mon Sep 17 00:00:00 2001 From: tyeth Date: Wed, 13 Nov 2024 18:35:15 +0000 Subject: [PATCH 01/30] Update network scan to sort by RSSI --- .../Wippersnapper_AIRLIFT.h | 66 ++++++++++++----- src/network_interfaces/Wippersnapper_ESP32.h | 72 +++++++++++++------ .../Wippersnapper_ESP8266.h | 45 +++++++++--- .../Wippersnapper_WIFININA.h | 61 ++++++++++++---- src/network_interfaces/ws_networking_pico.h | 55 +++++++++++--- 5 files changed, 224 insertions(+), 75 deletions(-) diff --git a/src/network_interfaces/Wippersnapper_AIRLIFT.h b/src/network_interfaces/Wippersnapper_AIRLIFT.h index 4c564b4e1..c3817e9f1 100644 --- a/src/network_interfaces/Wippersnapper_AIRLIFT.h +++ b/src/network_interfaces/Wippersnapper_AIRLIFT.h @@ -23,6 +23,8 @@ #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" #include "Arduino.h" +#include +#include #include "SPI.h" #include "WiFiNINA.h" #include "Wippersnapper.h" @@ -101,13 +103,24 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { _pass = WS._config.network.pass; } - /***********************************************************/ - /*! +// Define a structure to hold network information +struct WiFiNetwork { + char ssid[33]; // Maximum SSID length is 32 characters + null terminator + int rssi; +}; + +// Comparison function to sort by RSSI in descending order +bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) { + return a.rssi > b.rssi; +} + +/***********************************************************/ +/*! @brief Performs a scan of local WiFi networks. @returns True if `_network_ssid` is found, False otherwise. - */ - /***********************************************************/ - bool check_valid_ssid() { +*/ +/***********************************************************/ +bool check_valid_ssid() { // Disconnect WiFi from an AP if it was previously connected WiFi.disconnect(); delay(100); @@ -115,31 +128,46 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { // Perform a network scan int n = WiFi.scanNetworks(); if (n == 0) { - WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!"); - return false; + WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!"); + return false; } - // Was the network within secrets.json found? + // Dynamically allocate memory for the network list + std::vector networks(n); + + // Store the scanned networks in the vector 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; - } + strncpy(networks[i].ssid, WiFi.SSID(i).c_str(), sizeof(networks[i].ssid) - 1); + networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; // Ensure null termination + networks[i].rssi = WiFi.RSSI(i); + } + + // Sort the networks by RSSI in descending order + std::sort(networks.begin(), networks.end(), compareByRSSI); + + // Was the network within secrets.json found? + for (const auto &network : networks) { + if (strcmp(_ssid, network.ssid) == 0) { + WS_DEBUG_PRINT("SSID ("); + WS_DEBUG_PRINT(_ssid); + WS_DEBUG_PRINT(") found! RSSI: "); + WS_DEBUG_PRINTLN(network.rssi); + return 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(WiFi.RSSI(i)); - WS_DEBUG_PRINTLN("dB"); + for (const auto &network : networks) { + WS_DEBUG_PRINT(network.ssid); + WS_DEBUG_PRINT(" "); + WS_DEBUG_PRINT(network.rssi); + WS_DEBUG_PRINTLN("dB"); } return false; - } +} /********************************************************/ /*! diff --git a/src/network_interfaces/Wippersnapper_ESP32.h b/src/network_interfaces/Wippersnapper_ESP32.h index 99c4d47be..d7e2a7dd1 100644 --- a/src/network_interfaces/Wippersnapper_ESP32.h +++ b/src/network_interfaces/Wippersnapper_ESP32.h @@ -23,6 +23,8 @@ #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" #include "Arduino.h" +#include +#include #include "WiFi.h" #include "WiFiMulti.h" #include @@ -90,6 +92,17 @@ class Wippersnapper_ESP32 : public Wippersnapper { _pass = WS._config.network.pass; } + // Define a structure to hold network information + struct WiFiNetwork { + char ssid[33]; // Maximum SSID length is 32 characters + null terminator + int rssi; + }; + + // Comparison function to sort by RSSI in descending order + bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) { + return a.rssi > b.rssi; + } + /***********************************************************/ /*! @brief Performs a scan of local WiFi networks. @@ -116,41 +129,54 @@ class Wippersnapper_ESP32 : public Wippersnapper { // Perform a network scan int n = WiFi.scanNetworks(); if (n == 0) { - WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!"); - return false; + WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!"); + return false; } - // Was the network within secrets.json found? + // Dynamically allocate memory for the network list + std::vector networks(n); + + // Store the scanned networks in the vector 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) { - // 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) { + strncpy(networks[i].ssid, WiFi.SSID(i).c_str(), sizeof(networks[i].ssid) - 1); + networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; // Ensure null termination + networks[i].rssi = WiFi.RSSI(i); + } + + // Sort the networks by RSSI in descending order + std::sort(networks.begin(), networks.end(), compareByRSSI); + + // Was the network within secrets.json found? + for (const auto &network : networks) { + if (strcmp(_ssid, network.ssid) == 0) { WS_DEBUG_PRINT("SSID ("); - WS_DEBUG_PRINT(WS._multiNetworks[j].ssid); + WS_DEBUG_PRINT(_ssid); WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(WiFi.RSSI(i)); + WS_DEBUG_PRINTLN(network.rssi); return true; - } } - } + if (WS._isWiFiMulti) { + // multi network mode + for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) { + if (strcmp(WS._multiNetworks[j].ssid, network.ssid) == 0) { + WS_DEBUG_PRINT("SSID ("); + WS_DEBUG_PRINT(WS._multiNetworks[j].ssid); + WS_DEBUG_PRINT(") found! RSSI: "); + WS_DEBUG_PRINTLN(network.rssi); + return 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(WiFi.RSSI(i)); - WS_DEBUG_PRINTLN("dB"); + for (const auto &network : networks) { + WS_DEBUG_PRINT(network.ssid); + WS_DEBUG_PRINT(" "); + WS_DEBUG_PRINT(network.rssi); + WS_DEBUG_PRINTLN("dB"); } return false; diff --git a/src/network_interfaces/Wippersnapper_ESP8266.h b/src/network_interfaces/Wippersnapper_ESP8266.h index c7fa6b693..8d250fb51 100644 --- a/src/network_interfaces/Wippersnapper_ESP8266.h +++ b/src/network_interfaces/Wippersnapper_ESP8266.h @@ -18,6 +18,8 @@ #define WIPPERSNAPPER_ESP8266_H #ifdef ARDUINO_ARCH_ESP8266 +#include +#include #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" #include "ESP8266WiFi.h" @@ -113,6 +115,19 @@ class Wippersnapper_ESP8266 : public Wippersnapper { _pass = WS._config.network.pass; } + // Define a structure to hold network information + struct WiFiNetwork + { + char ssid[33]; // Maximum SSID length is 32 characters + null terminator + int rssi; + }; + + // Comparison function to sort by RSSI in descending order + bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) + { + return a.rssi > b.rssi; + } + /***********************************************************/ /*! @brief Performs a scan of local WiFi networks. @@ -133,23 +148,36 @@ class Wippersnapper_ESP8266 : public Wippersnapper { return false; } - // Was the network within secrets.json found? + // Dynamically allocate memory for the network list + std::vector networks(n); + + // Store the scanned networks in the vector for (int i = 0; i < n; ++i) { - if (strcmp(_ssid, WiFi.SSID(i).c_str()) == 0) { + strncpy(networks[i].ssid, WiFi.SSID(i).c_str(), sizeof(networks[i].ssid) - 1); + networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; // Ensure null termination + networks[i].rssi = WiFi.RSSI(i); + } + + // Sort the networks by RSSI in descending order + std::sort(networks.begin(), networks.end(), compareByRSSI); + + // Was the network within secrets.json found? + for (const auto &network : networks) { + if (strcmp(_ssid, network.ssid) == 0) { WS_DEBUG_PRINT("SSID ("); WS_DEBUG_PRINT(_ssid); WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(WiFi.RSSI(i)); + WS_DEBUG_PRINTLN(network.rssi); return true; } if (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) { + if (strcmp(WS._multiNetworks[j].ssid, network.ssid) == 0) { WS_DEBUG_PRINT("SSID ("); WS_DEBUG_PRINT(WS._multiNetworks[j].ssid); WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(WiFi.RSSI(i)); + WS_DEBUG_PRINTLN(network.rssi); return true; } } @@ -159,10 +187,11 @@ class Wippersnapper_ESP8266 : public Wippersnapper { // 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)); + for (const auto &network : networks) + { + WS_DEBUG_PRINT(network.ssid); WS_DEBUG_PRINT(" "); - WS_DEBUG_PRINT(WiFi.RSSI(i)); + WS_DEBUG_PRINT(network.rssi); WS_DEBUG_PRINTLN("dB"); } diff --git a/src/network_interfaces/Wippersnapper_WIFININA.h b/src/network_interfaces/Wippersnapper_WIFININA.h index 74f50bffe..10c845f8b 100644 --- a/src/network_interfaces/Wippersnapper_WIFININA.h +++ b/src/network_interfaces/Wippersnapper_WIFININA.h @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include #include @@ -109,10 +111,24 @@ class Wippersnapper_WIFININA : public Wippersnapper { strlcpy(WS._config.network.pass, _pass, sizeof(WS._config.network.pass)); } + + // Define a structure to hold network information + struct WiFiNetwork + { + char ssid[33]; // Maximum SSID length is 32 characters + null terminator + int rssi; + }; + + // Comparison function to sort by RSSI in descending order + bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) + { + return a.rssi > b.rssi; + } + /***********************************************************/ /*! - @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() { @@ -124,27 +140,42 @@ class Wippersnapper_WIFININA : public Wippersnapper { // Perform a network scan int n = WiFi.scanNetworks(); if (n == 0) { - WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!"); - return false; + WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!"); + return false; } - // Was the network within secrets.json found? + // Dynamically allocate memory for the network list + std::vector networks(n); + + // Store the scanned networks in the vector 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; - } + strncpy(networks[i].ssid, WiFi.SSID(i).c_str(), sizeof(networks[i].ssid) - 1); + networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; // Ensure null termination + networks[i].rssi = WiFi.RSSI(i); + } + + // Sort the networks by RSSI in descending order + std::sort(networks.begin(), networks.end(), compareByRSSI); + + // Was the network within secrets.json found? + for (const auto &network : networks) { + if (strcmp(_ssid, network.ssid) == 0) { + WS_DEBUG_PRINT("SSID ("); + WS_DEBUG_PRINT(_ssid); + WS_DEBUG_PRINT(") found! RSSI: "); + WS_DEBUG_PRINTLN(network.rssi); + return 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(WiFi.RSSI(i)); - WS_DEBUG_PRINTLN("dB"); + for (const auto &network : networks) { + WS_DEBUG_PRINT(network.ssid); + WS_DEBUG_PRINT(" "); + WS_DEBUG_PRINT(network.rssi); + WS_DEBUG_PRINTLN("dB"); } return false; diff --git a/src/network_interfaces/ws_networking_pico.h b/src/network_interfaces/ws_networking_pico.h index 25ce278b1..66ed5bf4d 100644 --- a/src/network_interfaces/ws_networking_pico.h +++ b/src/network_interfaces/ws_networking_pico.h @@ -27,6 +27,8 @@ #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" #include "Arduino.h" +#include +#include #include #include extern Wippersnapper WS; @@ -92,6 +94,19 @@ class ws_networking_pico : public Wippersnapper { _pass = WS._config.network.pass; } + // Define a structure to hold network information + struct WiFiNetwork + { + char ssid[33]; // Maximum SSID length is 32 characters + null terminator + int rssi; + }; + + // Comparison function to sort by RSSI in descending order + bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) + { + return a.rssi > b.rssi; + } + /***********************************************************/ /*! @brief Performs a scan of local WiFi networks. @@ -112,23 +127,42 @@ class ws_networking_pico : public Wippersnapper { return false; } + // Dynamically allocate memory for the network list + std::vector networks(n); + + // Store the scanned networks in the vector + for (int i = 0; i < n; ++i) + { + strncpy(networks[i].ssid, WiFi.SSID(i).c_str(), sizeof(networks[i].ssid) - 1); + networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; // Ensure null termination + networks[i].rssi = WiFi.RSSI(i); + } + + // Sort the networks by RSSI in descending order + std::sort(networks.begin(), networks.end(), compareByRSSI); + // Was the network within secrets.json found? - for (int i = 0; i < n; ++i) { - if (strcmp(_ssid, WiFi.SSID(i)) == 0) { + for (const auto &network : networks) + { + if (strcmp(_ssid, network.ssid) == 0) + { WS_DEBUG_PRINT("SSID ("); WS_DEBUG_PRINT(_ssid); WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(WiFi.RSSI(i)); + WS_DEBUG_PRINTLN(network.rssi); return true; } - if (WS._isWiFiMulti) { + if (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)) == 0) { + for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) + { + if (strcmp(WS._multiNetworks[j].ssid, network.ssid) == 0) + { WS_DEBUG_PRINT("SSID ("); WS_DEBUG_PRINT(WS._multiNetworks[j].ssid); WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(WiFi.RSSI(i)); + WS_DEBUG_PRINTLN(network.rssi); return true; } } @@ -138,10 +172,11 @@ class ws_networking_pico : public Wippersnapper { // 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)); + for (const auto &network : networks) + { + WS_DEBUG_PRINT(network.ssid); WS_DEBUG_PRINT(" "); - WS_DEBUG_PRINT(WiFi.RSSI(i)); + WS_DEBUG_PRINT(network.rssi); WS_DEBUG_PRINTLN("dB"); } From a5cd10fc7ed4c28e8bb06626bb0d80679abb45b2 Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 14 Nov 2024 15:02:55 +0000 Subject: [PATCH 02/30] correct ssid access for some platforms --- src/network_interfaces/Wippersnapper_AIRLIFT.h | 2 +- src/network_interfaces/Wippersnapper_WIFININA.h | 2 +- src/network_interfaces/ws_networking_pico.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/network_interfaces/Wippersnapper_AIRLIFT.h b/src/network_interfaces/Wippersnapper_AIRLIFT.h index c3817e9f1..07d04b1bf 100644 --- a/src/network_interfaces/Wippersnapper_AIRLIFT.h +++ b/src/network_interfaces/Wippersnapper_AIRLIFT.h @@ -137,7 +137,7 @@ bool check_valid_ssid() { // Store the scanned networks in the vector for (int i = 0; i < n; ++i) { - strncpy(networks[i].ssid, WiFi.SSID(i).c_str(), sizeof(networks[i].ssid) - 1); + strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1); networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; // Ensure null termination networks[i].rssi = WiFi.RSSI(i); } diff --git a/src/network_interfaces/Wippersnapper_WIFININA.h b/src/network_interfaces/Wippersnapper_WIFININA.h index 10c845f8b..d891b8a41 100644 --- a/src/network_interfaces/Wippersnapper_WIFININA.h +++ b/src/network_interfaces/Wippersnapper_WIFININA.h @@ -149,7 +149,7 @@ class Wippersnapper_WIFININA : public Wippersnapper { // Store the scanned networks in the vector for (int i = 0; i < n; ++i) { - strncpy(networks[i].ssid, WiFi.SSID(i).c_str(), sizeof(networks[i].ssid) - 1); + strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1); networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; // Ensure null termination networks[i].rssi = WiFi.RSSI(i); } diff --git a/src/network_interfaces/ws_networking_pico.h b/src/network_interfaces/ws_networking_pico.h index 66ed5bf4d..79ad8847f 100644 --- a/src/network_interfaces/ws_networking_pico.h +++ b/src/network_interfaces/ws_networking_pico.h @@ -133,7 +133,7 @@ class ws_networking_pico : public Wippersnapper { // Store the scanned networks in the vector for (int i = 0; i < n; ++i) { - strncpy(networks[i].ssid, WiFi.SSID(i).c_str(), sizeof(networks[i].ssid) - 1); + strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1); networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; // Ensure null termination networks[i].rssi = WiFi.RSSI(i); } From 34a4834d9069ce788b947ac3192cfc8b855896b3 Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 14 Nov 2024 15:42:49 +0000 Subject: [PATCH 03/30] clang format "sort networks by RSSI" --- .../Wippersnapper_AIRLIFT.h | 65 ++++++++++--------- src/network_interfaces/Wippersnapper_ESP32.h | 58 +++++++++-------- .../Wippersnapper_ESP8266.h | 19 +++--- .../Wippersnapper_WIFININA.h | 44 ++++++------- src/network_interfaces/ws_networking_pico.h | 34 ++++------ 5 files changed, 106 insertions(+), 114 deletions(-) diff --git a/src/network_interfaces/Wippersnapper_AIRLIFT.h b/src/network_interfaces/Wippersnapper_AIRLIFT.h index 07d04b1bf..c9f160f9f 100644 --- a/src/network_interfaces/Wippersnapper_AIRLIFT.h +++ b/src/network_interfaces/Wippersnapper_AIRLIFT.h @@ -23,11 +23,11 @@ #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" #include "Arduino.h" -#include -#include #include "SPI.h" #include "WiFiNINA.h" #include "Wippersnapper.h" +#include +#include #define NINAFWVER \ "1.7.7" /*!< min. nina-fw version compatible with this library. */ @@ -103,24 +103,24 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { _pass = WS._config.network.pass; } -// Define a structure to hold network information -struct WiFiNetwork { + // Define a structure to hold network information + struct WiFiNetwork { char ssid[33]; // Maximum SSID length is 32 characters + null terminator int rssi; -}; + }; -// Comparison function to sort by RSSI in descending order -bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) { + // Comparison function to sort by RSSI in descending order + bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) { return a.rssi > b.rssi; -} + } -/***********************************************************/ -/*! - @brief Performs a scan of local WiFi networks. - @returns True if `_network_ssid` is found, False otherwise. -*/ -/***********************************************************/ -bool check_valid_ssid() { + /***********************************************************/ + /*! + @brief Performs a scan of local WiFi networks. + @returns True if `_network_ssid` is found, False otherwise. + */ + /***********************************************************/ + bool check_valid_ssid() { // Disconnect WiFi from an AP if it was previously connected WiFi.disconnect(); delay(100); @@ -128,8 +128,8 @@ bool check_valid_ssid() { // Perform a network scan int n = WiFi.scanNetworks(); if (n == 0) { - WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!"); - return false; + WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!"); + return false; } // Dynamically allocate memory for the network list @@ -137,9 +137,10 @@ bool check_valid_ssid() { // Store the scanned networks in the vector for (int i = 0; i < n; ++i) { - strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1); - networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; // Ensure null termination - networks[i].rssi = WiFi.RSSI(i); + strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1); + networks[i].ssid[sizeof(networks[i].ssid) - 1] = + '\0'; // Ensure null termination + networks[i].rssi = WiFi.RSSI(i); } // Sort the networks by RSSI in descending order @@ -147,27 +148,27 @@ bool check_valid_ssid() { // Was the network within secrets.json found? for (const auto &network : networks) { - if (strcmp(_ssid, network.ssid) == 0) { - WS_DEBUG_PRINT("SSID ("); - WS_DEBUG_PRINT(_ssid); - WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(network.rssi); - return true; - } + if (strcmp(_ssid, network.ssid) == 0) { + WS_DEBUG_PRINT("SSID ("); + WS_DEBUG_PRINT(_ssid); + WS_DEBUG_PRINT(") found! RSSI: "); + WS_DEBUG_PRINTLN(network.rssi); + return 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 (const auto &network : networks) { - WS_DEBUG_PRINT(network.ssid); - WS_DEBUG_PRINT(" "); - WS_DEBUG_PRINT(network.rssi); - WS_DEBUG_PRINTLN("dB"); + WS_DEBUG_PRINT(network.ssid); + WS_DEBUG_PRINT(" "); + WS_DEBUG_PRINT(network.rssi); + WS_DEBUG_PRINTLN("dB"); } return false; -} + } /********************************************************/ /*! diff --git a/src/network_interfaces/Wippersnapper_ESP32.h b/src/network_interfaces/Wippersnapper_ESP32.h index d7e2a7dd1..5e2c12498 100644 --- a/src/network_interfaces/Wippersnapper_ESP32.h +++ b/src/network_interfaces/Wippersnapper_ESP32.h @@ -23,12 +23,12 @@ #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" #include "Arduino.h" -#include -#include #include "WiFi.h" #include "WiFiMulti.h" #include #include +#include +#include extern Wippersnapper WS; /****************************************************************************/ @@ -94,13 +94,13 @@ class Wippersnapper_ESP32 : public Wippersnapper { // Define a structure to hold network information struct WiFiNetwork { - char ssid[33]; // Maximum SSID length is 32 characters + null terminator - int rssi; + char ssid[33]; // Maximum SSID length is 32 characters + null terminator + int rssi; }; // Comparison function to sort by RSSI in descending order bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) { - return a.rssi > b.rssi; + return a.rssi > b.rssi; } /***********************************************************/ @@ -129,8 +129,8 @@ class Wippersnapper_ESP32 : public Wippersnapper { // Perform a network scan int n = WiFi.scanNetworks(); if (n == 0) { - WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!"); - return false; + WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!"); + return false; } // Dynamically allocate memory for the network list @@ -138,9 +138,11 @@ class Wippersnapper_ESP32 : public Wippersnapper { // Store the scanned networks in the vector for (int i = 0; i < n; ++i) { - strncpy(networks[i].ssid, WiFi.SSID(i).c_str(), sizeof(networks[i].ssid) - 1); - networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; // Ensure null termination - networks[i].rssi = WiFi.RSSI(i); + strncpy(networks[i].ssid, WiFi.SSID(i).c_str(), + sizeof(networks[i].ssid) - 1); + networks[i].ssid[sizeof(networks[i].ssid) - 1] = + '\0'; // Ensure null termination + networks[i].rssi = WiFi.RSSI(i); } // Sort the networks by RSSI in descending order @@ -148,35 +150,35 @@ class Wippersnapper_ESP32 : public Wippersnapper { // Was the network within secrets.json found? for (const auto &network : networks) { - if (strcmp(_ssid, network.ssid) == 0) { + if (strcmp(_ssid, network.ssid) == 0) { + WS_DEBUG_PRINT("SSID ("); + WS_DEBUG_PRINT(_ssid); + WS_DEBUG_PRINT(") found! RSSI: "); + WS_DEBUG_PRINTLN(network.rssi); + return true; + } + if (WS._isWiFiMulti) { + // multi network mode + for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) { + if (strcmp(WS._multiNetworks[j].ssid, network.ssid) == 0) { WS_DEBUG_PRINT("SSID ("); - WS_DEBUG_PRINT(_ssid); + WS_DEBUG_PRINT(WS._multiNetworks[j].ssid); WS_DEBUG_PRINT(") found! RSSI: "); WS_DEBUG_PRINTLN(network.rssi); return true; + } } - if (WS._isWiFiMulti) { - // multi network mode - for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) { - if (strcmp(WS._multiNetworks[j].ssid, network.ssid) == 0) { - WS_DEBUG_PRINT("SSID ("); - WS_DEBUG_PRINT(WS._multiNetworks[j].ssid); - WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(network.rssi); - return 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 (const auto &network : networks) { - WS_DEBUG_PRINT(network.ssid); - WS_DEBUG_PRINT(" "); - WS_DEBUG_PRINT(network.rssi); - WS_DEBUG_PRINTLN("dB"); + WS_DEBUG_PRINT(network.ssid); + WS_DEBUG_PRINT(" "); + WS_DEBUG_PRINT(network.rssi); + WS_DEBUG_PRINTLN("dB"); } return false; diff --git a/src/network_interfaces/Wippersnapper_ESP8266.h b/src/network_interfaces/Wippersnapper_ESP8266.h index 8d250fb51..e38eb0338 100644 --- a/src/network_interfaces/Wippersnapper_ESP8266.h +++ b/src/network_interfaces/Wippersnapper_ESP8266.h @@ -18,13 +18,13 @@ #define WIPPERSNAPPER_ESP8266_H #ifdef ARDUINO_ARCH_ESP8266 -#include -#include #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" #include "ESP8266WiFi.h" #include "ESP8266WiFiMulti.h" #include "Wippersnapper.h" +#include +#include /* NOTE - Projects that require "Secure MQTT" (TLS/SSL) also require a new * SSL certificate every year. If adding Secure MQTT to your ESP8266 project is @@ -116,15 +116,13 @@ class Wippersnapper_ESP8266 : public Wippersnapper { } // Define a structure to hold network information - struct WiFiNetwork - { + struct WiFiNetwork { char ssid[33]; // Maximum SSID length is 32 characters + null terminator int rssi; }; // Comparison function to sort by RSSI in descending order - bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) - { + bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) { return a.rssi > b.rssi; } @@ -153,8 +151,10 @@ class Wippersnapper_ESP8266 : public Wippersnapper { // Store the scanned networks in the vector for (int i = 0; i < n; ++i) { - strncpy(networks[i].ssid, WiFi.SSID(i).c_str(), sizeof(networks[i].ssid) - 1); - networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; // Ensure null termination + strncpy(networks[i].ssid, WiFi.SSID(i).c_str(), + sizeof(networks[i].ssid) - 1); + networks[i].ssid[sizeof(networks[i].ssid) - 1] = + '\0'; // Ensure null termination networks[i].rssi = WiFi.RSSI(i); } @@ -187,8 +187,7 @@ class Wippersnapper_ESP8266 : public Wippersnapper { // 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 (const auto &network : networks) - { + for (const auto &network : networks) { WS_DEBUG_PRINT(network.ssid); WS_DEBUG_PRINT(" "); WS_DEBUG_PRINT(network.rssi); diff --git a/src/network_interfaces/Wippersnapper_WIFININA.h b/src/network_interfaces/Wippersnapper_WIFININA.h index d891b8a41..0baba7cf1 100644 --- a/src/network_interfaces/Wippersnapper_WIFININA.h +++ b/src/network_interfaces/Wippersnapper_WIFININA.h @@ -19,10 +19,10 @@ #include #include #include -#include -#include #include #include +#include +#include #include "Wippersnapper.h" @@ -111,17 +111,14 @@ class Wippersnapper_WIFININA : public Wippersnapper { strlcpy(WS._config.network.pass, _pass, sizeof(WS._config.network.pass)); } - // Define a structure to hold network information - struct WiFiNetwork - { + struct WiFiNetwork { char ssid[33]; // Maximum SSID length is 32 characters + null terminator int rssi; }; // Comparison function to sort by RSSI in descending order - bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) - { + bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) { return a.rssi > b.rssi; } @@ -140,8 +137,8 @@ class Wippersnapper_WIFININA : public Wippersnapper { // Perform a network scan int n = WiFi.scanNetworks(); if (n == 0) { - WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!"); - return false; + WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!"); + return false; } // Dynamically allocate memory for the network list @@ -149,9 +146,10 @@ class Wippersnapper_WIFININA : public Wippersnapper { // Store the scanned networks in the vector for (int i = 0; i < n; ++i) { - strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1); - networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; // Ensure null termination - networks[i].rssi = WiFi.RSSI(i); + strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1); + networks[i].ssid[sizeof(networks[i].ssid) - 1] = + '\0'; // Ensure null termination + networks[i].rssi = WiFi.RSSI(i); } // Sort the networks by RSSI in descending order @@ -159,23 +157,23 @@ class Wippersnapper_WIFININA : public Wippersnapper { // Was the network within secrets.json found? for (const auto &network : networks) { - if (strcmp(_ssid, network.ssid) == 0) { - WS_DEBUG_PRINT("SSID ("); - WS_DEBUG_PRINT(_ssid); - WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(network.rssi); - return true; - } + if (strcmp(_ssid, network.ssid) == 0) { + WS_DEBUG_PRINT("SSID ("); + WS_DEBUG_PRINT(_ssid); + WS_DEBUG_PRINT(") found! RSSI: "); + WS_DEBUG_PRINTLN(network.rssi); + return 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 (const auto &network : networks) { - WS_DEBUG_PRINT(network.ssid); - WS_DEBUG_PRINT(" "); - WS_DEBUG_PRINT(network.rssi); - WS_DEBUG_PRINTLN("dB"); + WS_DEBUG_PRINT(network.ssid); + WS_DEBUG_PRINT(" "); + WS_DEBUG_PRINT(network.rssi); + WS_DEBUG_PRINTLN("dB"); } return false; diff --git a/src/network_interfaces/ws_networking_pico.h b/src/network_interfaces/ws_networking_pico.h index 79ad8847f..99e07a97e 100644 --- a/src/network_interfaces/ws_networking_pico.h +++ b/src/network_interfaces/ws_networking_pico.h @@ -27,10 +27,10 @@ #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" #include "Arduino.h" -#include -#include #include #include +#include +#include extern Wippersnapper WS; /****************************************************************************/ @@ -95,15 +95,13 @@ class ws_networking_pico : public Wippersnapper { } // Define a structure to hold network information - struct WiFiNetwork - { + struct WiFiNetwork { char ssid[33]; // Maximum SSID length is 32 characters + null terminator int rssi; }; // Comparison function to sort by RSSI in descending order - bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) - { + bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) { return a.rssi > b.rssi; } @@ -131,10 +129,10 @@ class ws_networking_pico : public Wippersnapper { std::vector networks(n); // Store the scanned networks in the vector - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1); - networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; // Ensure null termination + networks[i].ssid[sizeof(networks[i].ssid) - 1] = + '\0'; // Ensure null termination networks[i].rssi = WiFi.RSSI(i); } @@ -142,23 +140,18 @@ class ws_networking_pico : public Wippersnapper { std::sort(networks.begin(), networks.end(), compareByRSSI); // Was the network within secrets.json found? - for (const auto &network : networks) - { - if (strcmp(_ssid, network.ssid) == 0) - { + for (const auto &network : networks) { + if (strcmp(_ssid, network.ssid) == 0) { WS_DEBUG_PRINT("SSID ("); WS_DEBUG_PRINT(_ssid); WS_DEBUG_PRINT(") found! RSSI: "); WS_DEBUG_PRINTLN(network.rssi); return true; } - if (WS._isWiFiMulti) - { + if (WS._isWiFiMulti) { // multi network mode - for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) - { - if (strcmp(WS._multiNetworks[j].ssid, network.ssid) == 0) - { + for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) { + if (strcmp(WS._multiNetworks[j].ssid, network.ssid) == 0) { WS_DEBUG_PRINT("SSID ("); WS_DEBUG_PRINT(WS._multiNetworks[j].ssid); WS_DEBUG_PRINT(") found! RSSI: "); @@ -172,8 +165,7 @@ class ws_networking_pico : public Wippersnapper { // 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 (const auto &network : networks) - { + for (const auto &network : networks) { WS_DEBUG_PRINT(network.ssid); WS_DEBUG_PRINT(" "); WS_DEBUG_PRINT(network.rssi); From a2dbddfddf84657fcdc8c6480add1bd8d26ff580 Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 14 Nov 2024 18:25:58 +0000 Subject: [PATCH 04/30] Doxygen comments for compareByRSSI --- src/network_interfaces/Wippersnapper_AIRLIFT.h | 17 +++++++++++++++-- src/network_interfaces/Wippersnapper_ESP32.h | 17 +++++++++++++++-- src/network_interfaces/Wippersnapper_ESP8266.h | 17 +++++++++++++++-- src/network_interfaces/Wippersnapper_WIFININA.h | 17 +++++++++++++++-- src/network_interfaces/ws_networking_pico.h | 17 +++++++++++++++-- 5 files changed, 75 insertions(+), 10 deletions(-) diff --git a/src/network_interfaces/Wippersnapper_AIRLIFT.h b/src/network_interfaces/Wippersnapper_AIRLIFT.h index c9f160f9f..f46325007 100644 --- a/src/network_interfaces/Wippersnapper_AIRLIFT.h +++ b/src/network_interfaces/Wippersnapper_AIRLIFT.h @@ -103,13 +103,26 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { _pass = WS._config.network.pass; } - // Define a structure to hold network information + /**********************************************************/ + /*! + @brief a structure to hold network information + */ + /**********************************************************/ struct WiFiNetwork { char ssid[33]; // Maximum SSID length is 32 characters + null terminator int rssi; }; - // Comparison function to sort by RSSI in descending order + /*******************************************************************/ + /*! + @brief Comparison function to sort by RSSI in descending order + @param a + WiFiNetwork object + @param b + WiFiNetwork object + @returns True if a.rssi > b.rssi + */ + /*******************************************************************/ bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) { return a.rssi > b.rssi; } diff --git a/src/network_interfaces/Wippersnapper_ESP32.h b/src/network_interfaces/Wippersnapper_ESP32.h index 5e2c12498..212c844da 100644 --- a/src/network_interfaces/Wippersnapper_ESP32.h +++ b/src/network_interfaces/Wippersnapper_ESP32.h @@ -92,13 +92,26 @@ class Wippersnapper_ESP32 : public Wippersnapper { _pass = WS._config.network.pass; } - // Define a structure to hold network information + /**********************************************************/ + /*! + @brief a structure to hold network information + */ + /**********************************************************/ struct WiFiNetwork { char ssid[33]; // Maximum SSID length is 32 characters + null terminator int rssi; }; - // Comparison function to sort by RSSI in descending order + /*******************************************************************/ + /*! + @brief Comparison function to sort by RSSI in descending order + @param a + WiFiNetwork object + @param b + WiFiNetwork object + @returns True if a.rssi > b.rssi + */ + /*******************************************************************/ bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) { return a.rssi > b.rssi; } diff --git a/src/network_interfaces/Wippersnapper_ESP8266.h b/src/network_interfaces/Wippersnapper_ESP8266.h index e38eb0338..c9ca6b583 100644 --- a/src/network_interfaces/Wippersnapper_ESP8266.h +++ b/src/network_interfaces/Wippersnapper_ESP8266.h @@ -115,13 +115,26 @@ class Wippersnapper_ESP8266 : public Wippersnapper { _pass = WS._config.network.pass; } - // Define a structure to hold network information + /**********************************************************/ + /*! + @brief a structure to hold network information + */ + /**********************************************************/ struct WiFiNetwork { char ssid[33]; // Maximum SSID length is 32 characters + null terminator int rssi; }; - // Comparison function to sort by RSSI in descending order + /*******************************************************************/ + /*! + @brief Comparison function to sort by RSSI in descending order + @param a + WiFiNetwork object + @param b + WiFiNetwork object + @returns True if a.rssi > b.rssi + */ + /*******************************************************************/ bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) { return a.rssi > b.rssi; } diff --git a/src/network_interfaces/Wippersnapper_WIFININA.h b/src/network_interfaces/Wippersnapper_WIFININA.h index 0baba7cf1..cb8e9148c 100644 --- a/src/network_interfaces/Wippersnapper_WIFININA.h +++ b/src/network_interfaces/Wippersnapper_WIFININA.h @@ -111,13 +111,26 @@ class Wippersnapper_WIFININA : public Wippersnapper { strlcpy(WS._config.network.pass, _pass, sizeof(WS._config.network.pass)); } - // Define a structure to hold network information + /**********************************************************/ + /*! + @brief a structure to hold network information + */ + /**********************************************************/ struct WiFiNetwork { char ssid[33]; // Maximum SSID length is 32 characters + null terminator int rssi; }; - // Comparison function to sort by RSSI in descending order + /*******************************************************************/ + /*! + @brief Comparison function to sort by RSSI in descending order + @param a + WiFiNetwork object + @param b + WiFiNetwork object + @returns True if a.rssi > b.rssi + */ + /*******************************************************************/ bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) { return a.rssi > b.rssi; } diff --git a/src/network_interfaces/ws_networking_pico.h b/src/network_interfaces/ws_networking_pico.h index 99e07a97e..ace0ea220 100644 --- a/src/network_interfaces/ws_networking_pico.h +++ b/src/network_interfaces/ws_networking_pico.h @@ -94,13 +94,26 @@ class ws_networking_pico : public Wippersnapper { _pass = WS._config.network.pass; } - // Define a structure to hold network information + /**********************************************************/ + /*! + @brief a structure to hold network information + */ + /**********************************************************/ struct WiFiNetwork { char ssid[33]; // Maximum SSID length is 32 characters + null terminator int rssi; }; - // Comparison function to sort by RSSI in descending order + /*******************************************************************/ + /*! + @brief Comparison function to sort by RSSI in descending order + @param a + WiFiNetwork object + @param b + WiFiNetwork object + @returns True if a.rssi > b.rssi + */ + /*******************************************************************/ bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) { return a.rssi > b.rssi; } From e493f6ad28dbd035b6d50d93fe4a275cc1550edc Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 14 Nov 2024 19:11:10 +0000 Subject: [PATCH 05/30] Document wifi network struct for sorting --- src/network_interfaces/Wippersnapper_AIRLIFT.h | 10 +++++----- src/network_interfaces/Wippersnapper_ESP32.h | 10 +++++----- src/network_interfaces/Wippersnapper_ESP8266.h | 10 +++++----- src/network_interfaces/Wippersnapper_WIFININA.h | 10 +++++----- src/network_interfaces/ws_networking_pico.h | 10 +++++----- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/network_interfaces/Wippersnapper_AIRLIFT.h b/src/network_interfaces/Wippersnapper_AIRLIFT.h index f46325007..b08fde4fa 100644 --- a/src/network_interfaces/Wippersnapper_AIRLIFT.h +++ b/src/network_interfaces/Wippersnapper_AIRLIFT.h @@ -103,14 +103,14 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { _pass = WS._config.network.pass; } - /**********************************************************/ + /****************************************************************/ /*! - @brief a structure to hold network information + @brief a structure to hold network information for sorting */ - /**********************************************************/ + /****************************************************************/ struct WiFiNetwork { - char ssid[33]; // Maximum SSID length is 32 characters + null terminator - int rssi; + char ssid[33]; /*!< SSID (Max 32 characters + null terminator */ + int rssi; /*!< Received Signal Strength Indicator */ }; /*******************************************************************/ diff --git a/src/network_interfaces/Wippersnapper_ESP32.h b/src/network_interfaces/Wippersnapper_ESP32.h index 212c844da..afb388b81 100644 --- a/src/network_interfaces/Wippersnapper_ESP32.h +++ b/src/network_interfaces/Wippersnapper_ESP32.h @@ -92,14 +92,14 @@ class Wippersnapper_ESP32 : public Wippersnapper { _pass = WS._config.network.pass; } - /**********************************************************/ + /****************************************************************/ /*! - @brief a structure to hold network information + @brief a structure to hold network information for sorting */ - /**********************************************************/ + /****************************************************************/ struct WiFiNetwork { - char ssid[33]; // Maximum SSID length is 32 characters + null terminator - int rssi; + char ssid[33]; /*!< SSID (Max 32 characters + null terminator */ + int rssi; /*!< Received Signal Strength Indicator */ }; /*******************************************************************/ diff --git a/src/network_interfaces/Wippersnapper_ESP8266.h b/src/network_interfaces/Wippersnapper_ESP8266.h index c9ca6b583..72196b159 100644 --- a/src/network_interfaces/Wippersnapper_ESP8266.h +++ b/src/network_interfaces/Wippersnapper_ESP8266.h @@ -115,14 +115,14 @@ class Wippersnapper_ESP8266 : public Wippersnapper { _pass = WS._config.network.pass; } - /**********************************************************/ + /****************************************************************/ /*! - @brief a structure to hold network information + @brief a structure to hold network information for sorting */ - /**********************************************************/ + /****************************************************************/ struct WiFiNetwork { - char ssid[33]; // Maximum SSID length is 32 characters + null terminator - int rssi; + char ssid[33]; /*!< SSID (Max 32 characters + null terminator */ + int rssi; /*!< Received Signal Strength Indicator */ }; /*******************************************************************/ diff --git a/src/network_interfaces/Wippersnapper_WIFININA.h b/src/network_interfaces/Wippersnapper_WIFININA.h index cb8e9148c..3ed693d1c 100644 --- a/src/network_interfaces/Wippersnapper_WIFININA.h +++ b/src/network_interfaces/Wippersnapper_WIFININA.h @@ -111,14 +111,14 @@ class Wippersnapper_WIFININA : public Wippersnapper { strlcpy(WS._config.network.pass, _pass, sizeof(WS._config.network.pass)); } - /**********************************************************/ + /****************************************************************/ /*! - @brief a structure to hold network information + @brief a structure to hold network information for sorting */ - /**********************************************************/ + /****************************************************************/ struct WiFiNetwork { - char ssid[33]; // Maximum SSID length is 32 characters + null terminator - int rssi; + char ssid[33]; /*!< SSID (Max 32 characters + null terminator */ + int rssi; /*!< Received Signal Strength Indicator */ }; /*******************************************************************/ diff --git a/src/network_interfaces/ws_networking_pico.h b/src/network_interfaces/ws_networking_pico.h index ace0ea220..19eeafa36 100644 --- a/src/network_interfaces/ws_networking_pico.h +++ b/src/network_interfaces/ws_networking_pico.h @@ -94,14 +94,14 @@ class ws_networking_pico : public Wippersnapper { _pass = WS._config.network.pass; } - /**********************************************************/ + /****************************************************************/ /*! - @brief a structure to hold network information + @brief a structure to hold network information for sorting */ - /**********************************************************/ + /****************************************************************/ struct WiFiNetwork { - char ssid[33]; // Maximum SSID length is 32 characters + null terminator - int rssi; + char ssid[33]; /*!< SSID (Max 32 characters + null terminator */ + int rssi; /*!< Received Signal Strength Indicator */ }; /*******************************************************************/ From 747f02541b385f97f627480a861514c096d3c884 Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 21 Nov 2024 19:44:46 +0000 Subject: [PATCH 06/30] Swap dynamic memory for static in wifi rssi sorting --- src/Wippersnapper.h | 2 ++ .../Wippersnapper_AIRLIFT.h | 29 +++++++++---------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index ede523228..49f844d05 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -20,6 +20,7 @@ // Cpp STD #include +#include // Nanopb dependencies #include @@ -215,6 +216,7 @@ typedef enum { } fsm_net_t; #define WS_WDT_TIMEOUT 60000 ///< WDT timeout +#define WS_MAX_SORTED_NETWORKS 15 ///< Maximum number of networks to sort #define WS_MAX_ALT_WIFI_NETWORKS 3 ///< Maximum number of alternative networks /* MQTT Configuration */ #define WS_KEEPALIVE_INTERVAL_MS \ diff --git a/src/network_interfaces/Wippersnapper_AIRLIFT.h b/src/network_interfaces/Wippersnapper_AIRLIFT.h index b08fde4fa..f2e65eef2 100644 --- a/src/network_interfaces/Wippersnapper_AIRLIFT.h +++ b/src/network_interfaces/Wippersnapper_AIRLIFT.h @@ -109,8 +109,8 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { */ /****************************************************************/ struct WiFiNetwork { - char ssid[33]; /*!< SSID (Max 32 characters + null terminator */ - int rssi; /*!< Received Signal Strength Indicator */ + String ssid[32]; /*!< SSID (Max 32 characters + null terminator */ + int32_t rssi = -99; /*!< Received Signal Strength Indicator */ }; /*******************************************************************/ @@ -145,27 +145,24 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { return false; } - // Dynamically allocate memory for the network list - std::vector networks(n); + WiFiNetwork networks[WS_MAX_SORTED_NETWORKS]; - // Store the scanned networks in the vector - for (int i = 0; i < n; ++i) { - strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1); - networks[i].ssid[sizeof(networks[i].ssid) - 1] = - '\0'; // Ensure null termination + // Store the scanned networks in the array + for (int i = 0; i < n && i < WS_MAX_SORTED_NETWORKS; ++i) { + strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid)); networks[i].rssi = WiFi.RSSI(i); } // Sort the networks by RSSI in descending order - std::sort(networks.begin(), networks.end(), compareByRSSI); + std::sort(networks, networks + std::min(n, WS_MAX_SORTED_NETWORKS), compareByRSSI); // Was the network within secrets.json found? - for (const auto &network : networks) { - if (strcmp(_ssid, network.ssid) == 0) { + for (int i = 0; i < n; ++i) { + if (strcmp(_ssid, WiFi.SSID(i)) == 0) { WS_DEBUG_PRINT("SSID ("); WS_DEBUG_PRINT(_ssid); WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(network.rssi); + WS_DEBUG_PRINTLN(WiFi.RSSI(i)); return true; } } @@ -173,10 +170,10 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { // 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 (const auto &network : networks) { - WS_DEBUG_PRINT(network.ssid); + for (int i = 0; i < n; ++i) { + WS_DEBUG_PRINT(WiFi.SSID(i)); WS_DEBUG_PRINT(" "); - WS_DEBUG_PRINT(network.rssi); + WS_DEBUG_PRINT(WiFi.RSSI(i)); WS_DEBUG_PRINTLN("dB"); } From 3528b3f13c6ee8ff20b42664a99d224c7f33d73d Mon Sep 17 00:00:00 2001 From: tyeth Date: Tue, 26 Nov 2024 19:37:56 +0000 Subject: [PATCH 07/30] sack off string for static memory allocation --- platformio.ini | 31 +++++++++++-------- .../Wippersnapper_AIRLIFT.h | 22 ++++++++----- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/platformio.ini b/platformio.ini index dd4acafc2..c19fc750e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -16,8 +16,16 @@ framework = arduino monitor_speed = 115200 lib_compat_mode = strict lib_deps = + ;;;;;;;;;;; FunHouse / LVGL Boards uncomment these ;;;;;;;;;;;;;; + ; https://github.com/adafruit/Adafruit_HX8357_Library.git + ; https://github.com/adafruit/Adafruit_ILI9341.git + ; https://github.com/adafruit/Adafruit_STMPE610.git + ; https://github.com/adafruit/Adafruit-ST7735-Library.git + ; https://github.com/adafruit/Adafruit_TouchScreen.git + ; https://github.com/brentru/lvgl.git#wippersnapper + ; https://github.com/brentru/Adafruit_LvGL_Glue.git#development + ;;;;;;;;;;; All Boards need these libraries included ;;;;;;;;;;;;;; adafruit/Adafruit Zero DMA Library - https://github.com/adafruit/Adafruit_TinyUSB_Arduino.git adafruit/Adafruit NeoPixel adafruit/Adafruit SPIFlash adafruit/Adafruit DotStar @@ -78,14 +86,8 @@ lib_deps = https://github.com/Sensirion/arduino-i2c-sen5x.git https://github.com/adafruit/WiFiNINA.git https://github.com/Starmbi/hp_BH1750.git - ;;;;;;;;;;; FunHouse / LVGL Boards ;;;;;;;;;;;;;; - https://github.com/adafruit/Adafruit_HX8357_Library.git - https://github.com/adafruit/Adafruit_ILI9341.git - https://github.com/adafruit/Adafruit_STMPE610.git - https://github.com/adafruit/Adafruit-ST7735-Library.git - https://github.com/adafruit/Adafruit_TouchScreen.git - https://github.com/brentru/lvgl.git#wippersnapper - https://github.com/brentru/Adafruit_LvGL_Glue.git#development + https://github.com/adafruit/Adafruit_TinyUSB_Arduino.git + ; Common build environment for ESP32 platform @@ -107,9 +109,10 @@ platform = atmelsam platform_packages = platformio/framework-arduino-samd-adafruit@^1.7.13 platformio/tool-jlink@^1.78811.0 -lib_ldf_mode = deep +lib_ldf_mode = chain+ +lib_compat_mode = strict lib_archive = no ; debug timer issues see https://community.platformio.org/t/choose-usb-stack-as-tiny-usb/22451/5 -lib_ignore = OneWire +lib_ignore = OneWire, USBHost [common:rp2040] platform = https://github.com/maxgerhardt/platform-raspberrypi.git @@ -418,8 +421,9 @@ upload_port = /dev/cu.SLAB_USBtoUART [env:adafruit_pyportal_m4] extends = common:atsamd board = adafruit_pyportal_m4 -build_flags = -DUSE_TINYUSB=1 +build_flags = -DUSE_TINYUSB -DADAFRUIT_PYPORTAL +extra_scripts = pre:rename_usb_config.py ; Adafruit PyPortal M4 Titano [env:adafruit_pyportal_m4_titano] @@ -463,8 +467,9 @@ build_flags = -DUSE_TINYUSB [env:adafruit_metro_m4_airliftlite] extends = common:atsamd board = adafruit_metro_m4_airliftlite -build_flags = -DUSE_TINYUSB=1 +build_flags = -DUSE_TINYUSB -DADAFRUIT_METRO_M4_AIRLIFT_LITE +; extra_scripts = pre:rename_usb_config.py upload_port = /dev/cu.usbmodem1201 diff --git a/src/network_interfaces/Wippersnapper_AIRLIFT.h b/src/network_interfaces/Wippersnapper_AIRLIFT.h index f2e65eef2..a3f0ba788 100644 --- a/src/network_interfaces/Wippersnapper_AIRLIFT.h +++ b/src/network_interfaces/Wippersnapper_AIRLIFT.h @@ -103,13 +103,13 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { _pass = WS._config.network.pass; } - /****************************************************************/ + /*******************************************************************/ /*! - @brief a structure to hold network information for sorting + @brief fixed size structure to hold network information for sorting */ - /****************************************************************/ + /*******************************************************************/ struct WiFiNetwork { - String ssid[32]; /*!< SSID (Max 32 characters + null terminator */ + char ssid[33]; /*!< SSID (Max 32 characters + null terminator */ int32_t rssi = -99; /*!< Received Signal Strength Indicator */ }; @@ -148,9 +148,17 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { WiFiNetwork networks[WS_MAX_SORTED_NETWORKS]; // Store the scanned networks in the array - for (int i = 0; i < n && i < WS_MAX_SORTED_NETWORKS; ++i) { - strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid)); - networks[i].rssi = WiFi.RSSI(i); + for (int i = 0; i < n; ++i) { + if (i < WS_MAX_SORTED_NETWORKS){ + strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid)); + networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; + networks[i].rssi = WiFi.RSSI(i); + } else { + WS_DEBUG_PRINT("ERROR: Too many networks found! (>"); + WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS); + WS_DEBUG_PRINT(") Ignoring "); + WS_DEBUG_PRINT(WiFi.SSID(i)); + } } // Sort the networks by RSSI in descending order From 4335f340ab24061e41260a395747d1989802f7eb Mon Sep 17 00:00:00 2001 From: tyeth Date: Tue, 26 Nov 2024 21:24:21 +0000 Subject: [PATCH 08/30] Update logging and logic --- .../Wippersnapper_AIRLIFT.h | 32 +++++++---- src/network_interfaces/Wippersnapper_ESP32.h | 54 ++++++++++++------- 2 files changed, 57 insertions(+), 29 deletions(-) diff --git a/src/network_interfaces/Wippersnapper_AIRLIFT.h b/src/network_interfaces/Wippersnapper_AIRLIFT.h index a3f0ba788..02674832a 100644 --- a/src/network_interfaces/Wippersnapper_AIRLIFT.h +++ b/src/network_interfaces/Wippersnapper_AIRLIFT.h @@ -109,8 +109,8 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { */ /*******************************************************************/ struct WiFiNetwork { - char ssid[33]; /*!< SSID (Max 32 characters + null terminator */ - int32_t rssi = -99; /*!< Received Signal Strength Indicator */ + char ssid[33]; /*!< SSID (Max 32 characters + null terminator */ + int32_t rssi = -99; /*!< Received Signal Strength Indicator */ }; /*******************************************************************/ @@ -146,42 +146,54 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { } WiFiNetwork networks[WS_MAX_SORTED_NETWORKS]; + uint8_t numSavedNetworks = 0; // Store the scanned networks in the array for (int i = 0; i < n; ++i) { - if (i < WS_MAX_SORTED_NETWORKS){ + if (i < WS_MAX_SORTED_NETWORKS) { strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid)); networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; networks[i].rssi = WiFi.RSSI(i); + numSavedNetworks++; } else { WS_DEBUG_PRINT("ERROR: Too many networks found! (>"); WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS); WS_DEBUG_PRINT(") Ignoring "); WS_DEBUG_PRINT(WiFi.SSID(i)); + WS_DEBUG_PRINT("("); + WS_DEBUG_PRINT(WiFi.RSSI(i)); + WS_DEBUG_PRINTLN(")"); } } // Sort the networks by RSSI in descending order - std::sort(networks, networks + std::min(n, WS_MAX_SORTED_NETWORKS), compareByRSSI); + std::sort(networks, networks + numSavedNetworks, compareByRSSI); // Was the network within secrets.json found? - for (int i = 0; i < n; ++i) { - if (strcmp(_ssid, WiFi.SSID(i)) == 0) { + for (int i = 0; i < numSavedNetworks; ++i) { + if (strcmp(_ssid, networks[i].ssid) == 0) { WS_DEBUG_PRINT("SSID ("); WS_DEBUG_PRINT(_ssid); WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(WiFi.RSSI(i)); + WS_DEBUG_PRINTLN(networks[i].rssi); return 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: "); + WS_DEBUG_PRINT("WipperSnapper found these WiFi networks"); + if (n > WS_MAX_SORTED_NETWORKS) { + WS_DEBUG_PRINT(" (only first "); + WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS); + WS_DEBUG_PRINTLN(" used):"); + } else { + WS_DEBUG_PRINTLN(":"); + } for (int i = 0; i < n; ++i) { - WS_DEBUG_PRINT(WiFi.SSID(i)); + WS_DEBUG_PRINT(networks[i].ssid); WS_DEBUG_PRINT(" "); - WS_DEBUG_PRINT(WiFi.RSSI(i)); + WS_DEBUG_PRINT(networks[i].rssi); WS_DEBUG_PRINTLN("dB"); } diff --git a/src/network_interfaces/Wippersnapper_ESP32.h b/src/network_interfaces/Wippersnapper_ESP32.h index afb388b81..b963bfc2d 100644 --- a/src/network_interfaces/Wippersnapper_ESP32.h +++ b/src/network_interfaces/Wippersnapper_ESP32.h @@ -99,7 +99,7 @@ class Wippersnapper_ESP32 : public Wippersnapper { /****************************************************************/ struct WiFiNetwork { char ssid[33]; /*!< SSID (Max 32 characters + null terminator */ - int rssi; /*!< Received Signal Strength Indicator */ + int32_t rssi; /*!< Received Signal Strength Indicator */ }; /*******************************************************************/ @@ -146,38 +146,47 @@ class Wippersnapper_ESP32 : public Wippersnapper { return false; } - // Dynamically allocate memory for the network list - std::vector networks(n); - + WiFiNetwork networks[WS_MAX_SORTED_NETWORKS]; + uint8_t numSavedNetworks = 0; // Store the scanned networks in the vector for (int i = 0; i < n; ++i) { - strncpy(networks[i].ssid, WiFi.SSID(i).c_str(), - sizeof(networks[i].ssid) - 1); - networks[i].ssid[sizeof(networks[i].ssid) - 1] = - '\0'; // Ensure null termination - networks[i].rssi = WiFi.RSSI(i); + if (i < WS_MAX_SORTED_NETWORKS) { + strncpy(networks[i].ssid, WiFi.SSID(i).c_str(), + sizeof(networks[i].ssid)); + networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; + networks[i].rssi = WiFi.RSSI(i); + numSavedNetworks++; + } else { + WS_DEBUG_PRINT("ERROR: Too many networks found! (>"); + WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS); + WS_DEBUG_PRINT(") Ignoring "); + WS_DEBUG_PRINT(WiFi.SSID(i)); + WS_DEBUG_PRINT("("); + WS_DEBUG_PRINT(WiFi.RSSI(i)); + WS_DEBUG_PRINTLN(")"); + } } // Sort the networks by RSSI in descending order - std::sort(networks.begin(), networks.end(), compareByRSSI); + std::sort(networks, networks + numSavedNetworks, compareByRSSI); // Was the network within secrets.json found? - for (const auto &network : networks) { - if (strcmp(_ssid, network.ssid) == 0) { + for (int i = 0; i < numSavedNetworks; ++i) { + if (strcmp(_ssid, networks[i].ssid) == 0) { WS_DEBUG_PRINT("SSID ("); WS_DEBUG_PRINT(_ssid); WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(network.rssi); + WS_DEBUG_PRINTLN(networks[i].rssi); return true; } if (WS._isWiFiMulti) { // multi network mode for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) { - if (strcmp(WS._multiNetworks[j].ssid, network.ssid) == 0) { + if (strcmp(WS._multiNetworks[j].ssid, networks[i].ssid) == 0) { WS_DEBUG_PRINT("SSID ("); WS_DEBUG_PRINT(WS._multiNetworks[j].ssid); WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(network.rssi); + WS_DEBUG_PRINTLN(networks[i].rssi); return true; } } @@ -186,11 +195,18 @@ class Wippersnapper_ESP32 : public Wippersnapper { // 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 (const auto &network : networks) { - WS_DEBUG_PRINT(network.ssid); + WS_DEBUG_PRINT("WipperSnapper found these WiFi networks"); + if (n > WS_MAX_SORTED_NETWORKS) { + WS_DEBUG_PRINT(" (only first "); + WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS); + WS_DEBUG_PRINTLN(" used):"); + } else { + WS_DEBUG_PRINTLN(":"); + } + for (int i = 0; i < n; ++i) { + WS_DEBUG_PRINT(networks[i].ssid); WS_DEBUG_PRINT(" "); - WS_DEBUG_PRINT(network.rssi); + WS_DEBUG_PRINT(networks[i].rssi); WS_DEBUG_PRINTLN("dB"); } From db43d0f5f1f42283cfb653ff95075a7c4984da91 Mon Sep 17 00:00:00 2001 From: tyeth Date: Tue, 26 Nov 2024 21:39:16 +0000 Subject: [PATCH 09/30] Update pico and WIFININA(arduino versions) --- .../Wippersnapper_WIFININA.h | 49 ++++++++++++------ src/network_interfaces/ws_networking_pico.h | 51 ++++++++++++------- 2 files changed, 67 insertions(+), 33 deletions(-) diff --git a/src/network_interfaces/Wippersnapper_WIFININA.h b/src/network_interfaces/Wippersnapper_WIFININA.h index 3ed693d1c..4867286dc 100644 --- a/src/network_interfaces/Wippersnapper_WIFININA.h +++ b/src/network_interfaces/Wippersnapper_WIFININA.h @@ -148,44 +148,61 @@ class Wippersnapper_WIFININA : public Wippersnapper { delay(100); // Perform a network scan - int n = WiFi.scanNetworks(); + int8_t n = WiFi.scanNetworks(); if (n == 0) { WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!"); return false; } - // Dynamically allocate memory for the network list - std::vector networks(n); + WiFiNetwork networks[WS_MAX_SORTED_NETWORKS]; + uint8_t numSavedNetworks = 0; // Store the scanned networks in the vector for (int i = 0; i < n; ++i) { - strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1); - networks[i].ssid[sizeof(networks[i].ssid) - 1] = - '\0'; // Ensure null termination - networks[i].rssi = WiFi.RSSI(i); + if (i < WS_MAX_SORTED_NETWORKS) { + strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid)); + networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; + networks[i].rssi = WiFi.RSSI(i); + numSavedNetworks++; + } else { + WS_DEBUG_PRINT("ERROR: Too many networks found! (>"); + WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS); + WS_DEBUG_PRINT(") Ignoring "); + WS_DEBUG_PRINT(WiFi.SSID(i)); + WS_DEBUG_PRINT("("); + WS_DEBUG_PRINT(WiFi.RSSI(i)); + WS_DEBUG_PRINTLN(")"); + } } - // Sort the networks by RSSI in descending order - std::sort(networks.begin(), networks.end(), compareByRSSI); + /// Sort the networks by RSSI in descending order + std::sort(networks, networks + numSavedNetworks, compareByRSSI); // Was the network within secrets.json found? - for (const auto &network : networks) { - if (strcmp(_ssid, network.ssid) == 0) { + for (int i = 0; i < numSavedNetworks; ++i) { + if (strcmp(_ssid, networks[i].ssid) == 0) { WS_DEBUG_PRINT("SSID ("); WS_DEBUG_PRINT(_ssid); WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(network.rssi); + WS_DEBUG_PRINTLN(networks[i].rssi); return 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 (const auto &network : networks) { - WS_DEBUG_PRINT(network.ssid); + WS_DEBUG_PRINT("WipperSnapper found these WiFi networks"); + if (n > WS_MAX_SORTED_NETWORKS) { + WS_DEBUG_PRINT(" (only first "); + WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS); + WS_DEBUG_PRINTLN(" used):"); + } else { + WS_DEBUG_PRINTLN(":"); + } + for (int i = 0; i < n; ++i) { + WS_DEBUG_PRINT(networks[i].ssid); WS_DEBUG_PRINT(" "); - WS_DEBUG_PRINT(network.rssi); + WS_DEBUG_PRINT(networks[i].rssi); WS_DEBUG_PRINTLN("dB"); } diff --git a/src/network_interfaces/ws_networking_pico.h b/src/network_interfaces/ws_networking_pico.h index 19eeafa36..67ca2bf69 100644 --- a/src/network_interfaces/ws_networking_pico.h +++ b/src/network_interfaces/ws_networking_pico.h @@ -138,37 +138,47 @@ class ws_networking_pico : public Wippersnapper { return false; } - // Dynamically allocate memory for the network list - std::vector networks(n); + WiFiNetwork networks[WS_MAX_SORTED_NETWORKS]; + uint8_t numSavedNetworks = 0; // Store the scanned networks in the vector for (int i = 0; i < n; ++i) { - strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1); - networks[i].ssid[sizeof(networks[i].ssid) - 1] = - '\0'; // Ensure null termination - networks[i].rssi = WiFi.RSSI(i); + if (i < WS_MAX_SORTED_NETWORKS) { + strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid)); + networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; + networks[i].rssi = WiFi.RSSI(i); + numSavedNetworks++; + } else { + WS_DEBUG_PRINT("ERROR: Too many networks found! (>"); + WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS); + WS_DEBUG_PRINT(") Ignoring "); + WS_DEBUG_PRINT(WiFi.SSID(i)); + WS_DEBUG_PRINT("("); + WS_DEBUG_PRINT(WiFi.RSSI(i)); + WS_DEBUG_PRINTLN(")"); + } } - // Sort the networks by RSSI in descending order - std::sort(networks.begin(), networks.end(), compareByRSSI); + /// Sort the networks by RSSI in descending order + std::sort(networks, networks + numSavedNetworks, compareByRSSI); // Was the network within secrets.json found? - for (const auto &network : networks) { - if (strcmp(_ssid, network.ssid) == 0) { + for (int i = 0; i < numSavedNetworks; ++i) { + if (strcmp(_ssid, networks[i].ssid) == 0) { WS_DEBUG_PRINT("SSID ("); WS_DEBUG_PRINT(_ssid); WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(network.rssi); + WS_DEBUG_PRINTLN(networks[i].rssi); return true; } if (WS._isWiFiMulti) { // multi network mode for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) { - if (strcmp(WS._multiNetworks[j].ssid, network.ssid) == 0) { + if (strcmp(WS._multiNetworks[j].ssid, networks[i].ssid) == 0) { WS_DEBUG_PRINT("SSID ("); WS_DEBUG_PRINT(WS._multiNetworks[j].ssid); WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(network.rssi); + WS_DEBUG_PRINTLN(networks[i].rssi); return true; } } @@ -177,11 +187,18 @@ class ws_networking_pico : public Wippersnapper { // 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 (const auto &network : networks) { - WS_DEBUG_PRINT(network.ssid); + WS_DEBUG_PRINT("WipperSnapper found these WiFi networks"); + if (n > WS_MAX_SORTED_NETWORKS) { + WS_DEBUG_PRINT(" (only first "); + WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS); + WS_DEBUG_PRINTLN(" used):"); + } else { + WS_DEBUG_PRINTLN(":"); + } + for (int i = 0; i < n; ++i) { + WS_DEBUG_PRINT(networks[i].ssid); WS_DEBUG_PRINT(" "); - WS_DEBUG_PRINT(network.rssi); + WS_DEBUG_PRINT(networks[i].rssi); WS_DEBUG_PRINTLN("dB"); } From 644aff902ea9026a578e0169c77954ece56dd85d Mon Sep 17 00:00:00 2001 From: tyeth Date: Tue, 26 Nov 2024 21:48:56 +0000 Subject: [PATCH 10/30] Remove unnecessary includes + fixup esp8266 --- .../Wippersnapper_AIRLIFT.h | 2 - src/network_interfaces/Wippersnapper_ESP32.h | 2 - .../Wippersnapper_ESP8266.h | 54 ++++++++++++------- .../Wippersnapper_WIFININA.h | 2 - src/network_interfaces/ws_networking_pico.h | 2 - 5 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/network_interfaces/Wippersnapper_AIRLIFT.h b/src/network_interfaces/Wippersnapper_AIRLIFT.h index 02674832a..accd9128f 100644 --- a/src/network_interfaces/Wippersnapper_AIRLIFT.h +++ b/src/network_interfaces/Wippersnapper_AIRLIFT.h @@ -26,8 +26,6 @@ #include "SPI.h" #include "WiFiNINA.h" #include "Wippersnapper.h" -#include -#include #define NINAFWVER \ "1.7.7" /*!< min. nina-fw version compatible with this library. */ diff --git a/src/network_interfaces/Wippersnapper_ESP32.h b/src/network_interfaces/Wippersnapper_ESP32.h index b963bfc2d..4b2158b96 100644 --- a/src/network_interfaces/Wippersnapper_ESP32.h +++ b/src/network_interfaces/Wippersnapper_ESP32.h @@ -27,8 +27,6 @@ #include "WiFiMulti.h" #include #include -#include -#include extern Wippersnapper WS; /****************************************************************************/ diff --git a/src/network_interfaces/Wippersnapper_ESP8266.h b/src/network_interfaces/Wippersnapper_ESP8266.h index 72196b159..68dd37ccf 100644 --- a/src/network_interfaces/Wippersnapper_ESP8266.h +++ b/src/network_interfaces/Wippersnapper_ESP8266.h @@ -23,8 +23,6 @@ #include "ESP8266WiFi.h" #include "ESP8266WiFiMulti.h" #include "Wippersnapper.h" -#include -#include /* NOTE - Projects that require "Secure MQTT" (TLS/SSL) also require a new * SSL certificate every year. If adding Secure MQTT to your ESP8266 project is @@ -159,38 +157,47 @@ class Wippersnapper_ESP8266 : public Wippersnapper { return false; } - // Dynamically allocate memory for the network list - std::vector networks(n); - + WiFiNetwork networks[WS_MAX_SORTED_NETWORKS]; + uint8_t numSavedNetworks = 0; // Store the scanned networks in the vector for (int i = 0; i < n; ++i) { - strncpy(networks[i].ssid, WiFi.SSID(i).c_str(), - sizeof(networks[i].ssid) - 1); - networks[i].ssid[sizeof(networks[i].ssid) - 1] = - '\0'; // Ensure null termination - networks[i].rssi = WiFi.RSSI(i); + if (i < WS_MAX_SORTED_NETWORKS) { + strncpy(networks[i].ssid, WiFi.SSID(i).c_str(), + sizeof(networks[i].ssid)); + networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; + networks[i].rssi = WiFi.RSSI(i); + numSavedNetworks++; + } else { + WS_DEBUG_PRINT("ERROR: Too many networks found! (>"); + WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS); + WS_DEBUG_PRINT(") Ignoring "); + WS_DEBUG_PRINT(WiFi.SSID(i)); + WS_DEBUG_PRINT("("); + WS_DEBUG_PRINT(WiFi.RSSI(i)); + WS_DEBUG_PRINTLN(")"); + } } // Sort the networks by RSSI in descending order - std::sort(networks.begin(), networks.end(), compareByRSSI); + std::sort(networks, networks + numSavedNetworks, compareByRSSI); // Was the network within secrets.json found? - for (const auto &network : networks) { - if (strcmp(_ssid, network.ssid) == 0) { + for (int i = 0; i < numSavedNetworks; ++i) { + if (strcmp(_ssid, networks[i].ssid) == 0) { WS_DEBUG_PRINT("SSID ("); WS_DEBUG_PRINT(_ssid); WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(network.rssi); + WS_DEBUG_PRINTLN(networks[i].rssi); return true; } if (WS._isWiFiMulti) { // multi network mode for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) { - if (strcmp(WS._multiNetworks[j].ssid, network.ssid) == 0) { + if (strcmp(WS._multiNetworks[j].ssid, networks[i].ssid) == 0) { WS_DEBUG_PRINT("SSID ("); WS_DEBUG_PRINT(WS._multiNetworks[j].ssid); WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(network.rssi); + WS_DEBUG_PRINTLN(networks[i].rssi); return true; } } @@ -199,11 +206,18 @@ class Wippersnapper_ESP8266 : public Wippersnapper { // 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 (const auto &network : networks) { - WS_DEBUG_PRINT(network.ssid); + WS_DEBUG_PRINT("WipperSnapper found these WiFi networks"); + if (n > WS_MAX_SORTED_NETWORKS) { + WS_DEBUG_PRINT(" (only first "); + WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS); + WS_DEBUG_PRINTLN(" used):"); + } else { + WS_DEBUG_PRINTLN(":"); + } + for (int i = 0; i < n; ++i) { + WS_DEBUG_PRINT(networks[i].ssid); WS_DEBUG_PRINT(" "); - WS_DEBUG_PRINT(network.rssi); + WS_DEBUG_PRINT(networks[i].rssi); WS_DEBUG_PRINTLN("dB"); } diff --git a/src/network_interfaces/Wippersnapper_WIFININA.h b/src/network_interfaces/Wippersnapper_WIFININA.h index 4867286dc..a713b625d 100644 --- a/src/network_interfaces/Wippersnapper_WIFININA.h +++ b/src/network_interfaces/Wippersnapper_WIFININA.h @@ -21,8 +21,6 @@ #include #include #include -#include -#include #include "Wippersnapper.h" diff --git a/src/network_interfaces/ws_networking_pico.h b/src/network_interfaces/ws_networking_pico.h index 67ca2bf69..5f68c048b 100644 --- a/src/network_interfaces/ws_networking_pico.h +++ b/src/network_interfaces/ws_networking_pico.h @@ -29,8 +29,6 @@ #include "Arduino.h" #include #include -#include -#include extern Wippersnapper WS; /****************************************************************************/ From 0f09cfdce248c65e0bc6b50c07682114ed214701 Mon Sep 17 00:00:00 2001 From: tyeth Date: Tue, 26 Nov 2024 22:00:11 +0000 Subject: [PATCH 11/30] A comes before V --- src/Wippersnapper.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 49f844d05..f426ee26d 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -19,8 +19,8 @@ #define WIPPERSNAPPER_H // Cpp STD -#include #include +#include // Nanopb dependencies #include From 16188e893398260acd78e6612331d7eb9ac4d2df Mon Sep 17 00:00:00 2001 From: tyeth Date: Tue, 26 Nov 2024 22:56:22 +0000 Subject: [PATCH 12/30] Fix haltError to actually enable WDT to attempt to reset --- src/Wippersnapper.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index a4c190825..f2567424d 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -2500,6 +2500,7 @@ void Wippersnapper::runNetFSM() { */ /**************************************************************************/ void Wippersnapper::haltError(String error, ws_led_status_t ledStatusColor) { + WS.enableWDT(5000); for (;;) { WS_DEBUG_PRINT("ERROR [WDT RESET]: "); WS_DEBUG_PRINTLN(error); From 9c666e17439cdfc0c3106930ca73b89017b3a9e3 Mon Sep 17 00:00:00 2001 From: tyeth Date: Tue, 26 Nov 2024 23:23:03 +0000 Subject: [PATCH 13/30] ESP8266: WiFiMulti does RSSI sorting, normal wifi begin would require channel + bssid --- .../Wippersnapper_ESP8266.h | 50 ++++++++----------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/src/network_interfaces/Wippersnapper_ESP8266.h b/src/network_interfaces/Wippersnapper_ESP8266.h index 68dd37ccf..31ce7b08d 100644 --- a/src/network_interfaces/Wippersnapper_ESP8266.h +++ b/src/network_interfaces/Wippersnapper_ESP8266.h @@ -329,38 +329,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(); } } From c834456e3da226fd46885514875f8fea39c34985 Mon Sep 17 00:00:00 2001 From: tyeth Date: Tue, 26 Nov 2024 23:43:39 +0000 Subject: [PATCH 14/30] remove wifi.begin from esp8266 --- src/network_interfaces/Wippersnapper_ESP8266.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/network_interfaces/Wippersnapper_ESP8266.h b/src/network_interfaces/Wippersnapper_ESP8266.h index 31ce7b08d..670397d66 100644 --- a/src/network_interfaces/Wippersnapper_ESP8266.h +++ b/src/network_interfaces/Wippersnapper_ESP8266.h @@ -315,7 +315,7 @@ 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); + // WiFi.begin(_ssid, _pass); _status = WS_NET_DISCONNECTED; delay(100); From 37d3a91d8d1c2321da7b182c735fd7f35607faaa Mon Sep 17 00:00:00 2001 From: tyeth Date: Wed, 27 Nov 2024 02:56:14 +0000 Subject: [PATCH 15/30] Format ESP8266 + tweak pio target --- platformio.ini | 7 ++++++- src/network_interfaces/Wippersnapper_ESP8266.h | 9 ++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/platformio.ini b/platformio.ini index c19fc750e..ed5d6f08b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -411,7 +411,12 @@ extra_scripts = pre:rename_usb_config.py [env:huzzah] extends=common:esp8266 board = huzzah -build_flags = -DARDUINO_ESP8266_ADAFRUIT_HUZZAH +board_build.f_cpu = 80000000L +; Arduino CLI uses this from adafruit_ci#ci-wippersnapper +; esp8266:esp8266:huzzah:xtal=80,vt=flash,exception=disabled,stacksmash=disabled,ssl=all,mmu=3232,non32xfer=fast,eesz=4M2M,ip=lm2f,dbg=Disabled,lvl=None____,wipe=none,baud=115200 +build_flags = + -DARDUINO_ESP8266_ADAFRUIT_HUZZAH + -DDEBUG_ESP_PORT=Serial board_build.filesystem = littlefs upload_port = /dev/cu.SLAB_USBtoUART diff --git a/src/network_interfaces/Wippersnapper_ESP8266.h b/src/network_interfaces/Wippersnapper_ESP8266.h index 670397d66..ec7ac99c1 100644 --- a/src/network_interfaces/Wippersnapper_ESP8266.h +++ b/src/network_interfaces/Wippersnapper_ESP8266.h @@ -315,7 +315,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); @@ -338,19 +337,19 @@ class Wippersnapper_ESP8266 : public Wippersnapper { long startRetry = millis(); WS_DEBUG_PRINTLN("CONNECTING"); - + while (_wifiMulti.run(5000) != WL_CONNECTED && - millis() - startRetry < 10000) { + 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; } - + WS.feedWDT(); } } From d290af7c0d009934ad2017b9d8e074249de63d03 Mon Sep 17 00:00:00 2001 From: tyeth Date: Tue, 10 Dec 2024 18:42:38 +0000 Subject: [PATCH 16/30] Report BSSID's to help distinguish wifi networks --- platformio.ini | 2 +- src/network_interfaces/ws_networking_pico.h | 28 +++++++++++---------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/platformio.ini b/platformio.ini index ed5d6f08b..dcb0cef64 100644 --- a/platformio.ini +++ b/platformio.ini @@ -504,7 +504,7 @@ build_flags = -DDEBUG_RP2040_CORE -DDEBUG_RP2040_WIFI -DNDEBUG - -DLWIP_DEBUG + -DLWIP_DEBUG=1 -DDEBUG_RP2040_PORT=Serial1 -DDEBUG_RP2040_UART_1 -DDEBUG_RP2040_UART=1 diff --git a/src/network_interfaces/ws_networking_pico.h b/src/network_interfaces/ws_networking_pico.h index 5f68c048b..342a70dc7 100644 --- a/src/network_interfaces/ws_networking_pico.h +++ b/src/network_interfaces/ws_networking_pico.h @@ -185,19 +185,21 @@ class ws_networking_pico : public Wippersnapper { // User-set network not found, print scan results to serial console WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!"); - WS_DEBUG_PRINT("WipperSnapper found these WiFi networks"); - if (n > WS_MAX_SORTED_NETWORKS) { - WS_DEBUG_PRINT(" (only first "); - WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS); - WS_DEBUG_PRINTLN(" used):"); - } else { - WS_DEBUG_PRINTLN(":"); - } - for (int i = 0; i < n; ++i) { - WS_DEBUG_PRINT(networks[i].ssid); - WS_DEBUG_PRINT(" "); - WS_DEBUG_PRINT(networks[i].rssi); - WS_DEBUG_PRINTLN("dB"); + WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks:"); + for (uint8_t i = 0; i < n; ++i) { + WS_DEBUG_PRINT(WiFi.SSID(i)); + 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_PRINT("dB (ch"); + WS_DEBUG_PRINT(WiFi.channel(i)) + WS_DEBUG_PRINTLN(")"); } return false; From d35508f762c843efbfe197ea7f36057fcc200422 Mon Sep 17 00:00:00 2001 From: tyeth Date: Tue, 10 Dec 2024 20:09:44 +0000 Subject: [PATCH 17/30] Update reported network not found to include BSSID/Channel --- .../Wippersnapper_AIRLIFT.h | 31 ++++++++++-------- src/network_interfaces/Wippersnapper_ESP32.h | 31 ++++++++++-------- .../Wippersnapper_ESP8266.h | 32 +++++++++++-------- .../Wippersnapper_WIFININA.h | 31 ++++++++++-------- src/network_interfaces/ws_networking_pico.h | 9 ++++-- 5 files changed, 78 insertions(+), 56 deletions(-) diff --git a/src/network_interfaces/Wippersnapper_AIRLIFT.h b/src/network_interfaces/Wippersnapper_AIRLIFT.h index accd9128f..84a220760 100644 --- a/src/network_interfaces/Wippersnapper_AIRLIFT.h +++ b/src/network_interfaces/Wippersnapper_AIRLIFT.h @@ -180,19 +180,24 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { // User-set network not found, print scan results to serial console WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!"); - WS_DEBUG_PRINT("WipperSnapper found these WiFi networks"); - if (n > WS_MAX_SORTED_NETWORKS) { - WS_DEBUG_PRINT(" (only first "); - WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS); - WS_DEBUG_PRINTLN(" used):"); - } else { - WS_DEBUG_PRINTLN(":"); - } - for (int i = 0; i < n; ++i) { - WS_DEBUG_PRINT(networks[i].ssid); - WS_DEBUG_PRINT(" "); - WS_DEBUG_PRINT(networks[i].rssi); - WS_DEBUG_PRINTLN("dB"); + WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks:"); + for (uint8_t i = 0; i < n; ++i) + { + WS_DEBUG_PRINT(WiFi.SSID(i)); + 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_PRINT("dB (ch"); + WS_DEBUG_PRINT(WiFi.channel(i)) + WS_DEBUG_PRINTLN(")"); } return false; diff --git a/src/network_interfaces/Wippersnapper_ESP32.h b/src/network_interfaces/Wippersnapper_ESP32.h index 4b2158b96..f42b112e4 100644 --- a/src/network_interfaces/Wippersnapper_ESP32.h +++ b/src/network_interfaces/Wippersnapper_ESP32.h @@ -193,19 +193,24 @@ class Wippersnapper_ESP32 : public Wippersnapper { // User-set network not found, print scan results to serial console WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!"); - WS_DEBUG_PRINT("WipperSnapper found these WiFi networks"); - if (n > WS_MAX_SORTED_NETWORKS) { - WS_DEBUG_PRINT(" (only first "); - WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS); - WS_DEBUG_PRINTLN(" used):"); - } else { - WS_DEBUG_PRINTLN(":"); - } - for (int i = 0; i < n; ++i) { - WS_DEBUG_PRINT(networks[i].ssid); - WS_DEBUG_PRINT(" "); - WS_DEBUG_PRINT(networks[i].rssi); - WS_DEBUG_PRINTLN("dB"); + WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks:"); + for (uint8_t i = 0; i < n; ++i) + { + WS_DEBUG_PRINT(WiFi.SSID(i)); + 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_PRINT("dB (ch"); + WS_DEBUG_PRINT(WiFi.channel(i)) + WS_DEBUG_PRINTLN(")"); } return false; diff --git a/src/network_interfaces/Wippersnapper_ESP8266.h b/src/network_interfaces/Wippersnapper_ESP8266.h index ec7ac99c1..4032df142 100644 --- a/src/network_interfaces/Wippersnapper_ESP8266.h +++ b/src/network_interfaces/Wippersnapper_ESP8266.h @@ -205,20 +205,24 @@ class Wippersnapper_ESP8266 : public Wippersnapper { } // User-set network not found, print scan results to serial console - WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!"); - WS_DEBUG_PRINT("WipperSnapper found these WiFi networks"); - if (n > WS_MAX_SORTED_NETWORKS) { - WS_DEBUG_PRINT(" (only first "); - WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS); - WS_DEBUG_PRINTLN(" used):"); - } else { - WS_DEBUG_PRINTLN(":"); - } - for (int i = 0; i < n; ++i) { - WS_DEBUG_PRINT(networks[i].ssid); - WS_DEBUG_PRINT(" "); - WS_DEBUG_PRINT(networks[i].rssi); - WS_DEBUG_PRINTLN("dB"); + WS_DEBUG_PRINTLN("ERROR: Your WiFi network was not found!"); + WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks:"); + for (uint8_t i = 0; i < n; ++i) + { + WS_DEBUG_PRINT(WiFi.SSID(i)); + WS_DEBUG_PRINT(" ("); + const uint8_t* BSSID = WiFi.BSSID(i); + 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_PRINT("dB (ch"); + WS_DEBUG_PRINT(WiFi.channel(i)) + WS_DEBUG_PRINTLN(")"); } return false; diff --git a/src/network_interfaces/Wippersnapper_WIFININA.h b/src/network_interfaces/Wippersnapper_WIFININA.h index a713b625d..77bfefe72 100644 --- a/src/network_interfaces/Wippersnapper_WIFININA.h +++ b/src/network_interfaces/Wippersnapper_WIFININA.h @@ -189,19 +189,24 @@ class Wippersnapper_WIFININA : public Wippersnapper { // User-set network not found, print scan results to serial console WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!"); - WS_DEBUG_PRINT("WipperSnapper found these WiFi networks"); - if (n > WS_MAX_SORTED_NETWORKS) { - WS_DEBUG_PRINT(" (only first "); - WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS); - WS_DEBUG_PRINTLN(" used):"); - } else { - WS_DEBUG_PRINTLN(":"); - } - for (int i = 0; i < n; ++i) { - WS_DEBUG_PRINT(networks[i].ssid); - WS_DEBUG_PRINT(" "); - WS_DEBUG_PRINT(networks[i].rssi); - WS_DEBUG_PRINTLN("dB"); + WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks:"); + for (uint8_t i = 0; i < n; ++i) + { + WS_DEBUG_PRINT(WiFi.SSID(i)); + 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_PRINT("dB (ch"); + WS_DEBUG_PRINT(WiFi.channel(i)) + WS_DEBUG_PRINTLN(")"); } return false; diff --git a/src/network_interfaces/ws_networking_pico.h b/src/network_interfaces/ws_networking_pico.h index 342a70dc7..43b35900e 100644 --- a/src/network_interfaces/ws_networking_pico.h +++ b/src/network_interfaces/ws_networking_pico.h @@ -186,13 +186,16 @@ class ws_networking_pico : public Wippersnapper { // 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 (uint8_t i = 0; i < n; ++i) { + for (uint8_t i = 0; i < n; ++i) + { WS_DEBUG_PRINT(WiFi.SSID(i)); 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(":"); + for (int m = 0; m < WL_MAC_ADDR_LENGTH; m++) + { + if (m != 0) + WS_DEBUG_PRINT(":"); WS_DEBUG_PRINTHEX(BSSID[m]); } WS_DEBUG_PRINT(") "); From 4ceb09114facc1f2b785a9722359b6d7676d5691 Mon Sep 17 00:00:00 2001 From: tyeth Date: Tue, 10 Dec 2024 20:18:09 +0000 Subject: [PATCH 18/30] Add Mac Length define for non-picos --- src/Wippersnapper.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index f426ee26d..d16534671 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -215,6 +215,9 @@ typedef enum { FSM_NET_ESTABLISH_MQTT, } fsm_net_t; +#ifndef WL_MAC_ADDR_LENGTH +#define WL_MAC_ADDR_LENGTH 6 // MAC address length - from RP2040 BSP +#endif #define WS_WDT_TIMEOUT 60000 ///< WDT timeout #define WS_MAX_SORTED_NETWORKS 15 ///< Maximum number of networks to sort #define WS_MAX_ALT_WIFI_NETWORKS 3 ///< Maximum number of alternative networks From 537a70a83878cabc2f5d7e2404abe853f0a76221 Mon Sep 17 00:00:00 2001 From: tyeth Date: Tue, 10 Dec 2024 20:23:50 +0000 Subject: [PATCH 19/30] Move wifi defines to Ws_networking --- src/Wippersnapper.h | 5 -- src/Wippersnapper_Networking.h | 93 ++++++++++++++++++---------------- 2 files changed, 50 insertions(+), 48 deletions(-) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index d16534671..e7b32ce20 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -215,12 +215,7 @@ typedef enum { FSM_NET_ESTABLISH_MQTT, } fsm_net_t; -#ifndef WL_MAC_ADDR_LENGTH -#define WL_MAC_ADDR_LENGTH 6 // MAC address length - from RP2040 BSP -#endif #define WS_WDT_TIMEOUT 60000 ///< WDT timeout -#define WS_MAX_SORTED_NETWORKS 15 ///< Maximum number of networks to sort -#define WS_MAX_ALT_WIFI_NETWORKS 3 ///< Maximum number of alternative networks /* MQTT Configuration */ #define WS_KEEPALIVE_INTERVAL_MS \ 5000 ///< Session keepalive interval time, in milliseconds diff --git a/src/Wippersnapper_Networking.h b/src/Wippersnapper_Networking.h index 5e401a4ff..03a5c44d5 100644 --- a/src/Wippersnapper_Networking.h +++ b/src/Wippersnapper_Networking.h @@ -1,44 +1,51 @@ -/*! - * @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_SORTED_NETWORKS 15 ///< Maximum number of networks to sort +#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 \ No newline at end of file From f155cb1da801445d8e8e14b4cf50d4694b75aaa9 Mon Sep 17 00:00:00 2001 From: tyeth Date: Wed, 11 Dec 2024 20:10:45 +0000 Subject: [PATCH 20/30] Fix SAMD bug with tinyUSB attach being called second time --- src/Wippersnapper.cpp | 16 ++++++++++++++-- src/network_interfaces/Wippersnapper_AIRLIFT.h | 6 ++---- src/provisioning/tinyusb/Wippersnapper_FS.cpp | 5 +++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index f2567424d..0207510e4 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -2500,8 +2500,12 @@ void Wippersnapper::runNetFSM() { */ /**************************************************************************/ void Wippersnapper::haltError(String error, ws_led_status_t ledStatusColor) { - WS.enableWDT(5000); - for (;;) { + #ifdef USE_TINYUSB + if (!TinyUSBDevice.mounted()) { + TinyUSBDevice.attach(); // calling when already attached breaks SAMD + } + #endif + for (int i=0;;i++) { WS_DEBUG_PRINT("ERROR [WDT RESET]: "); WS_DEBUG_PRINTLN(error); // let the WDT fail out and reset! @@ -2513,6 +2517,12 @@ void Wippersnapper::haltError(String error, ws_led_status_t ledStatusColor) { // hardware and software watchdog timers, delayMicroseconds does not. delayMicroseconds(1000000); #endif + if (i < 20) { + yield(); + WS.feedWDT(); // feed the WDT for the first 20 seconds + } else if (i == 20) { + WS.enableWDT(5000); + } } } @@ -2712,8 +2722,10 @@ void print_reset_reason(int reason) { */ /**************************************************************************/ void printDeviceInfo() { + WS_PRINTER.flush(); WS_DEBUG_PRINTLN("-------Device Information-------"); WS_DEBUG_PRINT("Firmware Version: "); + WS_PRINTER.flush(); WS_DEBUG_PRINTLN(WS_VERSION); WS_DEBUG_PRINT("Board ID: "); WS_DEBUG_PRINTLN(BOARD_ID); diff --git a/src/network_interfaces/Wippersnapper_AIRLIFT.h b/src/network_interfaces/Wippersnapper_AIRLIFT.h index 84a220760..6f1b96a77 100644 --- a/src/network_interfaces/Wippersnapper_AIRLIFT.h +++ b/src/network_interfaces/Wippersnapper_AIRLIFT.h @@ -181,14 +181,12 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { // 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 (uint8_t i = 0; i < n; ++i) - { + for (uint8_t i = 0; i < n; ++i) { WS_DEBUG_PRINT(WiFi.SSID(i)); WS_DEBUG_PRINT(" ("); uint8_t BSSID[WL_MAC_ADDR_LENGTH]; WiFi.BSSID(i, BSSID); - for (int m = 0; m < WL_MAC_ADDR_LENGTH; m++) - { + for (int m = 0; m < WL_MAC_ADDR_LENGTH; m++) { if (m != 0) WS_DEBUG_PRINT(":"); WS_DEBUG_PRINTHEX(BSSID[m]); diff --git a/src/provisioning/tinyusb/Wippersnapper_FS.cpp b/src/provisioning/tinyusb/Wippersnapper_FS.cpp index e4fa0ee72..b2111cf9a 100644 --- a/src/provisioning/tinyusb/Wippersnapper_FS.cpp +++ b/src/provisioning/tinyusb/Wippersnapper_FS.cpp @@ -217,7 +217,12 @@ void Wippersnapper_FS::initUSBMSC() { // init MSC usb_msc.begin(); + // Attach MSC and wait for enumeration + if (TinyUSBDevice.mounted()) { + TinyUSBDevice.detach(); + delay(10); + } TinyUSBDevice.attach(); delay(500); } From a74184648e956f56c950ea2e2123ea6987744c03 Mon Sep 17 00:00:00 2001 From: tyeth Date: Wed, 11 Dec 2024 22:06:02 +0000 Subject: [PATCH 21/30] Attempt to get Huzzah matching arduino-cli ram usage --- platformio.ini | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/platformio.ini b/platformio.ini index dcb0cef64..7e1733d34 100644 --- a/platformio.ini +++ b/platformio.ini @@ -415,8 +415,19 @@ board_build.f_cpu = 80000000L ; Arduino CLI uses this from adafruit_ci#ci-wippersnapper ; esp8266:esp8266:huzzah:xtal=80,vt=flash,exception=disabled,stacksmash=disabled,ssl=all,mmu=3232,non32xfer=fast,eesz=4M2M,ip=lm2f,dbg=Disabled,lvl=None____,wipe=none,baud=115200 build_flags = + -Wl,--gc-sections + -Wl,-Map=output.map -DARDUINO_ESP8266_ADAFRUIT_HUZZAH -DDEBUG_ESP_PORT=Serial + -DVTABLES_IN_FLASH + -DNO_EXCEPTIONS + -DNO_STACK_SMASH_PROTECTION + -DSSL_ALL + -DMMU_3232 + -DNON32XFER_FAST + -DDEBUG_DISABLED + -DDEBUG_LEVEL_NONE +board_build.eesz=4M2M board_build.filesystem = littlefs upload_port = /dev/cu.SLAB_USBtoUART From 5aa4a7334df5274fede2bf6c173d2b7b9d3c3f42 Mon Sep 17 00:00:00 2001 From: tyeth Date: Wed, 11 Dec 2024 22:07:18 +0000 Subject: [PATCH 22/30] Correct tinyusb attach/detach usage --- src/Wippersnapper.cpp | 9 +++------ src/provisioning/tinyusb/Wippersnapper_FS.cpp | 5 ++--- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 0207510e4..a52d4a53f 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -2500,13 +2500,10 @@ void Wippersnapper::runNetFSM() { */ /**************************************************************************/ void Wippersnapper::haltError(String error, ws_led_status_t ledStatusColor) { - #ifdef USE_TINYUSB - if (!TinyUSBDevice.mounted()) { - TinyUSBDevice.attach(); // calling when already attached breaks SAMD - } - #endif for (int i=0;;i++) { - WS_DEBUG_PRINT("ERROR [WDT RESET]: "); + WS_DEBUG_PRINT("ERROR [WDT RESET IN "); + WS_DEBUG_PRINT(25 - i); + WS_DEBUG_PRINTLN("]: "); WS_DEBUG_PRINTLN(error); // let the WDT fail out and reset! statusLEDSolid(ledStatusColor); diff --git a/src/provisioning/tinyusb/Wippersnapper_FS.cpp b/src/provisioning/tinyusb/Wippersnapper_FS.cpp index b2111cf9a..92c348360 100644 --- a/src/provisioning/tinyusb/Wippersnapper_FS.cpp +++ b/src/provisioning/tinyusb/Wippersnapper_FS.cpp @@ -106,6 +106,7 @@ Wippersnapper_FS::Wippersnapper_FS() { // If a filesystem does not already exist - attempt to initialize a new // filesystem if (!initFilesystem() && !initFilesystem(true)) { + TinyUSBDevice.attach(); setStatusLEDColor(RED); fsHalt("ERROR Initializing Filesystem"); } @@ -222,8 +223,8 @@ void Wippersnapper_FS::initUSBMSC() { if (TinyUSBDevice.mounted()) { TinyUSBDevice.detach(); delay(10); + TinyUSBDevice.attach(); } - TinyUSBDevice.attach(); delay(500); } @@ -490,8 +491,6 @@ void Wippersnapper_FS::writeToBootOut(PGM_P str) { */ /**************************************************************************/ void Wippersnapper_FS::fsHalt(String msg) { - TinyUSBDevice.attach(); - delay(500); statusLEDSolid(WS_LED_STATUS_FS_WRITE); while (1) { WS_DEBUG_PRINTLN("Fatal Error: Halted execution!"); From 4d4dea23d450923e1f7de6fb87f28d7bbdb27fd5 Mon Sep 17 00:00:00 2001 From: tyeth Date: Wed, 11 Dec 2024 22:10:49 +0000 Subject: [PATCH 23/30] Correct ++i to i++ for esp8266 --- src/network_interfaces/Wippersnapper_ESP8266.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/network_interfaces/Wippersnapper_ESP8266.h b/src/network_interfaces/Wippersnapper_ESP8266.h index 4032df142..0d162ec4e 100644 --- a/src/network_interfaces/Wippersnapper_ESP8266.h +++ b/src/network_interfaces/Wippersnapper_ESP8266.h @@ -23,6 +23,7 @@ #include "ESP8266WiFi.h" #include "ESP8266WiFiMulti.h" #include "Wippersnapper.h" +#include "Wippersnapper_Networking.h" /* NOTE - Projects that require "Secure MQTT" (TLS/SSL) also require a new * SSL certificate every year. If adding Secure MQTT to your ESP8266 project is @@ -160,7 +161,7 @@ class Wippersnapper_ESP8266 : public Wippersnapper { WiFiNetwork networks[WS_MAX_SORTED_NETWORKS]; uint8_t numSavedNetworks = 0; // Store the scanned networks in the vector - for (int i = 0; i < n; ++i) { + for (int i = 0; i < n; i++) { if (i < WS_MAX_SORTED_NETWORKS) { strncpy(networks[i].ssid, WiFi.SSID(i).c_str(), sizeof(networks[i].ssid)); @@ -182,7 +183,7 @@ class Wippersnapper_ESP8266 : public Wippersnapper { std::sort(networks, networks + numSavedNetworks, compareByRSSI); // Was the network within secrets.json found? - for (int i = 0; i < numSavedNetworks; ++i) { + for (int i = 0; i < numSavedNetworks; i++) { if (strcmp(_ssid, networks[i].ssid) == 0) { WS_DEBUG_PRINT("SSID ("); WS_DEBUG_PRINT(_ssid); @@ -207,7 +208,7 @@ class Wippersnapper_ESP8266 : public Wippersnapper { // User-set network not found, print scan results to serial console WS_DEBUG_PRINTLN("ERROR: Your WiFi network was not found!"); WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks:"); - for (uint8_t i = 0; i < n; ++i) + for (uint8_t i = 0; i < n; i++) { WS_DEBUG_PRINT(WiFi.SSID(i)); WS_DEBUG_PRINT(" ("); From 21f893a19551ebef74a7cf717ecc1950b34e9679 Mon Sep 17 00:00:00 2001 From: tyeth Date: Wed, 11 Dec 2024 22:27:15 +0000 Subject: [PATCH 24/30] Remove scan print sort in favour of BSSID/RSSI/Channel (airlift) --- .../Wippersnapper_AIRLIFT.h | 49 +++++-------------- 1 file changed, 11 insertions(+), 38 deletions(-) diff --git a/src/network_interfaces/Wippersnapper_AIRLIFT.h b/src/network_interfaces/Wippersnapper_AIRLIFT.h index 6f1b96a77..056e37bb5 100644 --- a/src/network_interfaces/Wippersnapper_AIRLIFT.h +++ b/src/network_interfaces/Wippersnapper_AIRLIFT.h @@ -143,45 +143,14 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { return false; } - WiFiNetwork networks[WS_MAX_SORTED_NETWORKS]; - uint8_t numSavedNetworks = 0; - - // Store the scanned networks in the array - for (int i = 0; i < n; ++i) { - if (i < WS_MAX_SORTED_NETWORKS) { - strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid)); - networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; - networks[i].rssi = WiFi.RSSI(i); - numSavedNetworks++; - } else { - WS_DEBUG_PRINT("ERROR: Too many networks found! (>"); - WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS); - WS_DEBUG_PRINT(") Ignoring "); - WS_DEBUG_PRINT(WiFi.SSID(i)); - WS_DEBUG_PRINT("("); - WS_DEBUG_PRINT(WiFi.RSSI(i)); - WS_DEBUG_PRINTLN(")"); - } - } + bool foundNetwork = false; - // Sort the networks by RSSI in descending order - std::sort(networks, networks + numSavedNetworks, compareByRSSI); - - // Was the network within secrets.json found? - for (int i = 0; i < numSavedNetworks; ++i) { - if (strcmp(_ssid, networks[i].ssid) == 0) { - WS_DEBUG_PRINT("SSID ("); - WS_DEBUG_PRINT(_ssid); - WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(networks[i].rssi); - return 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 (uint8_t i = 0; i < n; ++i) { + for (uint8_t i = 0; i < n; i++) { + if (strcmp(WiFi.SSID(i), _ssid) == 0) { + foundNetwork = true; + break; + } WS_DEBUG_PRINT(WiFi.SSID(i)); WS_DEBUG_PRINT(" ("); uint8_t BSSID[WL_MAC_ADDR_LENGTH]; @@ -198,7 +167,11 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { WS_DEBUG_PRINTLN(")"); } - return false; + if(foundNetwork == 0) { + WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!"); + return false; + } + return true; } /********************************************************/ From f2ea9cdef4de85aa7a5074942f1d398a20593aff Mon Sep 17 00:00:00 2001 From: tyeth Date: Wed, 11 Dec 2024 22:45:23 +0000 Subject: [PATCH 25/30] Pico1w not reattaching --- src/provisioning/tinyusb/Wippersnapper_FS.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/provisioning/tinyusb/Wippersnapper_FS.cpp b/src/provisioning/tinyusb/Wippersnapper_FS.cpp index 92c348360..c255498f3 100644 --- a/src/provisioning/tinyusb/Wippersnapper_FS.cpp +++ b/src/provisioning/tinyusb/Wippersnapper_FS.cpp @@ -223,8 +223,8 @@ void Wippersnapper_FS::initUSBMSC() { if (TinyUSBDevice.mounted()) { TinyUSBDevice.detach(); delay(10); - TinyUSBDevice.attach(); } + TinyUSBDevice.attach(); delay(500); } From 2ac176742fcec9f5ca25510578ec85fb817d6c88 Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 12 Dec 2024 00:27:52 +0000 Subject: [PATCH 26/30] include defines --- src/network_interfaces/ws_networking_pico.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/network_interfaces/ws_networking_pico.h b/src/network_interfaces/ws_networking_pico.h index 43b35900e..b6b7136d3 100644 --- a/src/network_interfaces/ws_networking_pico.h +++ b/src/network_interfaces/ws_networking_pico.h @@ -23,6 +23,7 @@ #define PICO_CONNECT_RETRY_DELAY_MS 200 /*!< delay time between retries. */ #include "Wippersnapper.h" +#include "Wippersnapper_Networking.h" #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" From 4aa19d832f9c8d3dcc1962c9d0b3204444f02eae Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 12 Dec 2024 17:51:45 +0000 Subject: [PATCH 27/30] Clear null secrets as some text readers wont open binary --- src/provisioning/tinyusb/Wippersnapper_FS.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/provisioning/tinyusb/Wippersnapper_FS.cpp b/src/provisioning/tinyusb/Wippersnapper_FS.cpp index c255498f3..a71495fd2 100644 --- a/src/provisioning/tinyusb/Wippersnapper_FS.cpp +++ b/src/provisioning/tinyusb/Wippersnapper_FS.cpp @@ -238,6 +238,13 @@ bool Wippersnapper_FS::configFileExists() { // Does secrets.json file exist? if (!wipperFatFs.exists("/secrets.json")) return false; + File32 file = wipperFatFs.open("/secrets.json", FILE_READ); + if (!file) + return false; + int firstChar = file.peek(); + file.close(); + if (firstChar <= 0 || firstChar == 255) + return false; return true; } @@ -324,7 +331,7 @@ bool Wippersnapper_FS::createBootFile() { void Wippersnapper_FS::createSecretsFile() { // Open file for writing File32 secretsFile = wipperFatFs.open("/secrets.json", FILE_WRITE); - + secretsFile.truncate(0); // Create a default secretsConfig structure secretsConfig secretsConfig; strcpy(secretsConfig.aio_user, "YOUR_IO_USERNAME_HERE"); @@ -339,7 +346,7 @@ void Wippersnapper_FS::createSecretsFile() { serializeJsonPretty(doc, secretsFile); secretsFile.flush(); secretsFile.close(); - + writeToBootOut( "ERROR: Please edit the secrets.json file. Then, reset your board.\n"); #ifdef USE_DISPLAY From 314fd3810237125129b4b47a3273f8c16353dfde Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 12 Dec 2024 22:09:53 +0000 Subject: [PATCH 28/30] Add ESP32 include --- src/network_interfaces/Wippersnapper_ESP32.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/network_interfaces/Wippersnapper_ESP32.h b/src/network_interfaces/Wippersnapper_ESP32.h index f42b112e4..f2c90306d 100644 --- a/src/network_interfaces/Wippersnapper_ESP32.h +++ b/src/network_interfaces/Wippersnapper_ESP32.h @@ -19,6 +19,7 @@ #ifdef ARDUINO_ARCH_ESP32 #include "Wippersnapper.h" +#include "Wippersnapper_Networking.h" #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" From eb5f05d1f50da69e90b615df2e26ed350723de92 Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 12 Dec 2024 22:18:37 +0000 Subject: [PATCH 29/30] Flush serial when printing debug info --- src/Wippersnapper.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index e7b32ce20..3826f1ff0 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -48,11 +48,11 @@ // Define actual debug output functions when necessary. #ifdef WS_DEBUG #define WS_DEBUG_PRINT(...) \ - { WS_PRINTER.print(__VA_ARGS__); } ///< Prints debug output. + { WS_PRINTER.print(__VA_ARGS__); WS_PRINTER.flush(); } ///< Prints debug output. #define WS_DEBUG_PRINTLN(...) \ - { WS_PRINTER.println(__VA_ARGS__); } ///< Prints line from debug output. + { WS_PRINTER.println(__VA_ARGS__); WS_PRINTER.flush(); } ///< Prints line from debug output. #define WS_DEBUG_PRINTHEX(...) \ - { WS_PRINTER.print(__VA_ARGS__, HEX); } ///< Prints debug output. + { WS_PRINTER.print(__VA_ARGS__, HEX); WS_PRINTER.flush(); } ///< Prints debug output. #else #define WS_DEBUG_PRINT(...) \ {} ///< Prints debug output From f284c1653aaf482f77363fe55b55d379094fe2f2 Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 12 Dec 2024 22:35:30 +0000 Subject: [PATCH 30/30] Stop SAMD21 from flushing (overflows flash with extra routine) --- src/Wippersnapper.h | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 3826f1ff0..d175c9963 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -47,17 +47,28 @@ // Define actual debug output functions when necessary. #ifdef WS_DEBUG +#if defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_SAMD_MKRWIFI1010) #define WS_DEBUG_PRINT(...) \ - { WS_PRINTER.print(__VA_ARGS__); WS_PRINTER.flush(); } ///< Prints debug output. + { WS_PRINTER.print(__VA_ARGS__); yield(); } ///< Prints line from debug output. #define WS_DEBUG_PRINTLN(...) \ - { WS_PRINTER.println(__VA_ARGS__); WS_PRINTER.flush(); } ///< Prints line from debug output. + { WS_PRINTER.println(__VA_ARGS__); yield(); } ///< Prints line from debug output. #define WS_DEBUG_PRINTHEX(...) \ - { WS_PRINTER.print(__VA_ARGS__, HEX); WS_PRINTER.flush(); } ///< Prints debug output. + { WS_PRINTER.print(__VA_ARGS__, HEX); yield(); } ///< Prints debug output. +#else +#define WS_DEBUG_PRINT(...) \ + { WS_PRINTER.print(__VA_ARGS__); WS_PRINTER.flush(); yield(); } ///< Prints line from debug output. +#define WS_DEBUG_PRINTLN(...) \ + { WS_PRINTER.println(__VA_ARGS__); WS_PRINTER.flush(); yield(); } ///< Prints line from debug output. +#define WS_DEBUG_PRINTHEX(...) \ + { WS_PRINTER.print(__VA_ARGS__, HEX); WS_PRINTER.flush(); yield(); } ///< Prints debug output. +#endif #else #define WS_DEBUG_PRINT(...) \ {} ///< Prints debug output #define WS_DEBUG_PRINTLN(...) \ {} ///< Prints line from debug output. +#define WS_DEBUG_PRINTHEX(...) \ + {} ///< Prints debug output. #endif #define WS_DELAY_WITH_WDT(timeout) \