You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/hardware/03.nano/boards/nano-esp32/tutorials/spiff/spiff.md
+259-5Lines changed: 259 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,18 +11,20 @@ The SPIFFS (Serial Peripheral Interface Flash File System) is a file system desi
11
11
12
12
***Before proceeding with the SPIFFS file system setup on Nano ESP, please be aware that the mounting instructions mentioned in a comment on the code from the packaged built-in example are tailored for specific older IDE versions such as IDE 1.8. These instructions might not be relevant to IDE 2.0, as it does not support the utilization of custom tools, which are unnecessary in this case.***
-[Arduino ESP32 Core](https://github.com/arduino/arduino-esp32) installed (done in the Arduino IDE).
19
19
20
20
## Set Up SPIFFS File System
21
21
22
-
Start by opening the **SPIFFS_Test example** by going to:
22
+
Start by opening the **SPIFFS_Test example** by navigating to:
23
23
24
24
- File > Examples > SPIFFS > SPIFFS_Test
25
25
26
+
***The example is also included at the bottom (see [Example](#example)), together with an explanation of how the code work.***
27
+
26
28

27
29
28
30
Select the correct **Partition Scheme** by clicking:
@@ -33,7 +35,7 @@ Select the correct **Partition Scheme** by clicking:
33
35
34
36
Prepare your following these steps:
35
37
36
-
- Take your **unplugged** board connect a jumper cable between the **GND** and **B1** pins.
38
+
- Take your **unplugged** board and connect a jumper cable between the **GND** and **B1** pins.
37
39
38
40
-**Plug it in** and the RGB LED will turn on with a green or blue color.
39
41
@@ -77,4 +79,256 @@ Open the serial monitor and you should see the following output:
77
79
78
80

79
81
80
-
***Note: It might take a short while for the messages to be printed. If you don't see any messages after one minute repeat the process.***
82
+
***Note: It might take a short while for the messages to be printed. If you don't see any messages after one minute repeat the process.***
83
+
84
+
## Example
85
+
86
+
As mentioned above the code is taken from **File > Examples > SPIFFS > SPIFFS_Test** and can also be found inside the IDE's example section. It showcases different features available when using the SPIFSF file system.
87
+
88
+
```arduino
89
+
#include "FS.h"
90
+
#include "SPIFFS.h"
91
+
92
+
/* You only need to format SPIFFS the first time you run a
93
+
test or else use the SPIFFS plugin to create a partition
Serial.printf("Renaming file %s to %s\r\n", path1, path2);
178
+
if (fs.rename(path1, path2)) {
179
+
Serial.println("- file renamed");
180
+
} else {
181
+
Serial.println("- rename failed");
182
+
}
183
+
}
184
+
185
+
void deleteFile(fs::FS &fs, const char * path){
186
+
Serial.printf("Deleting file: %s\r\n", path);
187
+
if(fs.remove(path)){
188
+
Serial.println("- file deleted");
189
+
} else {
190
+
Serial.println("- delete failed");
191
+
}
192
+
}
193
+
194
+
void testFileIO(fs::FS &fs, const char * path){
195
+
Serial.printf("Testing file I/O with %s\r\n", path);
196
+
197
+
static uint8_t buf[512];
198
+
size_t len = 0;
199
+
File file = fs.open(path, FILE_WRITE);
200
+
if(!file){
201
+
Serial.println("- failed to open file for writing");
202
+
return;
203
+
}
204
+
205
+
size_t i;
206
+
Serial.print("- writing" );
207
+
uint32_t start = millis();
208
+
for(i=0; i<2048; i++){
209
+
if ((i & 0x001F) == 0x001F){
210
+
Serial.print(".");
211
+
}
212
+
file.write(buf, 512);
213
+
}
214
+
Serial.println("");
215
+
uint32_t end = millis() - start;
216
+
Serial.printf(" - %u bytes written in %u ms\r\n", 2048 * 512, end);
217
+
file.close();
218
+
219
+
file = fs.open(path);
220
+
start = millis();
221
+
end = start;
222
+
i = 0;
223
+
if(file && !file.isDirectory()){
224
+
len = file.size();
225
+
size_t flen = len;
226
+
start = millis();
227
+
Serial.print("- reading" );
228
+
while(len){
229
+
size_t toRead = len;
230
+
if(toRead > 512){
231
+
toRead = 512;
232
+
}
233
+
file.read(buf, toRead);
234
+
if ((i++ & 0x001F) == 0x001F){
235
+
Serial.print(".");
236
+
}
237
+
len -= toRead;
238
+
}
239
+
Serial.println("");
240
+
end = millis() - start;
241
+
Serial.printf("- %u bytes read in %u ms\r\n", flen, end);
242
+
file.close();
243
+
} else {
244
+
Serial.println("- failed to open file for reading");
245
+
}
246
+
}
247
+
248
+
void setup(){
249
+
Serial.begin(115200);
250
+
if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
251
+
Serial.println("SPIFFS Mount Failed");
252
+
return;
253
+
}
254
+
255
+
listDir(SPIFFS, "/", 0);
256
+
writeFile(SPIFFS, "/hello.txt", "Hello ");
257
+
appendFile(SPIFFS, "/hello.txt", "World!\r\n");
258
+
readFile(SPIFFS, "/hello.txt");
259
+
renameFile(SPIFFS, "/hello.txt", "/foo.txt");
260
+
readFile(SPIFFS, "/foo.txt");
261
+
deleteFile(SPIFFS, "/foo.txt");
262
+
testFileIO(SPIFFS, "/test.txt");
263
+
deleteFile(SPIFFS, "/test.txt");
264
+
Serial.println( "Test complete" );
265
+
}
266
+
267
+
void loop(){
268
+
269
+
}
270
+
```
271
+
272
+
### How it works?
273
+
274
+
- First, the **necessary libraries** are included:
275
+
276
+
```arduino
277
+
#include "FS.h"
278
+
#include "SPIFFS.h"
279
+
```
280
+
281
+
- The constant `FORMAT_SPIFFS_IF_FAILED` is used to specify whether the SPIFFS file system should be formatted if it fails to mount. If it's `true`, the file system will be formatted:
282
+
283
+
```arduino
284
+
#define FORMAT_SPIFFS_IF_FAILED true
285
+
```
286
+
287
+
- The code consists of several **functions** defined in the code showcasing **different features** such as:
288
+
-`void listDir()` = List Files
289
+
-`void readFile()` = Read Files
290
+
-`void writeFile()` = Write Files
291
+
-`void appendFile()` = Append a message to an existing file
292
+
-`void renameFile()` = Rename Files
293
+
-`void deleteFile()` = Delete Files
294
+
-`void testFileIO()` = Testing a file's input and output by writing a large amount of data and then reading it.
295
+
296
+
- Before calling all the functions mentioned above **two things** are done inside `setup()`:
297
+
- The serial communication is initialized with a baud rate of 115200.
298
+
- The SPIFFS file system is mounted and if `FORMAT_SPIFFS_IF_FAILED` is `true` it's formatted.
299
+
300
+
```arduino
301
+
void setup(){
302
+
Serial.begin(115200);
303
+
if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
304
+
Serial.println("SPIFFS Mount Failed");
305
+
return;
306
+
}
307
+
...
308
+
}
309
+
```
310
+
311
+
Lastly, all functions are called with parameters:
312
+
313
+
```arduino
314
+
void setup(){
315
+
Serial.begin(115200);
316
+
if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
317
+
Serial.println("SPIFFS Mount Failed");
318
+
return;
319
+
}
320
+
321
+
listDir(SPIFFS, "/", 0);
322
+
writeFile(SPIFFS, "/hello.txt", "Hello ");
323
+
appendFile(SPIFFS, "/hello.txt", "World!\r\n");
324
+
readFile(SPIFFS, "/hello.txt");
325
+
renameFile(SPIFFS, "/hello.txt", "/foo.txt");
326
+
readFile(SPIFFS, "/foo.txt");
327
+
deleteFile(SPIFFS, "/foo.txt");
328
+
testFileIO(SPIFFS, "/test.txt");
329
+
deleteFile(SPIFFS, "/test.txt");
330
+
Serial.println( "Test complete" );
331
+
}
332
+
```
333
+
334
+
- All messages printed on the serial monitor are defined in the respective function, except "Test Complete" which printed by `setup()` once everything is done.
0 commit comments