Skip to content

Commit 5c645ad

Browse files
committed
Update pico and WIFININA(arduino versions)
1 parent 301f71f commit 5c645ad

File tree

2 files changed

+67
-33
lines changed

2 files changed

+67
-33
lines changed

src/network_interfaces/Wippersnapper_WIFININA.h

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -148,44 +148,61 @@ class Wippersnapper_WIFININA : public Wippersnapper {
148148
delay(100);
149149

150150
// Perform a network scan
151-
int n = WiFi.scanNetworks();
151+
int8_t n = WiFi.scanNetworks();
152152
if (n == 0) {
153153
WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!");
154154
return false;
155155
}
156156

157-
// Dynamically allocate memory for the network list
158-
std::vector<WiFiNetwork> networks(n);
157+
WiFiNetwork networks[WS_MAX_SORTED_NETWORKS];
158+
uint8_t numSavedNetworks = 0;
159159

160160
// Store the scanned networks in the vector
161161
for (int i = 0; i < n; ++i) {
162-
strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1);
163-
networks[i].ssid[sizeof(networks[i].ssid) - 1] =
164-
'\0'; // Ensure null termination
165-
networks[i].rssi = WiFi.RSSI(i);
162+
if (i < WS_MAX_SORTED_NETWORKS) {
163+
strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid));
164+
networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0';
165+
networks[i].rssi = WiFi.RSSI(i);
166+
numSavedNetworks++;
167+
} else {
168+
WS_DEBUG_PRINT("ERROR: Too many networks found! (>");
169+
WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS);
170+
WS_DEBUG_PRINT(") Ignoring ");
171+
WS_DEBUG_PRINT(WiFi.SSID(i));
172+
WS_DEBUG_PRINT("(");
173+
WS_DEBUG_PRINT(WiFi.RSSI(i));
174+
WS_DEBUG_PRINTLN(")");
175+
}
166176
}
167177

168-
// Sort the networks by RSSI in descending order
169-
std::sort(networks.begin(), networks.end(), compareByRSSI);
178+
/// Sort the networks by RSSI in descending order
179+
std::sort(networks, networks + numSavedNetworks, compareByRSSI);
170180

171181
// Was the network within secrets.json found?
172-
for (const auto &network : networks) {
173-
if (strcmp(_ssid, network.ssid) == 0) {
182+
for (int i = 0; i < numSavedNetworks; ++i) {
183+
if (strcmp(_ssid, networks[i].ssid) == 0) {
174184
WS_DEBUG_PRINT("SSID (");
175185
WS_DEBUG_PRINT(_ssid);
176186
WS_DEBUG_PRINT(") found! RSSI: ");
177-
WS_DEBUG_PRINTLN(network.rssi);
187+
WS_DEBUG_PRINTLN(networks[i].rssi);
178188
return true;
179189
}
180190
}
181191

182192
// User-set network not found, print scan results to serial console
183193
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
184-
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: ");
185-
for (const auto &network : networks) {
186-
WS_DEBUG_PRINT(network.ssid);
194+
WS_DEBUG_PRINT("WipperSnapper found these WiFi networks");
195+
if (n > WS_MAX_SORTED_NETWORKS) {
196+
WS_DEBUG_PRINT(" (only first ");
197+
WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS);
198+
WS_DEBUG_PRINTLN(" used):");
199+
} else {
200+
WS_DEBUG_PRINTLN(":");
201+
}
202+
for (int i = 0; i < n; ++i) {
203+
WS_DEBUG_PRINT(networks[i].ssid);
187204
WS_DEBUG_PRINT(" ");
188-
WS_DEBUG_PRINT(network.rssi);
205+
WS_DEBUG_PRINT(networks[i].rssi);
189206
WS_DEBUG_PRINTLN("dB");
190207
}
191208

src/network_interfaces/ws_networking_pico.h

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -138,37 +138,47 @@ class ws_networking_pico : public Wippersnapper {
138138
return false;
139139
}
140140

141-
// Dynamically allocate memory for the network list
142-
std::vector<WiFiNetwork> networks(n);
141+
WiFiNetwork networks[WS_MAX_SORTED_NETWORKS];
142+
uint8_t numSavedNetworks = 0;
143143

144144
// Store the scanned networks in the vector
145145
for (int i = 0; i < n; ++i) {
146-
strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1);
147-
networks[i].ssid[sizeof(networks[i].ssid) - 1] =
148-
'\0'; // Ensure null termination
149-
networks[i].rssi = WiFi.RSSI(i);
146+
if (i < WS_MAX_SORTED_NETWORKS) {
147+
strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid));
148+
networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0';
149+
networks[i].rssi = WiFi.RSSI(i);
150+
numSavedNetworks++;
151+
} else {
152+
WS_DEBUG_PRINT("ERROR: Too many networks found! (>");
153+
WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS);
154+
WS_DEBUG_PRINT(") Ignoring ");
155+
WS_DEBUG_PRINT(WiFi.SSID(i));
156+
WS_DEBUG_PRINT("(");
157+
WS_DEBUG_PRINT(WiFi.RSSI(i));
158+
WS_DEBUG_PRINTLN(")");
159+
}
150160
}
151161

152-
// Sort the networks by RSSI in descending order
153-
std::sort(networks.begin(), networks.end(), compareByRSSI);
162+
/// Sort the networks by RSSI in descending order
163+
std::sort(networks, networks + numSavedNetworks, compareByRSSI);
154164

155165
// Was the network within secrets.json found?
156-
for (const auto &network : networks) {
157-
if (strcmp(_ssid, network.ssid) == 0) {
166+
for (int i = 0; i < numSavedNetworks; ++i) {
167+
if (strcmp(_ssid, networks[i].ssid) == 0) {
158168
WS_DEBUG_PRINT("SSID (");
159169
WS_DEBUG_PRINT(_ssid);
160170
WS_DEBUG_PRINT(") found! RSSI: ");
161-
WS_DEBUG_PRINTLN(network.rssi);
171+
WS_DEBUG_PRINTLN(networks[i].rssi);
162172
return true;
163173
}
164174
if (WS._isWiFiMulti) {
165175
// multi network mode
166176
for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) {
167-
if (strcmp(WS._multiNetworks[j].ssid, network.ssid) == 0) {
177+
if (strcmp(WS._multiNetworks[j].ssid, networks[i].ssid) == 0) {
168178
WS_DEBUG_PRINT("SSID (");
169179
WS_DEBUG_PRINT(WS._multiNetworks[j].ssid);
170180
WS_DEBUG_PRINT(") found! RSSI: ");
171-
WS_DEBUG_PRINTLN(network.rssi);
181+
WS_DEBUG_PRINTLN(networks[i].rssi);
172182
return true;
173183
}
174184
}
@@ -177,11 +187,18 @@ class ws_networking_pico : public Wippersnapper {
177187

178188
// User-set network not found, print scan results to serial console
179189
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
180-
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: ");
181-
for (const auto &network : networks) {
182-
WS_DEBUG_PRINT(network.ssid);
190+
WS_DEBUG_PRINT("WipperSnapper found these WiFi networks");
191+
if (n > WS_MAX_SORTED_NETWORKS) {
192+
WS_DEBUG_PRINT(" (only first ");
193+
WS_DEBUG_PRINT(WS_MAX_SORTED_NETWORKS);
194+
WS_DEBUG_PRINTLN(" used):");
195+
} else {
196+
WS_DEBUG_PRINTLN(":");
197+
}
198+
for (int i = 0; i < n; ++i) {
199+
WS_DEBUG_PRINT(networks[i].ssid);
183200
WS_DEBUG_PRINT(" ");
184-
WS_DEBUG_PRINT(network.rssi);
201+
WS_DEBUG_PRINT(networks[i].rssi);
185202
WS_DEBUG_PRINTLN("dB");
186203
}
187204

0 commit comments

Comments
 (0)