1# SPIFFS example 2 3(See the README.md file in the upper level 'examples' directory for more information about examples.) 4 5This example demonstrates how to use SPIFFS with ESP32. Example does the following steps: 6 71. Use an "all-in-one" `esp_vfs_spiffs_register` function to: 8 - initialize SPIFFS, 9 - mount SPIFFS filesystem using SPIFFS library (and format, if the filesystem can not be mounted), 10 - register SPIFFS filesystem in VFS, enabling C standard library and POSIX functions to be used. 112. Create a file using `fopen` and write to it using `fprintf`. 123. Rename the file. Before renaming, check if destination file already exists using `stat` function, and remove it using `unlink` function. 134. Open renamed file for reading, read back the line, and print it to the terminal. 14 15SPIFFS partition size is set in partitions_example.csv file. See [Partition Tables](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/partition-tables.html) documentation for more information. 16 17## How to use example 18 19### Hardware required 20 21This example does not require any special hardware, and can be run on any common development board. 22 23### Build and flash 24 25Replace PORT with serial port name: 26 27``` 28idf.py -p PORT flash monitor 29``` 30 31(To exit the serial monitor, type ``Ctrl-]``.) 32 33See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. 34 35## Example output 36 37Here is an example console output. In this case `format_if_mount_failed` parameter was set to `true` in the source code. SPIFFS was unformatted, so the initial mount has failed. SPIFFS was then formatted, and mounted again. 38 39``` 40I (324) example: Initializing SPIFFS 41W (324) SPIFFS: mount failed, -10025. formatting... 42I (19414) example: Partition size: total: 896321, used: 0 43I (19414) example: Opening file 44I (19504) example: File written 45I (19544) example: Renaming file 46I (19584) example: Reading file 47I (19584) example: Read from file: 'Hello World!' 48I (19584) example: SPIFFS unmounted 49``` 50 51To erase the contents of SPIFFS partition, run `idf.py erase-flash` command. Then upload the example again as described above. 52