Skip to content

Commit a4a4880

Browse files
committed
Define for max networks + _isWiFiMulti flag
1 parent 3c1af56 commit a4a4880

File tree

6 files changed

+53
-44
lines changed

6 files changed

+53
-44
lines changed

src/Wippersnapper.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ typedef enum {
162162
FSM_NET_ESTABLISH_MQTT,
163163
} fsm_net_t;
164164

165-
#define WS_WDT_TIMEOUT 60000 ///< WDT timeout
165+
#define WS_WDT_TIMEOUT 60000 ///< WDT timeout
166+
#define WS_MAX_ALT_WIFI_NETWORKS 3 ///< Maximum number of alternative networks
166167
/* MQTT Configuration */
167168
#define WS_KEEPALIVE_INTERVAL_MS \
168169
5000 ///< Session keepalive interval time, in milliseconds
@@ -317,6 +318,7 @@ class Wippersnapper {
317318

318319
secretsConfig _config; /*!< Wippersnapper secrets.json as a struct. */
319320
networkConfig _multiNetworks[3]; /*!< Wippersnapper networks as structs. */
321+
bool _isWiFiMulti = false; /*!< True if multiple networks are defined. */
320322

321323
// TODO: Does this need to be within this class?
322324
int32_t totalDigitalPins; /*!< Total number of digital-input capable pins */

src/network_interfaces/Wippersnapper_ESP32.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,17 @@ class Wippersnapper_ESP32 : public Wippersnapper {
110110

111111
// Was the network within secrets.json found?
112112
for (int i = 0; i < n; ++i) {
113-
if (strlen(WS._multiNetworks[0].ssid) > 0) {
113+
if (strcmp(_ssid, WiFi.SSID(i).c_str()) == 0) {
114+
return true;
115+
}
116+
if (WS._isWiFiMulti) {
114117
// multi network mode
115-
for (int j = 0; j < 5; j++) {
116-
if (strcmp(WS._multiNetworks[j].ssid, WiFi.SSID(i).c_str()) == 0)
118+
for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) {
119+
if (strcmp(WS._multiNetworks[j].ssid, WiFi.SSID(i).c_str()) == 0) {
117120
return true;
121+
}
118122
}
119-
} // else single network mode
120-
else if (strcmp(_ssid, WiFi.SSID(i).c_str()) == 0)
121-
return true;
123+
}
122124
}
123125

124126
// User-set network not found, print scan results to serial console
@@ -274,14 +276,14 @@ class Wippersnapper_ESP32 : public Wippersnapper {
274276
WiFi.setAutoReconnect(false);
275277
_disconnect();
276278
delay(100);
277-
if (strlen(WS._multiNetworks[0].ssid) > 0) {
279+
if (WS._isWiFiMulti) {
278280
// multi network mode
279281
_wifiMulti.APlistClean();
280282
_wifiMulti.setAllowOpenAP(false);
281283
// add default network
282284
_wifiMulti.addAP(_ssid, _pass);
283285
// add array of alternative networks
284-
for (int i = 0; i < 3; i++) {
286+
for (int i = 0; i < WS_MAX_ALT_WIFI_NETWORKS; i++) {
285287
if (strlen(WS._multiNetworks[i].ssid) > 0) {
286288
_wifiMulti.addAP(WS._multiNetworks[i].ssid,
287289
WS._multiNetworks[i].pass);

src/network_interfaces/Wippersnapper_ESP8266.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,16 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
136136

137137
// Was the network within secrets.json found?
138138
for (int i = 0; i < n; ++i) {
139-
if (strlen(WS._multiNetworks[0].ssid) > 0) {
139+
if (strcmp(_ssid, WiFi.SSID(i).c_str()) == 0) {
140+
return true;
141+
}
142+
if (WS._isWiFiMulti) {
140143
// multi network mode
141-
for (int j = 0; j < 3; j++) {
144+
for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) {
142145
if (strcmp(WS._multiNetworks[j].ssid, WiFi.SSID(i).c_str()) == 0)
143146
return true;
144147
}
145-
} // else single network mode
146-
else if (strcmp(_ssid, WiFi.SSID(i).c_str()) == 0)
147-
return true;
148+
}
148149
}
149150

150151
// User-set network not found, print scan results to serial console
@@ -249,7 +250,7 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
249250

250251
if (strlen(WS._multiNetworks[0].ssid) > 0) {
251252
// multi network mode
252-
for (int i = 0; i < 5; i++) {
253+
for (int i = 0; i < WS_MAX_ALT_WIFI_NETWORKS; i++) {
253254
if (strlen(WS._multiNetworks[i].ssid) > 0 &&
254255
(_wifiMulti.existsAP(WS._multiNetworks[i].ssid) == false)) {
255256
// doesn't exist, add it
@@ -268,7 +269,7 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
268269
// ESP8266 WDT requires yield() during a busy-loop so it doesn't bite
269270
yield();
270271
}
271-
if (_wifiMulti.run(5000) == WL_CONNECTED) {
272+
if (WiFi.status() == WL_CONNECTED) {
272273
_status = WS_NET_CONNECTED;
273274
} else {
274275
_status = WS_NET_DISCONNECTED;

src/network_interfaces/ws_networking_pico.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,17 @@ class ws_networking_pico : public Wippersnapper {
108108

109109
// Was the network within secrets.json found?
110110
for (int i = 0; i < n; ++i) {
111-
if (strlen(WS._multiNetworks[0].ssid) > 0) {
111+
if (strcmp(_ssid, WiFi.SSID(i)) == 0) {
112+
return true;
113+
}
114+
if (WS._isWiFiMulti) {
112115
// multi network mode
113-
for (int j = 0; j < 3; j++) {
114-
if (strcmp(WS._multiNetworks[j].ssid, WiFi.SSID(i)) == 0)
116+
for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) {
117+
if (strcmp(WS._multiNetworks[j].ssid, WiFi.SSID(i)) == 0) {
115118
return true;
119+
}
116120
}
117-
} // else single network mode
118-
else if (strcmp(_ssid, WiFi.SSID(i)) == 0)
119-
return true;
121+
}
120122
}
121123

122124
// User-set network not found, print scan results to serial console
@@ -278,13 +280,13 @@ class ws_networking_pico : public Wippersnapper {
278280
_disconnect();
279281
delay(5000);
280282
WS.feedWDT();
281-
if (strlen(WS._multiNetworks[0].ssid) > 0) {
283+
if (WS._isWiFiMulti) {
282284
// multi network mode
283285
_wifiMulti.clearAPList();
284286
// add default network
285287
_wifiMulti.addAP(_ssid, _pass);
286288
// add array of alternative networks
287-
for (int i = 0; i < 5; i++) {
289+
for (int i = 0; i < WS_MAX_ALT_WIFI_NETWORKS; i++) {
288290
_wifiMulti.addAP(WS._multiNetworks[i].ssid,
289291
WS._multiNetworks[i].pass);
290292
}

src/provisioning/littlefs/WipperSnapper_LittleFS.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,29 @@ void WipperSnapper_LittleFS::parseSecrets() {
8080

8181
WS_DEBUG_PRINTLN("Found multiple wifi networks in secrets.json");
8282
// Parse network credentials from array in secrets
83-
JsonArray networks = doc["network_type_wifi"]["alternative_networks"];
84-
int8_t networkCount = networks.size();
83+
JsonArray altnetworks = doc["network_type_wifi"]["alternative_networks"];
84+
int8_t altNetworkCount = (int8_t)altnetworks.size();
8585
WS_DEBUG_PRINT("Network count: ");
86-
WS_DEBUG_PRINTLN(networkCount);
87-
if (networkCount == 0) {
88-
fsHalt(
89-
"ERROR: No networks found in network_type_wifi in secrets.json!");
86+
WS_DEBUG_PRINTLN(altNetworkCount);
87+
if (altNetworkCount == 0) {
88+
fsHalt("ERROR: No alternative network entries found under "
89+
"network_type_wifi.alternative_networks in secrets.json!");
9090
}
91-
// check if over 5, warn user and take first five
92-
for (int i = 0; i < networkCount; i++) {
91+
// check if over 3, warn user and take first three
92+
for (int i = 0; i < altNetworkCount; i++) {
9393
if (i >= 3) {
9494
WS_DEBUG_PRINT("WARNING: More than 3 networks in secrets.json, "
9595
"only the first 3 will be used. Not using ");
96-
WS_DEBUG_PRINTLN(networks[i]["network_ssid"].as<const char *>());
96+
WS_DEBUG_PRINTLN(altnetworks[i]["network_ssid"].as<const char *>());
9797
break;
9898
}
99-
convertFromJson(networks[i], WS._multiNetworks[i]);
99+
convertFromJson(altnetworks[i], WS._multiNetworks[i]);
100100
WS_DEBUG_PRINT("Added SSID: ");
101101
WS_DEBUG_PRINTLN(WS._multiNetworks[i].ssid);
102102
WS_DEBUG_PRINT("PASS: ");
103103
WS_DEBUG_PRINTLN(WS._multiNetworks[i].pass);
104104
}
105+
WS._isWiFiMulti = true;
105106
} else {
106107
fsHalt("ERROR: Unrecognised value type for "
107108
"network_type_wifi.alternative_networks in secrets.json!");

src/provisioning/tinyusb/Wippersnapper_FS.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -364,28 +364,29 @@ void Wippersnapper_FS::parseSecrets() {
364364

365365
WS_DEBUG_PRINTLN("Found multiple wifi networks in secrets.json");
366366
// Parse network credentials from array in secrets
367-
JsonArray networks = doc["network_type_wifi"]["alternative_networks"];
368-
int8_t networkCount = networks.size();
367+
JsonArray altnetworks = doc["network_type_wifi"]["alternative_networks"];
368+
int8_t altNetworkCount = (int8_t)altnetworks.size();
369369
WS_DEBUG_PRINT("Network count: ");
370-
WS_DEBUG_PRINTLN(networkCount);
371-
if (networkCount == 0) {
372-
fsHalt(
373-
"ERROR: No networks found in network_type_wifi in secrets.json!");
370+
WS_DEBUG_PRINTLN(altNetworkCount);
371+
if (altNetworkCount == 0) {
372+
fsHalt("ERROR: No alternative network entries found under "
373+
"network_type_wifi.alternative_networks in secrets.json!");
374374
}
375-
// check if over 5, warn user and take first five
376-
for (int i = 0; i < networkCount; i++) {
375+
// check if over 3, warn user and take first three
376+
for (int i = 0; i < altNetworkCount; i++) {
377377
if (i >= 3) {
378378
WS_DEBUG_PRINT("WARNING: More than 3 networks in secrets.json, "
379379
"only the first 3 will be used. Not using ");
380-
WS_DEBUG_PRINTLN(networks[i]["network_ssid"].as<const char *>());
380+
WS_DEBUG_PRINTLN(altnetworks[i]["network_ssid"].as<const char *>());
381381
break;
382382
}
383-
convertFromJson(networks[i], WS._multiNetworks[i]);
383+
convertFromJson(altnetworks[i], WS._multiNetworks[i]);
384384
WS_DEBUG_PRINT("Added SSID: ");
385385
WS_DEBUG_PRINTLN(WS._multiNetworks[i].ssid);
386386
WS_DEBUG_PRINT("PASS: ");
387387
WS_DEBUG_PRINTLN(WS._multiNetworks[i].pass);
388388
}
389+
WS._isWiFiMulti = true;
389390
} else {
390391
fsHalt("ERROR: Unrecognised value type for "
391392
"network_type_wifi.alternative_networks in secrets.json!");

0 commit comments

Comments
 (0)