Skip to content

Commit e78470e

Browse files
committed
Merge remote-tracking branch 'tyeth/wireless-rssi-sorting' into uat-xmas2024
2 parents 9af3867 + 0de83b9 commit e78470e

7 files changed

+191
-183
lines changed

src/Wippersnapper.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,7 @@ typedef enum {
214214
FSM_NET_ESTABLISH_MQTT,
215215
} fsm_net_t;
216216

217-
#define WS_WDT_TIMEOUT 60000 ///< WDT timeout
218-
#define WS_MAX_ALT_WIFI_NETWORKS 3 ///< Maximum number of alternative networks
217+
#define WS_WDT_TIMEOUT 60000 ///< WDT timeout
219218
/* MQTT Configuration */
220219
#define WS_KEEPALIVE_INTERVAL_MS \
221220
5000 ///< Session keepalive interval time, in milliseconds

src/Wippersnapper_Networking.h

+48-43
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,49 @@
1-
/*!
2-
* @file Wippersnapper_Networking.h
3-
*
4-
* This file includes network interfaces at compile-time.
5-
*
6-
* Adafruit invests time and resources providing this open source code,
7-
* please support Adafruit and open-source hardware by purchasing
8-
* products from Adafruit!
9-
*
10-
* Copyright (c) Brent Rubell 2020-2021 for Adafruit Industries.
11-
*
12-
* BSD license, all text here must be included in any redistribution.
13-
*
14-
*/
15-
16-
#ifndef WIPPERSNAPPER_NETWORKING_H
17-
#define WIPPERSNAPPER_NETWORKING_H
18-
19-
#if defined(ADAFRUIT_METRO_M4_EXPRESS) || \
20-
defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) || defined(ADAFRUIT_PYPORTAL) || \
21-
defined(ADAFRUIT_PYPORTAL_M4_TITANO) || defined(USE_AIRLIFT)
22-
#include "network_interfaces/Wippersnapper_AIRLIFT.h"
23-
/** Nina-FW (adafruit fork) networking class */
24-
typedef Wippersnapper_AIRLIFT Wippersnapper_WiFi;
25-
#elif defined(ARDUINO_ARCH_ESP8266)
26-
#include "network_interfaces/Wippersnapper_ESP8266.h"
27-
/** ESP8266's networking class */
28-
typedef Wippersnapper_ESP8266 Wippersnapper_WiFi;
29-
#elif defined(ARDUINO_ARCH_ESP32)
30-
#include "network_interfaces/Wippersnapper_ESP32.h"
31-
/** ESP32's networking class */
32-
typedef Wippersnapper_ESP32 Wippersnapper_WiFi;
33-
#elif defined(ARDUINO_ARCH_RP2040)
34-
#include "network_interfaces/ws_networking_pico.h"
35-
typedef ws_networking_pico Wippersnapper_WiFi;
36-
#elif defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_SAMD_MKRWIFI1010)
37-
/** Nina-FW (arduino) networking class */
38-
#include "network_interfaces/Wippersnapper_WIFININA.h"
39-
typedef Wippersnapper_WIFININA Wippersnapper_WiFi;
40-
#else
41-
#warning "Must define network interface in config.h!"
42-
#endif
43-
1+
/*!
2+
* @file Wippersnapper_Networking.h
3+
*
4+
* This file includes network interfaces at compile-time.
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Brent Rubell 2020-2021 for Adafruit Industries.
11+
*
12+
* BSD license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
16+
#ifndef WIPPERSNAPPER_NETWORKING_H
17+
#define WIPPERSNAPPER_NETWORKING_H
18+
19+
#ifndef WL_MAC_ADDR_LENGTH
20+
#define WL_MAC_ADDR_LENGTH 6 ///< MAC address length - from RP2040 BSP
21+
#endif
22+
#define WS_MAX_ALT_WIFI_NETWORKS 3 ///< Maximum number of alternative networks
23+
24+
#if defined(ADAFRUIT_METRO_M4_EXPRESS) || \
25+
defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) || defined(ADAFRUIT_PYPORTAL) || \
26+
defined(ADAFRUIT_PYPORTAL_M4_TITANO) || defined(USE_AIRLIFT)
27+
#include "network_interfaces/Wippersnapper_AIRLIFT.h"
28+
/** Nina-FW (adafruit fork) networking class */
29+
typedef Wippersnapper_AIRLIFT Wippersnapper_WiFi;
30+
#elif defined(ARDUINO_ARCH_ESP8266)
31+
#include "network_interfaces/Wippersnapper_ESP8266.h"
32+
/** ESP8266's networking class */
33+
typedef Wippersnapper_ESP8266 Wippersnapper_WiFi;
34+
#elif defined(ARDUINO_ARCH_ESP32)
35+
#include "network_interfaces/Wippersnapper_ESP32.h"
36+
/** ESP32's networking class */
37+
typedef Wippersnapper_ESP32 Wippersnapper_WiFi;
38+
#elif defined(ARDUINO_ARCH_RP2040)
39+
#include "network_interfaces/ws_networking_pico.h"
40+
typedef ws_networking_pico Wippersnapper_WiFi;
41+
#elif defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_SAMD_MKRWIFI1010)
42+
/** Nina-FW (arduino) networking class */
43+
#include "network_interfaces/Wippersnapper_WIFININA.h"
44+
typedef Wippersnapper_WIFININA Wippersnapper_WiFi;
45+
#else
46+
#warning "Must define network interface in config.h!"
47+
#endif
48+
4449
#endif // WIPPERSNAPPER_NETWORKING_H

src/network_interfaces/Wippersnapper_AIRLIFT.h

+25-17
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ class Wippersnapper_AIRLIFT : public Wippersnapper {
103103

104104
/***********************************************************/
105105
/*!
106-
@brief Performs a scan of local WiFi networks.
107-
@returns True if `_network_ssid` is found, False otherwise.
106+
@brief Performs a scan of local WiFi networks.
107+
@returns True if `_network_ssid` is found, False otherwise.
108108
*/
109109
/***********************************************************/
110110
bool check_valid_ssid() {
@@ -119,26 +119,33 @@ class Wippersnapper_AIRLIFT : public Wippersnapper {
119119
return false;
120120
}
121121

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

131-
// User-set network not found, print scan results to serial console
132-
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
133-
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: ");
134-
for (int i = 0; i < n; ++i) {
124+
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks:");
125+
for (uint8_t i = 0; i < n; i++) {
126+
if (!foundNetwork && strcmp(WiFi.SSID(i), _ssid) == 0) {
127+
foundNetwork = true;
128+
}
135129
WS_DEBUG_PRINT(WiFi.SSID(i));
136-
WS_DEBUG_PRINT(" ");
130+
WS_DEBUG_PRINT(" (");
131+
uint8_t BSSID[WL_MAC_ADDR_LENGTH];
132+
WiFi.BSSID(i, BSSID);
133+
for (int m = 0; m < WL_MAC_ADDR_LENGTH; m++) {
134+
if (m != 0)
135+
WS_DEBUG_PRINT(":");
136+
WS_DEBUG_PRINTHEX(BSSID[m]);
137+
}
138+
WS_DEBUG_PRINT(") ");
137139
WS_DEBUG_PRINT(WiFi.RSSI(i));
138-
WS_DEBUG_PRINTLN("dB");
140+
WS_DEBUG_PRINT("dB (ch");
141+
WS_DEBUG_PRINT(WiFi.channel(i))
142+
WS_DEBUG_PRINTLN(")");
139143
}
140144

141-
return false;
145+
if (!foundNetwork) {
146+
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
147+
}
148+
return foundNetwork;
142149
}
143150

144151
/********************************************************/
@@ -323,6 +330,7 @@ class Wippersnapper_AIRLIFT : public Wippersnapper {
323330

324331
// validate co-processor's firmware version
325332
if (!firmwareCheck()) {
333+
// TODO: see if there's a way to add to bootlog without usb reattach
326334
WS_DEBUG_PRINTLN("Please upgrade the firmware on the ESP module to the "
327335
"latest version.");
328336
}

src/network_interfaces/Wippersnapper_ESP32.h

+24-24
Original file line numberDiff line numberDiff line change
@@ -120,40 +120,40 @@ class Wippersnapper_ESP32 : public Wippersnapper {
120120
return false;
121121
}
122122

123-
// Was the network within secrets.json found?
124-
for (int i = 0; i < n; ++i) {
125-
if (strcmp(_ssid, WiFi.SSID(i).c_str()) == 0) {
126-
WS_DEBUG_PRINT("SSID (");
127-
WS_DEBUG_PRINT(_ssid);
128-
WS_DEBUG_PRINT(") found! RSSI: ");
129-
WS_DEBUG_PRINTLN(WiFi.RSSI(i));
130-
return true;
131-
}
132-
if (WS._isWiFiMulti) {
123+
bool foundNetwork = false;
124+
125+
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks:");
126+
for (uint8_t i = 0; i < n; i++) {
127+
if (!foundNetwork && strcmp(WiFi.SSID(i).c_str(), _ssid) == 0) {
128+
foundNetwork = true;
129+
} else if (!foundNetwork && WS._isWiFiMulti) {
133130
// multi network mode
134131
for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) {
135132
if (strcmp(WS._multiNetworks[j].ssid, WiFi.SSID(i).c_str()) == 0) {
136-
WS_DEBUG_PRINT("SSID (");
137-
WS_DEBUG_PRINT(WS._multiNetworks[j].ssid);
138-
WS_DEBUG_PRINT(") found! RSSI: ");
139-
WS_DEBUG_PRINTLN(WiFi.RSSI(i));
140-
return true;
133+
foundNetwork = true;
141134
}
142135
}
143136
}
144-
}
145-
146-
// User-set network not found, print scan results to serial console
147-
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
148-
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: ");
149-
for (int i = 0; i < n; ++i) {
150137
WS_DEBUG_PRINT(WiFi.SSID(i));
151-
WS_DEBUG_PRINT(" ");
138+
WS_DEBUG_PRINT(" (");
139+
uint8_t BSSID[WL_MAC_ADDR_LENGTH];
140+
WiFi.BSSID(i, BSSID);
141+
for (int m = 0; m < WL_MAC_ADDR_LENGTH; m++) {
142+
if (m != 0)
143+
WS_DEBUG_PRINT(":");
144+
WS_DEBUG_PRINTHEX(BSSID[m]);
145+
}
146+
WS_DEBUG_PRINT(") ");
152147
WS_DEBUG_PRINT(WiFi.RSSI(i));
153-
WS_DEBUG_PRINTLN("dB");
148+
WS_DEBUG_PRINT("dB (ch");
149+
WS_DEBUG_PRINT(WiFi.channel(i))
150+
WS_DEBUG_PRINTLN(")");
154151
}
155152

156-
return false;
153+
if (!foundNetwork) {
154+
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
155+
}
156+
return foundNetwork;
157157
}
158158

159159
/********************************************************/

src/network_interfaces/Wippersnapper_ESP8266.h

+44-55
Original file line numberDiff line numberDiff line change
@@ -133,40 +133,40 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
133133
return false;
134134
}
135135

136-
// Was the network within secrets.json found?
137-
for (int i = 0; i < n; ++i) {
138-
if (strcmp(_ssid, WiFi.SSID(i).c_str()) == 0) {
139-
WS_DEBUG_PRINT("SSID (");
140-
WS_DEBUG_PRINT(_ssid);
141-
WS_DEBUG_PRINT(") found! RSSI: ");
142-
WS_DEBUG_PRINTLN(WiFi.RSSI(i));
143-
return true;
144-
}
145-
if (WS._isWiFiMulti) {
136+
bool foundNetwork = false;
137+
138+
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks:");
139+
for (uint8_t i = 0; i < n; i++) {
140+
if (!foundNetwork && strcmp(WiFi.SSID(i).c_str(), _ssid) == 0) {
141+
foundNetwork = true;
142+
} else if (!foundNetwork && WS._isWiFiMulti) {
146143
// multi network mode
147144
for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) {
148145
if (strcmp(WS._multiNetworks[j].ssid, WiFi.SSID(i).c_str()) == 0) {
149-
WS_DEBUG_PRINT("SSID (");
150-
WS_DEBUG_PRINT(WS._multiNetworks[j].ssid);
151-
WS_DEBUG_PRINT(") found! RSSI: ");
152-
WS_DEBUG_PRINTLN(WiFi.RSSI(i));
153-
return true;
146+
foundNetwork = true;
154147
}
155148
}
156149
}
157-
}
158-
159-
// User-set network not found, print scan results to serial console
160-
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
161-
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: ");
162-
for (int i = 0; i < n; ++i) {
163150
WS_DEBUG_PRINT(WiFi.SSID(i));
164-
WS_DEBUG_PRINT(" ");
151+
WS_DEBUG_PRINT(" (");
152+
uint8_t BSSID[WL_MAC_ADDR_LENGTH];
153+
memcpy(BSSID, WiFi.BSSID(i), WL_MAC_ADDR_LENGTH);
154+
for (int m = 0; m < WL_MAC_ADDR_LENGTH; m++) {
155+
if (m != 0)
156+
WS_DEBUG_PRINT(":");
157+
WS_DEBUG_PRINTHEX(BSSID[m]);
158+
}
159+
WS_DEBUG_PRINT(") ");
165160
WS_DEBUG_PRINT(WiFi.RSSI(i));
166-
WS_DEBUG_PRINTLN("dB");
161+
WS_DEBUG_PRINT("dB (ch");
162+
WS_DEBUG_PRINT(WiFi.channel(i))
163+
WS_DEBUG_PRINTLN(")");
167164
}
168165

169-
return false;
166+
if (!foundNetwork) {
167+
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
168+
}
169+
return foundNetwork;
170170
}
171171

172172
/********************************************************/
@@ -260,7 +260,6 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
260260
delay(100);
261261
// ESP8266 MUST be in STA mode to avoid device acting as client/server
262262
WiFi.mode(WIFI_STA);
263-
WiFi.begin(_ssid, _pass);
264263
_status = WS_NET_DISCONNECTED;
265264
delay(100);
266265

@@ -274,38 +273,28 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
274273
WS._multiNetworks[i].pass);
275274
}
276275
}
277-
// add default network
278-
if (_wifiMulti.existsAP(_ssid) == false) {
279-
_wifiMulti.addAP(_ssid, _pass);
280-
}
281-
long startRetry = millis();
282-
WS_DEBUG_PRINTLN("CONNECTING");
283-
while (_wifiMulti.run(5000) != WL_CONNECTED &&
284-
millis() - startRetry < 10000) {
285-
// ESP8266 WDT requires yield() during a busy-loop so it doesn't bite
286-
yield();
287-
}
288-
if (WiFi.status() == WL_CONNECTED) {
289-
_status = WS_NET_CONNECTED;
290-
} else {
291-
_status = WS_NET_DISCONNECTED;
292-
}
276+
}
277+
278+
// add default network
279+
if (_wifiMulti.existsAP(_ssid) == false) {
280+
_wifiMulti.addAP(_ssid, _pass);
281+
}
282+
283+
long startRetry = millis();
284+
WS_DEBUG_PRINTLN("CONNECTING");
285+
286+
while (_wifiMulti.run(5000) != WL_CONNECTED &&
287+
millis() - startRetry < 10000) {
288+
// ESP8266 WDT requires yield() during a busy-loop so it doesn't bite
289+
yield();
290+
}
291+
292+
if (WiFi.status() == WL_CONNECTED) {
293+
_status = WS_NET_CONNECTED;
293294
} else {
294-
// single network mode
295-
296-
// wait for a connection to be established
297-
long startRetry = millis();
298-
WS_DEBUG_PRINTLN("CONNECTING");
299-
while (WiFi.status() != WL_CONNECTED && millis() - startRetry < 10000) {
300-
// ESP8266 WDT requires yield() during a busy-loop so it doesn't bite
301-
yield();
302-
}
303-
if (WiFi.status() == WL_CONNECTED) {
304-
_status = WS_NET_CONNECTED;
305-
} else {
306-
_status = WS_NET_DISCONNECTED;
307-
}
295+
_status = WS_NET_DISCONNECTED;
308296
}
297+
309298
WS.feedWDT();
310299
}
311300
}

0 commit comments

Comments
 (0)