@@ -99,7 +99,7 @@ class Wippersnapper_ESP32 : public Wippersnapper {
99
99
/* ***************************************************************/
100
100
struct WiFiNetwork {
101
101
char ssid[33 ]; /* !< SSID (Max 32 characters + null terminator */
102
- int rssi; /* !< Received Signal Strength Indicator */
102
+ int32_t rssi; /* !< Received Signal Strength Indicator */
103
103
};
104
104
105
105
/* ******************************************************************/
@@ -146,38 +146,47 @@ class Wippersnapper_ESP32 : public Wippersnapper {
146
146
return false ;
147
147
}
148
148
149
- // Dynamically allocate memory for the network list
150
- std::vector<WiFiNetwork> networks (n);
151
-
149
+ WiFiNetwork networks[WS_MAX_SORTED_NETWORKS];
150
+ uint8_t numSavedNetworks = 0 ;
152
151
// Store the scanned networks in the vector
153
152
for (int i = 0 ; i < n; ++i) {
154
- strncpy (networks[i].ssid , WiFi.SSID (i).c_str (),
155
- sizeof (networks[i].ssid ) - 1 );
156
- networks[i].ssid [sizeof (networks[i].ssid ) - 1 ] =
157
- ' \0 ' ; // Ensure null termination
158
- networks[i].rssi = WiFi.RSSI (i);
153
+ if (i < WS_MAX_SORTED_NETWORKS) {
154
+ strncpy (networks[i].ssid , WiFi.SSID (i).c_str (),
155
+ sizeof (networks[i].ssid ));
156
+ networks[i].ssid [sizeof (networks[i].ssid ) - 1 ] = ' \0 ' ;
157
+ networks[i].rssi = WiFi.RSSI (i);
158
+ numSavedNetworks++;
159
+ } else {
160
+ WS_DEBUG_PRINT (" ERROR: Too many networks found! (>" );
161
+ WS_DEBUG_PRINT (WS_MAX_SORTED_NETWORKS);
162
+ WS_DEBUG_PRINT (" ) Ignoring " );
163
+ WS_DEBUG_PRINT (WiFi.SSID (i));
164
+ WS_DEBUG_PRINT (" (" );
165
+ WS_DEBUG_PRINT (WiFi.RSSI (i));
166
+ WS_DEBUG_PRINTLN (" )" );
167
+ }
159
168
}
160
169
161
170
// Sort the networks by RSSI in descending order
162
- std::sort (networks. begin () , networks. end () , compareByRSSI);
171
+ std::sort (networks, networks + numSavedNetworks , compareByRSSI);
163
172
164
173
// Was the network within secrets.json found?
165
- for (const auto &network : networks ) {
166
- if (strcmp (_ssid, network .ssid ) == 0 ) {
174
+ for (int i = 0 ; i < numSavedNetworks; ++i ) {
175
+ if (strcmp (_ssid, networks[i] .ssid ) == 0 ) {
167
176
WS_DEBUG_PRINT (" SSID (" );
168
177
WS_DEBUG_PRINT (_ssid);
169
178
WS_DEBUG_PRINT (" ) found! RSSI: " );
170
- WS_DEBUG_PRINTLN (network .rssi );
179
+ WS_DEBUG_PRINTLN (networks[i] .rssi );
171
180
return true ;
172
181
}
173
182
if (WS._isWiFiMulti ) {
174
183
// multi network mode
175
184
for (int j = 0 ; j < WS_MAX_ALT_WIFI_NETWORKS; j++) {
176
- if (strcmp (WS._multiNetworks [j].ssid , network .ssid ) == 0 ) {
185
+ if (strcmp (WS._multiNetworks [j].ssid , networks[i] .ssid ) == 0 ) {
177
186
WS_DEBUG_PRINT (" SSID (" );
178
187
WS_DEBUG_PRINT (WS._multiNetworks [j].ssid );
179
188
WS_DEBUG_PRINT (" ) found! RSSI: " );
180
- WS_DEBUG_PRINTLN (network .rssi );
189
+ WS_DEBUG_PRINTLN (networks[i] .rssi );
181
190
return true ;
182
191
}
183
192
}
@@ -186,11 +195,18 @@ class Wippersnapper_ESP32 : public Wippersnapper {
186
195
187
196
// User-set network not found, print scan results to serial console
188
197
WS_DEBUG_PRINTLN (" ERROR: Your requested WiFi network was not found!" );
189
- WS_DEBUG_PRINTLN (" WipperSnapper found these WiFi networks: " );
190
- for (const auto &network : networks) {
191
- WS_DEBUG_PRINT (network.ssid );
198
+ WS_DEBUG_PRINT (" WipperSnapper found these WiFi networks" );
199
+ if (n > WS_MAX_SORTED_NETWORKS) {
200
+ WS_DEBUG_PRINT (" (only first " );
201
+ WS_DEBUG_PRINT (WS_MAX_SORTED_NETWORKS);
202
+ WS_DEBUG_PRINTLN (" used):" );
203
+ } else {
204
+ WS_DEBUG_PRINTLN (" :" );
205
+ }
206
+ for (int i = 0 ; i < n; ++i) {
207
+ WS_DEBUG_PRINT (networks[i].ssid );
192
208
WS_DEBUG_PRINT (" " );
193
- WS_DEBUG_PRINT (network .rssi );
209
+ WS_DEBUG_PRINT (networks[i] .rssi );
194
210
WS_DEBUG_PRINTLN (" dB" );
195
211
}
196
212
0 commit comments