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
- // Extract a config struct from the JSON document
72
- WS._config = doc.as <secretsConfig>();
73
-
74
- // Validate the config struct is not filled with default values
75
- if (strcmp (WS._config .aio_user , " YOUR_IO_USERNAME_HERE" ) == 0 ||
76
- strcmp (WS._config .aio_key , " YOUR_IO_KEY_HERE" ) == 0 ) {
77
- fsHalt (
78
- " ERROR: Invalid IO credentials in secrets.json! TO FIX: Please change "
79
- " io_username and io_key to match your Adafruit IO credentials!\n " );
80
- }
81
-
82
- if (strcmp (WS._config .network .ssid , " YOUR_WIFI_SSID_HERE" ) == 0 ||
83
- strcmp (WS._config .network .pass , " YOUR_WIFI_PASS_HERE" ) == 0 ) {
84
- fsHalt (" ERROR: Invalid network credentials in secrets.json! TO FIX: Please "
85
- " change network_ssid and network_password to match your Adafruit IO "
86
- " credentials!\n " );
87
- }
88
-
89
- // Close the file
90
- secretsFile.close ();
91
-
92
- // Stop LittleFS, we no longer need it
93
- LittleFS.end ();
94
- }
95
-
96
- /* *************************************************************************/
97
- /* !
98
- @brief Halts execution and blinks the status LEDs yellow.
99
- @param msg
100
- Error message to print to serial console.
101
- */
102
- /* *************************************************************************/
103
- void WipperSnapper_LittleFS::fsHalt (String msg) {
104
- statusLEDSolid (WS_LED_STATUS_FS_WRITE);
105
- while (1 ) {
106
- WS_DEBUG_PRINTLN (" Fatal Error: Halted execution!" );
107
- WS_DEBUG_PRINTLN (msg.c_str ());
108
- delay (1000 );
109
- yield ();
110
- }
111
- }
112
-
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
+ if (doc[" network_type_wifi" ].is <JsonObjectConst>()) {
73
+ WS_DEBUG_PRINTLN (" Found single wifi network in secrets.json" );
74
+ // Parse network credentials from secrets
75
+ convertFromJson (doc[" network_type_wifi" ], WS._config .network );
76
+ } else if (doc[" network_type_wifi" ].is <JsonArrayConst>()) {
77
+ WS_DEBUG_PRINTLN (" Found multiple wifi networks in secrets.json" );
78
+ // Parse network credentials from secrets
79
+ int8_t networkCount = doc[" network_type_wifi" ].size ();
80
+ convertFromJson (doc[" network_type_wifi" ][networkCount - 1 ],
81
+ WS._config .network );
82
+ } else {
83
+ fsHalt (" ERROR: Unrecognised value type for network_type_wifi in "
84
+ " secrets.json!" );
85
+ }
86
+ } else {
87
+ fsHalt (" ERROR: Could not find network_type_wifi in secrets.json!" );
88
+ }
89
+ // Extract a config struct from the JSON document
90
+ WS._config = doc.as <secretsConfig>();
91
+
92
+ // Validate the config struct is not filled with default values
93
+ if (strcmp (WS._config .aio_user , " YOUR_IO_USERNAME_HERE" ) == 0 ||
94
+ strcmp (WS._config .aio_key , " YOUR_IO_KEY_HERE" ) == 0 ) {
95
+ fsHalt (
96
+ " ERROR: Invalid IO credentials in secrets.json! TO FIX: Please change "
97
+ " io_username and io_key to match your Adafruit IO credentials!\n " );
98
+ }
99
+
100
+ if (strcmp (WS._config .network .ssid , " YOUR_WIFI_SSID_HERE" ) == 0 ||
101
+ strcmp (WS._config .network .pass , " YOUR_WIFI_PASS_HERE" ) == 0 ) {
102
+ fsHalt (" ERROR: Invalid network credentials in secrets.json! TO FIX: Please "
103
+ " change network_ssid and network_password to match your Adafruit IO "
104
+ " credentials!\n " );
105
+ }
106
+
107
+ // Close the file
108
+ secretsFile.close ();
109
+
110
+ // Stop LittleFS, we no longer need it
111
+ LittleFS.end ();
112
+ }
113
+
114
+ /* *************************************************************************/
115
+ /* !
116
+ @brief Halts execution and blinks the status LEDs yellow.
117
+ @param msg
118
+ Error message to print to serial console.
119
+ */
120
+ /* *************************************************************************/
121
+ void WipperSnapper_LittleFS::fsHalt (String msg) {
122
+ statusLEDSolid (WS_LED_STATUS_FS_WRITE);
123
+ while (1 ) {
124
+ WS_DEBUG_PRINTLN (" Fatal Error: Halted execution!" );
125
+ WS_DEBUG_PRINTLN (msg.c_str ());
126
+ delay (1000 );
127
+ yield ();
128
+ }
129
+ }
130
+
113
131
#endif
0 commit comments