23
23
#include " ESP8266WiFi.h"
24
24
#include " ESP8266WiFiMulti.h"
25
25
#include " Wippersnapper.h"
26
- #include < algorithm>
27
- #include < vector>
28
26
29
27
/* NOTE - Projects that require "Secure MQTT" (TLS/SSL) also require a new
30
28
* SSL certificate every year. If adding Secure MQTT to your ESP8266 project is
@@ -159,38 +157,47 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
159
157
return false ;
160
158
}
161
159
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 ;
165
162
// Store the scanned networks in the vector
166
163
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
+ }
172
179
}
173
180
174
181
// Sort the networks by RSSI in descending order
175
- std::sort (networks. begin () , networks. end () , compareByRSSI);
182
+ std::sort (networks, networks + numSavedNetworks , compareByRSSI);
176
183
177
184
// 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 ) {
180
187
WS_DEBUG_PRINT (" SSID (" );
181
188
WS_DEBUG_PRINT (_ssid);
182
189
WS_DEBUG_PRINT (" ) found! RSSI: " );
183
- WS_DEBUG_PRINTLN (network .rssi );
190
+ WS_DEBUG_PRINTLN (networks[i] .rssi );
184
191
return true ;
185
192
}
186
193
if (WS._isWiFiMulti ) {
187
194
// multi network mode
188
195
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 ) {
190
197
WS_DEBUG_PRINT (" SSID (" );
191
198
WS_DEBUG_PRINT (WS._multiNetworks [j].ssid );
192
199
WS_DEBUG_PRINT (" ) found! RSSI: " );
193
- WS_DEBUG_PRINTLN (network .rssi );
200
+ WS_DEBUG_PRINTLN (networks[i] .rssi );
194
201
return true ;
195
202
}
196
203
}
@@ -199,11 +206,18 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
199
206
200
207
// User-set network not found, print scan results to serial console
201
208
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 );
205
219
WS_DEBUG_PRINT (" " );
206
- WS_DEBUG_PRINT (network .rssi );
220
+ WS_DEBUG_PRINT (networks[i] .rssi );
207
221
WS_DEBUG_PRINTLN (" dB" );
208
222
}
209
223
0 commit comments