Accessing SPIFFS Partitions from ESP32 Arduino Projects

This tutorial shows how to add an SPIFFS data partition to an ESP32 Arduino project in order to store various resources. We will show how to add 2 sample files to the partition, program it into the FLASH memory and use the SPIFFS API to read the file contents programmatically.

Before you begin, install VisualGDB 5.5 or later.

  1. Start Visual Studio and open VisualGDB Arduino Project Wizard:
  2. Enter the name and location of the project you would like to create:
  3. Although the ESP32 Arduino platform comes with its own SPIFFS sample that can be cloned via the VisualGDB Arduino wizard, we will instead show how to add SPIFFS support from scratch, so select “Create a new ‘Blinking LED’ project“:
  4. On the next page pick your ESP32 board. It is also recommended to select the COM port where the target prints the output:
  5. Finally, select debugging settings that work with your target and press “Finish”:
  6. VisualGDB will create a basic LEDBlink sketch. Erase the contents of setup() and loop() functions and include the <SPIFFS.h> file:
  7. Locate the SPIFFS Partition node in Solution Explorer. Right-click on it and select “Add->New Item“:
  8. Enter “file1.txt” as the file name:
  9. VisualGDB will create a new file placing it in the “<Project Directory>\data” folder. Open it via Solution Explorer and add a few lines to it:
  10. Similarly, create a file2.txt file containing different lines:
  11. Now we will show how to read the SPIFFS contents programmatically. First of all, make sure that the COM port is set correctly on the Arduino Project page of VisualGDB Project Properties:
  12. Then enable the Arduino Serial Terminal as shown below:
  13. Finally, add the following code inside the setup() function:
        Serial.begin(115200);
        if (!SPIFFS.begin(false))
        {
            Serial.println("SPIFFS Mount Failed");
            return;
        }
     
        File rootDir = SPIFFS.open("/");
        if (rootDir)
        {
            for (File file = rootDir.openNextFile(); file; file = rootDir.openNextFile())
            {
                Serial.printf("[%s (%d bytes)]\r\n", file.name(), file.size());
                while (file.available())
                {
                    String str = file.readStringUntil('\n');
                    Serial.printf("\t%s\r\n", str.c_str());
                }
            }
        }

    The code mounts the SPIFFS partition and then iterates over all files, printing their contents line-by-line.

  14. Set a breakpoint on the file.available() call and press F5 to start debugging. The breakpoint will trigger on the first file:
  15. You can step through the sketch, or let it run till the end to observe the complete listing of all files:
  16. Now we will show how to program the SPIFFS partition separately from the program itself. Change some of the lines in file2.txt so that we could observe the differences:
  17. Right-click on the SPIFFS Partition in Solution Explorer and select “Program FLASH Memory“:
  18. VisualGDB will automatically update and program the SPIFFS image, leaving the program itself unchanged:
  19. Open the serial terminal without starting another debug session via a context menu command on the project node:
  20. Although we have not rebuilt or reprogrammed the program itself, the output will now show the second version of the file that was successfully programmed into the FLASH memory: