diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index ede52322..0520406b 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -214,8 +214,7 @@ typedef enum { FSM_NET_ESTABLISH_MQTT, } fsm_net_t; -#define WS_WDT_TIMEOUT 60000 ///< WDT timeout -#define WS_MAX_ALT_WIFI_NETWORKS 3 ///< Maximum number of alternative networks +#define WS_WDT_TIMEOUT 60000 ///< WDT timeout /* MQTT Configuration */ #define WS_KEEPALIVE_INTERVAL_MS \ 5000 ///< Session keepalive interval time, in milliseconds diff --git a/src/Wippersnapper_Networking.h b/src/Wippersnapper_Networking.h index 5e401a4f..e5c2c75f 100644 --- a/src/Wippersnapper_Networking.h +++ b/src/Wippersnapper_Networking.h @@ -1,44 +1,49 @@ -/*! - * @file Wippersnapper_Networking.h - * - * This file includes network interfaces at compile-time. - * - * Adafruit invests time and resources providing this open source code, - * please support Adafruit and open-source hardware by purchasing - * products from Adafruit! - * - * Copyright (c) Brent Rubell 2020-2021 for Adafruit Industries. - * - * BSD license, all text here must be included in any redistribution. - * - */ - -#ifndef WIPPERSNAPPER_NETWORKING_H -#define WIPPERSNAPPER_NETWORKING_H - -#if defined(ADAFRUIT_METRO_M4_EXPRESS) || \ - defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) || defined(ADAFRUIT_PYPORTAL) || \ - defined(ADAFRUIT_PYPORTAL_M4_TITANO) || defined(USE_AIRLIFT) -#include "network_interfaces/Wippersnapper_AIRLIFT.h" -/** Nina-FW (adafruit fork) networking class */ -typedef Wippersnapper_AIRLIFT Wippersnapper_WiFi; -#elif defined(ARDUINO_ARCH_ESP8266) -#include "network_interfaces/Wippersnapper_ESP8266.h" -/** ESP8266's networking class */ -typedef Wippersnapper_ESP8266 Wippersnapper_WiFi; -#elif defined(ARDUINO_ARCH_ESP32) -#include "network_interfaces/Wippersnapper_ESP32.h" -/** ESP32's networking class */ -typedef Wippersnapper_ESP32 Wippersnapper_WiFi; -#elif defined(ARDUINO_ARCH_RP2040) -#include "network_interfaces/ws_networking_pico.h" -typedef ws_networking_pico Wippersnapper_WiFi; -#elif defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_SAMD_MKRWIFI1010) -/** Nina-FW (arduino) networking class */ -#include "network_interfaces/Wippersnapper_WIFININA.h" -typedef Wippersnapper_WIFININA Wippersnapper_WiFi; -#else -#warning "Must define network interface in config.h!" -#endif - +/*! + * @file Wippersnapper_Networking.h + * + * This file includes network interfaces at compile-time. + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit and open-source hardware by purchasing + * products from Adafruit! + * + * Copyright (c) Brent Rubell 2020-2021 for Adafruit Industries. + * + * BSD license, all text here must be included in any redistribution. + * + */ + +#ifndef WIPPERSNAPPER_NETWORKING_H +#define WIPPERSNAPPER_NETWORKING_H + +#ifndef WL_MAC_ADDR_LENGTH +#define WL_MAC_ADDR_LENGTH 6 ///< MAC address length - from RP2040 BSP +#endif +#define WS_MAX_ALT_WIFI_NETWORKS 3 ///< Maximum number of alternative networks + +#if defined(ADAFRUIT_METRO_M4_EXPRESS) || \ + defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) || defined(ADAFRUIT_PYPORTAL) || \ + defined(ADAFRUIT_PYPORTAL_M4_TITANO) || defined(USE_AIRLIFT) +#include "network_interfaces/Wippersnapper_AIRLIFT.h" +/** Nina-FW (adafruit fork) networking class */ +typedef Wippersnapper_AIRLIFT Wippersnapper_WiFi; +#elif defined(ARDUINO_ARCH_ESP8266) +#include "network_interfaces/Wippersnapper_ESP8266.h" +/** ESP8266's networking class */ +typedef Wippersnapper_ESP8266 Wippersnapper_WiFi; +#elif defined(ARDUINO_ARCH_ESP32) +#include "network_interfaces/Wippersnapper_ESP32.h" +/** ESP32's networking class */ +typedef Wippersnapper_ESP32 Wippersnapper_WiFi; +#elif defined(ARDUINO_ARCH_RP2040) +#include "network_interfaces/ws_networking_pico.h" +typedef ws_networking_pico Wippersnapper_WiFi; +#elif defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_SAMD_MKRWIFI1010) +/** Nina-FW (arduino) networking class */ +#include "network_interfaces/Wippersnapper_WIFININA.h" +typedef Wippersnapper_WIFININA Wippersnapper_WiFi; +#else +#warning "Must define network interface in config.h!" +#endif + #endif // WIPPERSNAPPER_NETWORKING_H \ No newline at end of file diff --git a/src/network_interfaces/Wippersnapper_AIRLIFT.h b/src/network_interfaces/Wippersnapper_AIRLIFT.h index 4c564b4e..a278d1fe 100644 --- a/src/network_interfaces/Wippersnapper_AIRLIFT.h +++ b/src/network_interfaces/Wippersnapper_AIRLIFT.h @@ -103,8 +103,8 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { /***********************************************************/ /*! - @brief Performs a scan of local WiFi networks. - @returns True if `_network_ssid` is found, False otherwise. + @brief Performs a scan of local WiFi networks. + @returns True if `_network_ssid` is found, False otherwise. */ /***********************************************************/ bool check_valid_ssid() { @@ -119,26 +119,33 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { return false; } - // Was the network within secrets.json found? - for (int i = 0; i < n; ++i) { - if (strcmp(_ssid, WiFi.SSID(i)) == 0) { - WS_DEBUG_PRINT("SSID found! RSSI: "); - WS_DEBUG_PRINTLN(WiFi.RSSI(i)); - return true; - } - } + bool foundNetwork = false; - // User-set network not found, print scan results to serial console - WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!"); - WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: "); - for (int i = 0; i < n; ++i) { + WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks:"); + for (uint8_t i = 0; i < n; i++) { + if (!foundNetwork && strcmp(WiFi.SSID(i), _ssid) == 0) { + foundNetwork = true; + } WS_DEBUG_PRINT(WiFi.SSID(i)); - WS_DEBUG_PRINT(" "); + WS_DEBUG_PRINT(" ("); + uint8_t BSSID[WL_MAC_ADDR_LENGTH]; + WiFi.BSSID(i, BSSID); + for (int m = 0; m < WL_MAC_ADDR_LENGTH; m++) { + if (m != 0) + WS_DEBUG_PRINT(":"); + WS_DEBUG_PRINTHEX(BSSID[m]); + } + WS_DEBUG_PRINT(") "); WS_DEBUG_PRINT(WiFi.RSSI(i)); - WS_DEBUG_PRINTLN("dB"); + WS_DEBUG_PRINT("dB (ch"); + WS_DEBUG_PRINT(WiFi.channel(i)) + WS_DEBUG_PRINTLN(")"); } - return false; + if (!foundNetwork) { + WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!"); + } + return foundNetwork; } /********************************************************/ @@ -323,6 +330,7 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { // validate co-processor's firmware version if (!firmwareCheck()) { + // TODO: see if there's a way to add to bootlog without usb reattach WS_DEBUG_PRINTLN("Please upgrade the firmware on the ESP module to the " "latest version."); } diff --git a/src/network_interfaces/Wippersnapper_ESP32.h b/src/network_interfaces/Wippersnapper_ESP32.h index 99c4d47b..98fc8ae0 100644 --- a/src/network_interfaces/Wippersnapper_ESP32.h +++ b/src/network_interfaces/Wippersnapper_ESP32.h @@ -120,40 +120,40 @@ class Wippersnapper_ESP32 : public Wippersnapper { return false; } - // Was the network within secrets.json found? - for (int i = 0; i < n; ++i) { - if (strcmp(_ssid, WiFi.SSID(i).c_str()) == 0) { - WS_DEBUG_PRINT("SSID ("); - WS_DEBUG_PRINT(_ssid); - WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(WiFi.RSSI(i)); - return true; - } - if (WS._isWiFiMulti) { + bool foundNetwork = false; + + WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks:"); + for (uint8_t i = 0; i < n; i++) { + if (!foundNetwork && strcmp(WiFi.SSID(i).c_str(), _ssid) == 0) { + foundNetwork = true; + } else if (!foundNetwork && WS._isWiFiMulti) { // multi network mode for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) { if (strcmp(WS._multiNetworks[j].ssid, WiFi.SSID(i).c_str()) == 0) { - WS_DEBUG_PRINT("SSID ("); - WS_DEBUG_PRINT(WS._multiNetworks[j].ssid); - WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(WiFi.RSSI(i)); - return true; + foundNetwork = true; } } } - } - - // User-set network not found, print scan results to serial console - WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!"); - WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: "); - for (int i = 0; i < n; ++i) { WS_DEBUG_PRINT(WiFi.SSID(i)); - WS_DEBUG_PRINT(" "); + WS_DEBUG_PRINT(" ("); + uint8_t BSSID[WL_MAC_ADDR_LENGTH]; + WiFi.BSSID(i, BSSID); + for (int m = 0; m < WL_MAC_ADDR_LENGTH; m++) { + if (m != 0) + WS_DEBUG_PRINT(":"); + WS_DEBUG_PRINTHEX(BSSID[m]); + } + WS_DEBUG_PRINT(") "); WS_DEBUG_PRINT(WiFi.RSSI(i)); - WS_DEBUG_PRINTLN("dB"); + WS_DEBUG_PRINT("dB (ch"); + WS_DEBUG_PRINT(WiFi.channel(i)) + WS_DEBUG_PRINTLN(")"); } - return false; + if (!foundNetwork) { + WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!"); + } + return foundNetwork; } /********************************************************/ diff --git a/src/network_interfaces/Wippersnapper_ESP8266.h b/src/network_interfaces/Wippersnapper_ESP8266.h index c7fa6b69..1a9989d0 100644 --- a/src/network_interfaces/Wippersnapper_ESP8266.h +++ b/src/network_interfaces/Wippersnapper_ESP8266.h @@ -133,40 +133,40 @@ class Wippersnapper_ESP8266 : public Wippersnapper { return false; } - // Was the network within secrets.json found? - for (int i = 0; i < n; ++i) { - if (strcmp(_ssid, WiFi.SSID(i).c_str()) == 0) { - WS_DEBUG_PRINT("SSID ("); - WS_DEBUG_PRINT(_ssid); - WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(WiFi.RSSI(i)); - return true; - } - if (WS._isWiFiMulti) { + bool foundNetwork = false; + + WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks:"); + for (uint8_t i = 0; i < n; i++) { + if (!foundNetwork && strcmp(WiFi.SSID(i).c_str(), _ssid) == 0) { + foundNetwork = true; + } else if (!foundNetwork && WS._isWiFiMulti) { // multi network mode for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) { if (strcmp(WS._multiNetworks[j].ssid, WiFi.SSID(i).c_str()) == 0) { - WS_DEBUG_PRINT("SSID ("); - WS_DEBUG_PRINT(WS._multiNetworks[j].ssid); - WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(WiFi.RSSI(i)); - return true; + foundNetwork = true; } } } - } - - // User-set network not found, print scan results to serial console - WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!"); - WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: "); - for (int i = 0; i < n; ++i) { WS_DEBUG_PRINT(WiFi.SSID(i)); - WS_DEBUG_PRINT(" "); + WS_DEBUG_PRINT(" ("); + uint8_t BSSID[WL_MAC_ADDR_LENGTH]; + memcpy(BSSID, WiFi.BSSID(i), WL_MAC_ADDR_LENGTH); + for (int m = 0; m < WL_MAC_ADDR_LENGTH; m++) { + if (m != 0) + WS_DEBUG_PRINT(":"); + WS_DEBUG_PRINTHEX(BSSID[m]); + } + WS_DEBUG_PRINT(") "); WS_DEBUG_PRINT(WiFi.RSSI(i)); - WS_DEBUG_PRINTLN("dB"); + WS_DEBUG_PRINT("dB (ch"); + WS_DEBUG_PRINT(WiFi.channel(i)) + WS_DEBUG_PRINTLN(")"); } - return false; + if (!foundNetwork) { + WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!"); + } + return foundNetwork; } /********************************************************/ @@ -260,7 +260,6 @@ class Wippersnapper_ESP8266 : public Wippersnapper { delay(100); // ESP8266 MUST be in STA mode to avoid device acting as client/server WiFi.mode(WIFI_STA); - WiFi.begin(_ssid, _pass); _status = WS_NET_DISCONNECTED; delay(100); @@ -274,38 +273,28 @@ class Wippersnapper_ESP8266 : public Wippersnapper { WS._multiNetworks[i].pass); } } - // add default network - if (_wifiMulti.existsAP(_ssid) == false) { - _wifiMulti.addAP(_ssid, _pass); - } - long startRetry = millis(); - WS_DEBUG_PRINTLN("CONNECTING"); - while (_wifiMulti.run(5000) != WL_CONNECTED && - millis() - startRetry < 10000) { - // ESP8266 WDT requires yield() during a busy-loop so it doesn't bite - yield(); - } - if (WiFi.status() == WL_CONNECTED) { - _status = WS_NET_CONNECTED; - } else { - _status = WS_NET_DISCONNECTED; - } + } + + // add default network + if (_wifiMulti.existsAP(_ssid) == false) { + _wifiMulti.addAP(_ssid, _pass); + } + + long startRetry = millis(); + WS_DEBUG_PRINTLN("CONNECTING"); + + while (_wifiMulti.run(5000) != WL_CONNECTED && + millis() - startRetry < 10000) { + // ESP8266 WDT requires yield() during a busy-loop so it doesn't bite + yield(); + } + + if (WiFi.status() == WL_CONNECTED) { + _status = WS_NET_CONNECTED; } else { - // single network mode - - // wait for a connection to be established - long startRetry = millis(); - WS_DEBUG_PRINTLN("CONNECTING"); - while (WiFi.status() != WL_CONNECTED && millis() - startRetry < 10000) { - // ESP8266 WDT requires yield() during a busy-loop so it doesn't bite - yield(); - } - if (WiFi.status() == WL_CONNECTED) { - _status = WS_NET_CONNECTED; - } else { - _status = WS_NET_DISCONNECTED; - } + _status = WS_NET_DISCONNECTED; } + WS.feedWDT(); } } diff --git a/src/network_interfaces/Wippersnapper_WIFININA.h b/src/network_interfaces/Wippersnapper_WIFININA.h index 74f50bff..29fa1691 100644 --- a/src/network_interfaces/Wippersnapper_WIFININA.h +++ b/src/network_interfaces/Wippersnapper_WIFININA.h @@ -111,8 +111,8 @@ class Wippersnapper_WIFININA : public Wippersnapper { /***********************************************************/ /*! - @brief Performs a scan of local WiFi networks. - @returns True if `_network_ssid` is found, False otherwise. + @brief Performs a scan of local WiFi networks. + @returns True if `_network_ssid` is found, False otherwise. */ /***********************************************************/ bool check_valid_ssid() { @@ -122,32 +122,39 @@ 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; } - // Was the network within secrets.json found? - for (int i = 0; i < n; ++i) { - if (strcmp(_ssid, WiFi.SSID(i)) == 0) { - WS_DEBUG_PRINT("SSID found! RSSI: "); - WS_DEBUG_PRINTLN(WiFi.RSSI(i)); - return true; - } - } + bool foundNetwork = false; - // User-set network not found, print scan results to serial console - WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!"); - WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: "); - for (int i = 0; i < n; ++i) { + WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks:"); + for (uint8_t i = 0; i < n; i++) { + if (!foundNetwork && strcmp(WiFi.SSID(i), _ssid) == 0) { + foundNetwork = true; + } WS_DEBUG_PRINT(WiFi.SSID(i)); - WS_DEBUG_PRINT(" "); + WS_DEBUG_PRINT(" ("); + uint8_t BSSID[WL_MAC_ADDR_LENGTH]; + WiFi.BSSID(i, BSSID); + for (int m = 0; m < WL_MAC_ADDR_LENGTH; m++) { + if (m != 0) + WS_DEBUG_PRINT(":"); + WS_DEBUG_PRINTHEX(BSSID[m]); + } + WS_DEBUG_PRINT(") "); WS_DEBUG_PRINT(WiFi.RSSI(i)); - WS_DEBUG_PRINTLN("dB"); + WS_DEBUG_PRINT("dB (ch"); + WS_DEBUG_PRINT(WiFi.channel(i)) + WS_DEBUG_PRINTLN(")"); } - return false; + if (!foundNetwork) { + WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!"); + } + return foundNetwork; } /********************************************************/ diff --git a/src/network_interfaces/ws_networking_pico.h b/src/network_interfaces/ws_networking_pico.h index 25ce278b..c6d90abc 100644 --- a/src/network_interfaces/ws_networking_pico.h +++ b/src/network_interfaces/ws_networking_pico.h @@ -112,40 +112,40 @@ class ws_networking_pico : public Wippersnapper { return false; } - // Was the network within secrets.json found? - for (int i = 0; i < n; ++i) { - if (strcmp(_ssid, WiFi.SSID(i)) == 0) { - WS_DEBUG_PRINT("SSID ("); - WS_DEBUG_PRINT(_ssid); - WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(WiFi.RSSI(i)); - return true; - } - if (WS._isWiFiMulti) { + bool foundNetwork = false; + + WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks:"); + for (uint8_t i = 0; i < n; i++) { + if (!foundNetwork && strcmp(WiFi.SSID(i), _ssid) == 0) { + foundNetwork = true; + } else if (!foundNetwork && WS._isWiFiMulti) { // multi network mode for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) { if (strcmp(WS._multiNetworks[j].ssid, WiFi.SSID(i)) == 0) { - WS_DEBUG_PRINT("SSID ("); - WS_DEBUG_PRINT(WS._multiNetworks[j].ssid); - WS_DEBUG_PRINT(") found! RSSI: "); - WS_DEBUG_PRINTLN(WiFi.RSSI(i)); - return true; + foundNetwork = true; } } } - } - - // User-set network not found, print scan results to serial console - WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!"); - WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: "); - for (int i = 0; i < n; ++i) { WS_DEBUG_PRINT(WiFi.SSID(i)); - WS_DEBUG_PRINT(" "); + WS_DEBUG_PRINT(" ("); + uint8_t BSSID[WL_MAC_ADDR_LENGTH]; + WiFi.BSSID(i, BSSID); + for (int m = 0; m < WL_MAC_ADDR_LENGTH; m++) { + if (m != 0) + WS_DEBUG_PRINT(":"); + WS_DEBUG_PRINTHEX(BSSID[m]); + } + WS_DEBUG_PRINT(") "); WS_DEBUG_PRINT(WiFi.RSSI(i)); - WS_DEBUG_PRINTLN("dB"); + WS_DEBUG_PRINT("dB (ch"); + WS_DEBUG_PRINT(WiFi.channel(i)) + WS_DEBUG_PRINTLN(")"); } - return false; + if (!foundNetwork) { + WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!"); + } + return foundNetwork; } /********************************************************/