Skip to content

Commit 768406a

Browse files
committed
WIP: esp32s2 connected, pico to test, airlift + 8266(?) to add
1 parent 1f8c507 commit 768406a

File tree

5 files changed

+145
-22
lines changed

5 files changed

+145
-22
lines changed

src/Wippersnapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ class Wippersnapper {
316316
Adafruit_MQTT *_mqtt; /*!< Reference to Adafruit_MQTT, _mqtt. */
317317

318318
secretsConfig _config; /*!< Wippersnapper secrets.json as a struct. */
319+
networkConfig _multiNetworks[5]; /*!< Wippersnapper network.json as a struct. */
319320

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

src/network_interfaces/Wippersnapper_ESP32.h

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "Adafruit_MQTT_Client.h"
2525
#include "Arduino.h"
2626
#include "WiFi.h"
27+
#include "WiFiMulti.h"
2728
#include <WiFiClientSecure.h>
2829
extern Wippersnapper WS;
2930

@@ -109,7 +110,14 @@ class Wippersnapper_ESP32 : public Wippersnapper {
109110

110111
// Was the network within secrets.json found?
111112
for (int i = 0; i < n; ++i) {
112-
if (strcmp(_ssid, WiFi.SSID(i).c_str()) == 0)
113+
if (strlen(WS._multiNetworks[0].ssid) > 0) {
114+
// multi network mode
115+
for (int j = 0; j < 5; j++) {
116+
if (strcmp(WS._multiNetworks[j].ssid, WiFi.SSID(i).c_str()) == 0)
117+
return true;
118+
}
119+
} // else single network mode
120+
else if (strcmp(_ssid, WiFi.SSID(i).c_str()) == 0)
113121
return true;
114122
}
115123

@@ -262,11 +270,32 @@ class Wippersnapper_ESP32 : public Wippersnapper {
262270
if (strlen(_ssid) == 0) {
263271
_status = WS_SSID_INVALID;
264272
} else {
273+
WiFi.setAutoReconnect(false);
265274
_disconnect();
266275
delay(100);
267-
WiFi.begin(_ssid, _pass);
268-
_status = WS_NET_DISCONNECTED;
269-
delay(5000);
276+
if (strlen(WS._multiNetworks[0].ssid) > 0) {
277+
// multi network mode
278+
_wifiMulti.APlistClean();
279+
_wifiMulti.setAllowOpenAP(false);
280+
for (int i = 0; i < 5; i++) {
281+
if (strlen(WS._multiNetworks[i].ssid) > 0) {
282+
_wifiMulti.addAP(WS._multiNetworks[i].ssid,
283+
WS._multiNetworks[i].pass);
284+
}
285+
}
286+
if (_wifiMulti.run(20000) == WL_CONNECTED) {
287+
_status = WS_NET_CONNECTED;
288+
} else {
289+
_status = WS_NET_CONNECT_FAILED;
290+
}
291+
} else {
292+
// single network mode
293+
WiFi.begin(_ssid, _pass);
294+
_status = WS_NET_DISCONNECTED;
295+
WS.feedWDT();
296+
delay(5000);
297+
}
298+
WS.feedWDT();
270299
}
271300
}
272301

@@ -279,6 +308,9 @@ class Wippersnapper_ESP32 : public Wippersnapper {
279308
WiFi.disconnect();
280309
delay(500);
281310
}
311+
312+
private:
313+
WiFiMulti _wifiMulti;
282314
};
283315

284316
#endif // ARDUINO_ARCH_ESP32_H

src/network_interfaces/ws_networking_pico.h

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,14 @@ 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 (strcmp(_ssid, WiFi.SSID(i)) == 0)
111+
if (strlen(WS._multiNetworks[0].ssid) > 0) {
112+
// multi network mode
113+
for (int j = 0; j < 5; j++) {
114+
if (strcmp(WS._multiNetworks[j].ssid, WiFi.SSID(i).c_str()) == 0)
115+
return true;
116+
}
117+
} // else single network mode
118+
else if (strcmp(_ssid, WiFi.SSID(i).c_str()) == 0)
112119
return true;
113120
}
114121

@@ -259,27 +266,40 @@ class ws_networking_pico : public Wippersnapper {
259266
if (WiFi.status() == WL_CONNECTED)
260267
return;
261268

269+
WiFi.mode(WIFI_STA);
270+
WS.feedWDT();
271+
WiFi.setTimeout(20000);
272+
WS.feedWDT();
273+
262274
if (strlen(_ssid) == 0) {
263275
_status = WS_SSID_INVALID;
264276
} else {
265277
_disconnect();
266278
delay(5000);
267279
WS.feedWDT();
268-
WiFi.mode(WIFI_STA);
269-
WS.feedWDT();
270-
WiFi.setTimeout(20000);
271-
WS.feedWDT();
272-
WiFi.begin(_ssid, _pass);
273-
// Wait setTimeout duration for a connection and check if connected every
274-
// 5 seconds
275-
for (int i = 0; i < 4; i++) {
276-
WS.feedWDT();
277-
delay(5000);
278-
WS.feedWDT();
279-
if (WiFi.status() == WL_CONNECTED) {
280+
if (strlen(WS._multiNetworks[0].ssid) > 0) {
281+
// multi network mode
282+
for (int i = 0; i < 5; i++) {
283+
_wifiMulti.addAP(WS._multiNetworks[i].ssid,
284+
WS._multiNetworks[i].pass);
285+
}
286+
if (_wifiMulti.run(20000) == WL_CONNECTED) {
280287
_status = WS_NET_CONNECTED;
281288
return;
282289
}
290+
} else {
291+
WiFi.begin(_ssid, _pass);
292+
// Wait setTimeout duration for a connection and check if connected every
293+
// 5 seconds
294+
for (int i = 0; i < 4; i++) {
295+
WS.feedWDT();
296+
delay(5000);
297+
WS.feedWDT();
298+
if (WiFi.status() == WL_CONNECTED) {
299+
_status = WS_NET_CONNECTED;
300+
return;
301+
}
302+
}
283303
}
284304
_status = WS_NET_DISCONNECTED;
285305
}
@@ -296,6 +316,9 @@ class ws_networking_pico : public Wippersnapper {
296316
delay(5000);
297317
WS.feedWDT();
298318
}
319+
320+
private:
321+
WiFiMulti _wifiMulti;
299322
};
300323

301324
#endif // ARDUINO_ARCH_RP2040

src/provisioning/littlefs/WipperSnapper_LittleFS.cpp

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,58 @@ void WipperSnapper_LittleFS::parseSecrets() {
6868
error.c_str());
6969
}
7070

71-
if (doc.containsKey("network_type_wifi")) {
72-
if (doc["network_type_wifi"].is<JsonObjectConst>()) {
71+
if (doc.containsKey("network_type_wifi")) {
72+
if (doc["network_type_wifi"].is<JsonObject>()) {
7373
WS_DEBUG_PRINTLN("Found single wifi network in secrets.json");
7474
// Parse network credentials from secrets
7575
convertFromJson(doc["network_type_wifi"], WS._config.network);
76-
} else if (doc["network_type_wifi"].is<JsonArrayConst>()) {
76+
} else if (doc["network_type_wifi"].is<JsonArray>()) {
7777
WS_DEBUG_PRINTLN("Found multiple wifi networks in secrets.json");
7878
// Parse network credentials from secrets
7979
int8_t networkCount = doc["network_type_wifi"].size();
80+
WS_DEBUG_PRINT("Network count: ");
81+
WS_DEBUG_PRINTLN(networkCount);
82+
if (networkCount == 0) {
83+
fsHalt("ERROR: No networks found in network_type_wifi in secrets.json!");
84+
} else
85+
// check if over 5, warn user and take first five
86+
for (int i = 0; i < networkCount; i++) {
87+
if (i > 4) {
88+
WS_DEBUG_PRINT("WARNING: More than 5 networks in secrets.json, "
89+
"only the first 5 will be used. Not using ");
90+
WS_DEBUG_PRINTLN(doc["network_type_wifi"][i]["ssid"].as<const char*>());
91+
break;
92+
}
93+
convertFromJson(doc["network_type_wifi"][i], WS._multiNetworks[i]);
94+
WS_DEBUG_PRINT("Added SSID: ");
95+
WS_DEBUG_PRINTLN(WS._multiNetworks[i].ssid);
96+
WS_DEBUG_PRINT("PASS: ");
97+
WS_DEBUG_PRINTLN(WS._multiNetworks[i].pass);
98+
}
99+
100+
WS_DEBUG_PRINTLN("Parsing last network in array");
80101
convertFromJson(doc["network_type_wifi"][networkCount - 1],
81102
WS._config.network);
103+
WS_DEBUG_PRINTLN("Parsed last network in array");
104+
WS_DEBUG_PRINT("SSID: ");
105+
WS_DEBUG_PRINTLN(WS._config.network.ssid);
106+
WS_DEBUG_PRINT("PASS: ");
107+
WS_DEBUG_PRINTLN(WS._config.network.pass);
108+
WS_DEBUG_PRINTLN("removing network_type_wifi");
109+
doc.remove("network_type_wifi");
110+
WS_DEBUG_PRINTLN("removed network_type_wifi, creating new flat object");
111+
doc["network_type_wifi"].to<JsonObject>();
112+
WS_DEBUG_PRINTLN("created new object, setting network");
113+
doc["network_type_wifi"].set(WS._config.network);
114+
82115
} else {
83116
fsHalt("ERROR: Unrecognised value type for network_type_wifi in "
84117
"secrets.json!");
85118
}
86119
} else {
87120
fsHalt("ERROR: Could not find network_type_wifi in secrets.json!");
88121
}
122+
89123
// Extract a config struct from the JSON document
90124
WS._config = doc.as<secretsConfig>();
91125

src/provisioning/tinyusb/Wippersnapper_FS.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,16 +352,49 @@ void Wippersnapper_FS::parseSecrets() {
352352
}
353353

354354
if (doc.containsKey("network_type_wifi")) {
355-
if (doc["network_type_wifi"].is<JsonObjectConst>()) {
355+
if (doc["network_type_wifi"].is<JsonObject>()) {
356356
WS_DEBUG_PRINTLN("Found single wifi network in secrets.json");
357357
// Parse network credentials from secrets
358358
convertFromJson(doc["network_type_wifi"], WS._config.network);
359-
} else if (doc["network_type_wifi"].is<JsonArrayConst>()) {
359+
} else if (doc["network_type_wifi"].is<JsonArray>()) {
360360
WS_DEBUG_PRINTLN("Found multiple wifi networks in secrets.json");
361361
// Parse network credentials from secrets
362362
int8_t networkCount = doc["network_type_wifi"].size();
363+
WS_DEBUG_PRINT("Network count: ");
364+
WS_DEBUG_PRINTLN(networkCount);
365+
if (networkCount == 0) {
366+
fsHalt("ERROR: No networks found in network_type_wifi in secrets.json!");
367+
} else
368+
// check if over 5, warn user and take first five
369+
for (int i = 0; i < networkCount; i++) {
370+
if (i > 4) {
371+
WS_DEBUG_PRINT("WARNING: More than 5 networks in secrets.json, "
372+
"only the first 5 will be used. Not using ");
373+
WS_DEBUG_PRINTLN(doc["network_type_wifi"][i]["ssid"].as<const char*>());
374+
break;
375+
}
376+
convertFromJson(doc["network_type_wifi"][i], WS._multiNetworks[i]);
377+
WS_DEBUG_PRINT("Added SSID: ");
378+
WS_DEBUG_PRINTLN(WS._multiNetworks[i].ssid);
379+
WS_DEBUG_PRINT("PASS: ");
380+
WS_DEBUG_PRINTLN(WS._multiNetworks[i].pass);
381+
}
382+
383+
WS_DEBUG_PRINTLN("Parsing last network in array");
363384
convertFromJson(doc["network_type_wifi"][networkCount - 1],
364385
WS._config.network);
386+
WS_DEBUG_PRINTLN("Parsed last network in array");
387+
WS_DEBUG_PRINT("SSID: ");
388+
WS_DEBUG_PRINTLN(WS._config.network.ssid);
389+
WS_DEBUG_PRINT("PASS: ");
390+
WS_DEBUG_PRINTLN(WS._config.network.pass);
391+
WS_DEBUG_PRINTLN("removing network_type_wifi");
392+
doc.remove("network_type_wifi");
393+
WS_DEBUG_PRINTLN("removed network_type_wifi, creating new flat object");
394+
doc["network_type_wifi"].to<JsonObject>();
395+
WS_DEBUG_PRINTLN("created new object, setting network");
396+
doc["network_type_wifi"].set(WS._config.network);
397+
365398
} else {
366399
fsHalt("ERROR: Unrecognised value type for network_type_wifi in "
367400
"secrets.json!");

0 commit comments

Comments
 (0)