1
- /* !
2
- * @file Wippersnapper_LittleFS.cpp
3
- *
4
- * Interfaces with LittleFS filesystem for ESP32, ESP8266 platforms.
5
- *
6
- * Adafruit invests time and resources providing this open source code,
7
- * please support Adafruit and open-source hardware by purchasing
8
- * products from Adafruit!
9
- *
10
- * Copyright (c) Brent Rubell 2021-2022 for Adafruit Industries.
11
- *
12
- * BSD license, all text here must be included in any redistribution.
13
- *
14
- */
15
- #if defined(ARDUINO_FEATHER_ESP32) || \
16
- defined (ARDUINO_ESP8266_ADAFRUIT_HUZZAH) || \
17
- defined(ARDUINO_ADAFRUIT_ITSYBITSY_ESP32) || \
18
- defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2) || \
19
- defined(ARDUINO_ADAFRUIT_QTPY_ESP32_PICO) || \
20
- defined(ARDUINO_ADAFRUIT_QTPY_ESP32C3)
21
- #include " WipperSnapper_LittleFS.h"
22
-
23
- /* *************************************************************************/
24
- /* !
25
- @brief Attempts to set up and initialize a pre-existing LittleFS
26
- filesystem.
27
- */
28
- /* *************************************************************************/
29
- WipperSnapper_LittleFS::WipperSnapper_LittleFS () {
30
- // Attempt to initialize filesystem
31
- if (!LittleFS.begin ()) {
32
- setStatusLEDColor (RED);
33
- fsHalt (" ERROR: Failure initializing LittleFS!" );
34
- }
35
- }
36
-
37
- /* *************************************************************************/
38
- /* !
39
- @brief Destructor for LittleFS
40
- */
41
- /* *************************************************************************/
42
- WipperSnapper_LittleFS::~WipperSnapper_LittleFS () { LittleFS.end (); }
43
-
44
- /* *************************************************************************/
45
- /* !
46
- @brief Locates, opens and parses the WipperSnapper secrets file
47
- on the LittleFS filesystem.
48
- */
49
- /* *************************************************************************/
50
- void WipperSnapper_LittleFS::parseSecrets () {
51
- // Check if `secrets.json` file exists on FS
52
- if (!LittleFS.exists (" /secrets.json" )) {
53
- fsHalt (" ERROR: No secrets.json found on filesystem - did you upload "
54
- " credentials?" );
55
- }
56
-
57
- // Attempt to open secrets.json file for reading
58
- File secretsFile = LittleFS.open (" /secrets.json" , " r" );
59
- if (!secretsFile) {
60
- fsHalt (" ERROR: Could not open secrets.json file for reading!" );
61
- }
62
-
63
- // Attempt to deserialize the file's JSON document
64
- JsonDocument doc;
65
- DeserializationError error = deserializeJson (doc, secretsFile);
66
- if (error) {
67
- fsHalt (String (" ERROR: deserializeJson() failed with code " ) +
68
- error.c_str ());
69
- }
70
-
71
- if (doc.containsKey (" network_type_wifi" )) {
72
- // set default network config
73
- convertFromJson (doc[" network_type_wifi" ], WS._config .network );
74
-
75
- if (!doc[" network_type_wifi" ].containsKey (" alternate_networks" )) {
76
- WS_DEBUG_PRINTLN (" Found single wifi network in secrets.json" );
77
-
78
- } else if (doc[" network_type_wifi" ][" alternative_networks" ].is <JsonArray>()) {
79
- WS_DEBUG_PRINTLN (" Found multiple wifi networks in secrets.json" );
80
- // Parse network credentials from array in secrets
81
- JsonArray networks = doc[" network_type_wifi" ][" alternative_networks" ];
82
- int8_t networkCount = networks.size ();
83
- WS_DEBUG_PRINT (" Network count: " );
84
- WS_DEBUG_PRINTLN (networkCount);
85
- if (networkCount == 0 ) {
86
- fsHalt (" ERROR: No networks found in network_type_wifi in secrets.json!" );
87
- }
88
- // check if over 5, warn user and take first five
89
- for (int i = 0 ; i < networkCount; i++) {
90
- if (i >= 3 ) {
91
- WS_DEBUG_PRINT (" WARNING: More than 3 networks in secrets.json, "
92
- " only the first 3 will be used. Not using " );
93
- WS_DEBUG_PRINTLN (networks[i][" ssid" ].as <const char *>());
94
- break ;
95
- }
96
- convertFromJson (networks[i], WS._multiNetworks [i]);
97
- WS_DEBUG_PRINT (" Added SSID: " );
98
- WS_DEBUG_PRINTLN (WS._multiNetworks [i].ssid );
99
- WS_DEBUG_PRINT (" PASS: " );
100
- WS_DEBUG_PRINTLN (WS._multiNetworks [i].pass );
101
- }
102
- } else {
103
- fsHalt (" ERROR: Unrecognised value type for network_type_wifi in "
104
- " secrets.json!" );
105
- }
106
- } else {
107
- fsHalt (" ERROR: Could not find network_type_wifi in secrets.json!" );
108
- }
109
-
110
- // Extract a config struct from the JSON document
111
- WS._config = doc.as <secretsConfig>();
112
-
113
- // Validate the config struct is not filled with default values
114
- if (strcmp (WS._config .aio_user , " YOUR_IO_USERNAME_HERE" ) == 0 ||
115
- strcmp (WS._config .aio_key , " YOUR_IO_KEY_HERE" ) == 0 ) {
116
- fsHalt (
117
- " ERROR: Invalid IO credentials in secrets.json! TO FIX: Please change "
118
- " io_username and io_key to match your Adafruit IO credentials!\n " );
119
- }
120
-
121
- if (strcmp (WS._config .network .ssid , " YOUR_WIFI_SSID_HERE" ) == 0 ||
122
- strcmp (WS._config .network .pass , " YOUR_WIFI_PASS_HERE" ) == 0 ) {
123
- fsHalt (" ERROR: Invalid network credentials in secrets.json! TO FIX: Please "
124
- " change network_ssid and network_password to match your Adafruit IO "
125
- " credentials!\n " );
126
- }
127
-
128
- // Close the file
129
- secretsFile.close ();
130
-
131
- // Stop LittleFS, we no longer need it
132
- LittleFS.end ();
133
- }
134
-
135
- /* *************************************************************************/
136
- /* !
137
- @brief Halts execution and blinks the status LEDs yellow.
138
- @param msg
139
- Error message to print to serial console.
140
- */
141
- /* *************************************************************************/
142
- void WipperSnapper_LittleFS::fsHalt (String msg) {
143
- statusLEDSolid (WS_LED_STATUS_FS_WRITE);
144
- while (1 ) {
145
- WS_DEBUG_PRINTLN (" Fatal Error: Halted execution!" );
146
- WS_DEBUG_PRINTLN (msg.c_str ());
147
- delay (1000 );
148
- yield ();
149
- }
150
- }
151
-
1
+ /* !
2
+ * @file Wippersnapper_LittleFS.cpp
3
+ *
4
+ * Interfaces with LittleFS filesystem for ESP32, ESP8266 platforms.
5
+ *
6
+ * Adafruit invests time and resources providing this open source code,
7
+ * please support Adafruit and open-source hardware by purchasing
8
+ * products from Adafruit!
9
+ *
10
+ * Copyright (c) Brent Rubell 2021-2022 for Adafruit Industries.
11
+ *
12
+ * BSD license, all text here must be included in any redistribution.
13
+ *
14
+ */
15
+ #if defined(ARDUINO_FEATHER_ESP32) || \
16
+ defined (ARDUINO_ESP8266_ADAFRUIT_HUZZAH) || \
17
+ defined(ARDUINO_ADAFRUIT_ITSYBITSY_ESP32) || \
18
+ defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2) || \
19
+ defined(ARDUINO_ADAFRUIT_QTPY_ESP32_PICO) || \
20
+ defined(ARDUINO_ADAFRUIT_QTPY_ESP32C3)
21
+ #include " WipperSnapper_LittleFS.h"
22
+
23
+ /* *************************************************************************/
24
+ /* !
25
+ @brief Attempts to set up and initialize a pre-existing LittleFS
26
+ filesystem.
27
+ */
28
+ /* *************************************************************************/
29
+ WipperSnapper_LittleFS::WipperSnapper_LittleFS () {
30
+ // Attempt to initialize filesystem
31
+ if (!LittleFS.begin ()) {
32
+ setStatusLEDColor (RED);
33
+ fsHalt (" ERROR: Failure initializing LittleFS!" );
34
+ }
35
+ }
36
+
37
+ /* *************************************************************************/
38
+ /* !
39
+ @brief Destructor for LittleFS
40
+ */
41
+ /* *************************************************************************/
42
+ WipperSnapper_LittleFS::~WipperSnapper_LittleFS () { LittleFS.end (); }
43
+
44
+ /* *************************************************************************/
45
+ /* !
46
+ @brief Locates, opens and parses the WipperSnapper secrets file
47
+ on the LittleFS filesystem.
48
+ */
49
+ /* *************************************************************************/
50
+ void WipperSnapper_LittleFS::parseSecrets () {
51
+ // Check if `secrets.json` file exists on FS
52
+ if (!LittleFS.exists (" /secrets.json" )) {
53
+ fsHalt (" ERROR: No secrets.json found on filesystem - did you upload "
54
+ " credentials?" );
55
+ }
56
+
57
+ // Attempt to open secrets.json file for reading
58
+ File secretsFile = LittleFS.open (" /secrets.json" , " r" );
59
+ if (!secretsFile) {
60
+ fsHalt (" ERROR: Could not open secrets.json file for reading!" );
61
+ }
62
+
63
+ // Attempt to deserialize the file's JSON document
64
+ JsonDocument doc;
65
+ DeserializationError error = deserializeJson (doc, secretsFile);
66
+ if (error) {
67
+ fsHalt (String (" ERROR: deserializeJson() failed with code " ) +
68
+ error.c_str ());
69
+ }
70
+
71
+ if (doc.containsKey (" network_type_wifi" )) {
72
+ // set default network config
73
+ convertFromJson (doc[" network_type_wifi" ], WS._config .network );
74
+
75
+ if (!doc[" network_type_wifi" ].containsKey (" alternate_networks" )) {
76
+ WS_DEBUG_PRINTLN (" Found single wifi network in secrets.json" );
77
+
78
+ } else if (doc[" network_type_wifi" ][" alternative_networks" ].is <JsonArray>()) {
79
+ WS_DEBUG_PRINTLN (" Found multiple wifi networks in secrets.json" );
80
+ // Parse network credentials from array in secrets
81
+ JsonArray networks = doc[" network_type_wifi" ][" alternative_networks" ];
82
+ int8_t networkCount = networks.size ();
83
+ WS_DEBUG_PRINT (" Network count: " );
84
+ WS_DEBUG_PRINTLN (networkCount);
85
+ if (networkCount == 0 ) {
86
+ fsHalt (" ERROR: No networks found in network_type_wifi in secrets.json!" );
87
+ }
88
+ // check if over 5, warn user and take first five
89
+ for (int i = 0 ; i < networkCount; i++) {
90
+ if (i >= 3 ) {
91
+ WS_DEBUG_PRINT (" WARNING: More than 3 networks in secrets.json, "
92
+ " only the first 3 will be used. Not using " );
93
+ WS_DEBUG_PRINTLN (networks[i][" ssid" ].as <const char *>());
94
+ break ;
95
+ }
96
+ convertFromJson (networks[i], WS._multiNetworks [i]);
97
+ WS_DEBUG_PRINT (" Added SSID: " );
98
+ WS_DEBUG_PRINTLN (WS._multiNetworks [i].ssid );
99
+ WS_DEBUG_PRINT (" PASS: " );
100
+ WS_DEBUG_PRINTLN (WS._multiNetworks [i].pass );
101
+ }
102
+ } else {
103
+ fsHalt (" ERROR: Unrecognised value type for network_type_wifi in "
104
+ " secrets.json!" );
105
+ }
106
+ } else {
107
+ fsHalt (" ERROR: Could not find network_type_wifi in secrets.json!" );
108
+ }
109
+
110
+ // Extract a config struct from the JSON document
111
+ WS._config = doc.as <secretsConfig>();
112
+
113
+ // Validate the config struct is not filled with default values
114
+ if (strcmp (WS._config .aio_user , " YOUR_IO_USERNAME_HERE" ) == 0 ||
115
+ strcmp (WS._config .aio_key , " YOUR_IO_KEY_HERE" ) == 0 ) {
116
+ fsHalt (
117
+ " ERROR: Invalid IO credentials in secrets.json! TO FIX: Please change "
118
+ " io_username and io_key to match your Adafruit IO credentials!\n " );
119
+ }
120
+
121
+ if (strcmp (WS._config .network .ssid , " YOUR_WIFI_SSID_HERE" ) == 0 ||
122
+ strcmp (WS._config .network .pass , " YOUR_WIFI_PASS_HERE" ) == 0 ) {
123
+ fsHalt (" ERROR: Invalid network credentials in secrets.json! TO FIX: Please "
124
+ " change network_ssid and network_password to match your Adafruit IO "
125
+ " credentials!\n " );
126
+ }
127
+
128
+ // Close the file
129
+ secretsFile.close ();
130
+
131
+ // Stop LittleFS, we no longer need it
132
+ LittleFS.end ();
133
+ }
134
+
135
+ /* *************************************************************************/
136
+ /* !
137
+ @brief Halts execution and blinks the status LEDs yellow.
138
+ @param msg
139
+ Error message to print to serial console.
140
+ */
141
+ /* *************************************************************************/
142
+ void WipperSnapper_LittleFS::fsHalt (String msg) {
143
+ statusLEDSolid (WS_LED_STATUS_FS_WRITE);
144
+ while (1 ) {
145
+ WS_DEBUG_PRINTLN (" Fatal Error: Halted execution!" );
146
+ WS_DEBUG_PRINTLN (msg.c_str ());
147
+ delay (1000 );
148
+ yield ();
149
+ }
150
+ }
151
+
152
152
#endif
0 commit comments