@@ -99,7 +99,7 @@ class Wippersnapper_ESP32 : public Wippersnapper {
9999 /* ***************************************************************/
100100 struct WiFiNetwork {
101101 char ssid[33 ]; /* !< SSID (Max 32 characters + null terminator */
102- int rssi; /* !< Received Signal Strength Indicator */
102+ int32_t rssi; /* !< Received Signal Strength Indicator */
103103 };
104104
105105 /* ******************************************************************/
@@ -146,38 +146,47 @@ class Wippersnapper_ESP32 : public Wippersnapper {
146146 return false ;
147147 }
148148
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 ;
152151 // Store the scanned networks in the vector
153152 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+ }
159168 }
160169
161170 // Sort the networks by RSSI in descending order
162- std::sort (networks. begin () , networks. end () , compareByRSSI);
171+ std::sort (networks, networks + numSavedNetworks , compareByRSSI);
163172
164173 // 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 ) {
167176 WS_DEBUG_PRINT (" SSID (" );
168177 WS_DEBUG_PRINT (_ssid);
169178 WS_DEBUG_PRINT (" ) found! RSSI: " );
170- WS_DEBUG_PRINTLN (network .rssi );
179+ WS_DEBUG_PRINTLN (networks[i] .rssi );
171180 return true ;
172181 }
173182 if (WS._isWiFiMulti ) {
174183 // multi network mode
175184 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 ) {
177186 WS_DEBUG_PRINT (" SSID (" );
178187 WS_DEBUG_PRINT (WS._multiNetworks [j].ssid );
179188 WS_DEBUG_PRINT (" ) found! RSSI: " );
180- WS_DEBUG_PRINTLN (network .rssi );
189+ WS_DEBUG_PRINTLN (networks[i] .rssi );
181190 return true ;
182191 }
183192 }
@@ -186,11 +195,18 @@ class Wippersnapper_ESP32 : public Wippersnapper {
186195
187196 // User-set network not found, print scan results to serial console
188197 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 );
192208 WS_DEBUG_PRINT (" " );
193- WS_DEBUG_PRINT (network .rssi );
209+ WS_DEBUG_PRINT (networks[i] .rssi );
194210 WS_DEBUG_PRINTLN (" dB" );
195211 }
196212
0 commit comments