2323#include " ESP8266WiFi.h"
2424#include " ESP8266WiFiMulti.h"
2525#include " Wippersnapper.h"
26- #include < algorithm>
27- #include < vector>
2826
2927/* NOTE - Projects that require "Secure MQTT" (TLS/SSL) also require a new
3028 * SSL certificate every year. If adding Secure MQTT to your ESP8266 project is
@@ -159,38 +157,47 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
159157 return false ;
160158 }
161159
162- // Dynamically allocate memory for the network list
163- std::vector<WiFiNetwork> networks (n);
164-
160+ WiFiNetwork networks[WS_MAX_SORTED_NETWORKS];
161+ uint8_t numSavedNetworks = 0 ;
165162 // Store the scanned networks in the vector
166163 for (int i = 0 ; i < n; ++i) {
167- strncpy (networks[i].ssid , WiFi.SSID (i).c_str (),
168- sizeof (networks[i].ssid ) - 1 );
169- networks[i].ssid [sizeof (networks[i].ssid ) - 1 ] =
170- ' \0 ' ; // Ensure null termination
171- networks[i].rssi = WiFi.RSSI (i);
164+ if (i < WS_MAX_SORTED_NETWORKS) {
165+ strncpy (networks[i].ssid , WiFi.SSID (i).c_str (),
166+ sizeof (networks[i].ssid ));
167+ networks[i].ssid [sizeof (networks[i].ssid ) - 1 ] = ' \0 ' ;
168+ networks[i].rssi = WiFi.RSSI (i);
169+ numSavedNetworks++;
170+ } else {
171+ WS_DEBUG_PRINT (" ERROR: Too many networks found! (>" );
172+ WS_DEBUG_PRINT (WS_MAX_SORTED_NETWORKS);
173+ WS_DEBUG_PRINT (" ) Ignoring " );
174+ WS_DEBUG_PRINT (WiFi.SSID (i));
175+ WS_DEBUG_PRINT (" (" );
176+ WS_DEBUG_PRINT (WiFi.RSSI (i));
177+ WS_DEBUG_PRINTLN (" )" );
178+ }
172179 }
173180
174181 // Sort the networks by RSSI in descending order
175- std::sort (networks. begin () , networks. end () , compareByRSSI);
182+ std::sort (networks, networks + numSavedNetworks , compareByRSSI);
176183
177184 // Was the network within secrets.json found?
178- for (const auto &network : networks ) {
179- if (strcmp (_ssid, network .ssid ) == 0 ) {
185+ for (int i = 0 ; i < numSavedNetworks; ++i ) {
186+ if (strcmp (_ssid, networks[i] .ssid ) == 0 ) {
180187 WS_DEBUG_PRINT (" SSID (" );
181188 WS_DEBUG_PRINT (_ssid);
182189 WS_DEBUG_PRINT (" ) found! RSSI: " );
183- WS_DEBUG_PRINTLN (network .rssi );
190+ WS_DEBUG_PRINTLN (networks[i] .rssi );
184191 return true ;
185192 }
186193 if (WS._isWiFiMulti ) {
187194 // multi network mode
188195 for (int j = 0 ; j < WS_MAX_ALT_WIFI_NETWORKS; j++) {
189- if (strcmp (WS._multiNetworks [j].ssid , network .ssid ) == 0 ) {
196+ if (strcmp (WS._multiNetworks [j].ssid , networks[i] .ssid ) == 0 ) {
190197 WS_DEBUG_PRINT (" SSID (" );
191198 WS_DEBUG_PRINT (WS._multiNetworks [j].ssid );
192199 WS_DEBUG_PRINT (" ) found! RSSI: " );
193- WS_DEBUG_PRINTLN (network .rssi );
200+ WS_DEBUG_PRINTLN (networks[i] .rssi );
194201 return true ;
195202 }
196203 }
@@ -199,11 +206,18 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
199206
200207 // User-set network not found, print scan results to serial console
201208 WS_DEBUG_PRINTLN (" ERROR: Your requested WiFi network was not found!" );
202- WS_DEBUG_PRINTLN (" WipperSnapper found these WiFi networks: " );
203- for (const auto &network : networks) {
204- WS_DEBUG_PRINT (network.ssid );
209+ WS_DEBUG_PRINT (" WipperSnapper found these WiFi networks" );
210+ if (n > WS_MAX_SORTED_NETWORKS) {
211+ WS_DEBUG_PRINT (" (only first " );
212+ WS_DEBUG_PRINT (WS_MAX_SORTED_NETWORKS);
213+ WS_DEBUG_PRINTLN (" used):" );
214+ } else {
215+ WS_DEBUG_PRINTLN (" :" );
216+ }
217+ for (int i = 0 ; i < n; ++i) {
218+ WS_DEBUG_PRINT (networks[i].ssid );
205219 WS_DEBUG_PRINT (" " );
206- WS_DEBUG_PRINT (network .rssi );
220+ WS_DEBUG_PRINT (networks[i] .rssi );
207221 WS_DEBUG_PRINTLN (" dB" );
208222 }
209223
0 commit comments