Need some guidance on using SPIFFS

I've been trying to setup a SPIFFS file system on my ESP32-WROOM to serve some files for a web server. My process was to create a SPIFFS bin image on my PC, then flash it to the ESP32 and list the files but it's not working as it can't find the filesystem (SPIFFS Mount Failed). I'm using a really simple sketch just to make sure I can list the files that I'm flashing. I've set the Partition Scheme to the Default 4MB with spiffs partition.

#include "FS.h"
#include "SPIFFS.h"

void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
    Serial.printf("Listing directory: %s\r\n", dirname);

    File root = fs.open(dirname);
    if(!root){
        Serial.println("- failed to open directory");
        return;
    }
    if(!root.isDirectory()){
        Serial.println(" - not a directory");
        return;
    }

    File file = root.openNextFile();
    while(file){
        if(file.isDirectory()){
            Serial.print("  DIR : ");
            Serial.println(file.name());
            if(levels){
                listDir(fs, file.path(), levels -1);
            }
        } else {
            Serial.print("  FILE: ");
            Serial.print(file.name());
            Serial.print("\tSIZE: ");
            Serial.println(file.size());
        }
        file = root.openNextFile();
    }
}

void setup() {
    Serial.begin(115200);
    if(!SPIFFS.begin(false)){
        Serial.println("SPIFFS Mount Failed");
        return;
    }
  
    Serial.println("SPIFFS Info:");
    Serial.printf("Total Bytes: %u\n", SPIFFS.totalBytes());
    Serial.printf("Used Bytes: %u\n", SPIFFS.usedBytes());
    Serial.printf("Free Bytes: %u\n",SPIFFS.totalBytes() - SPIFFS.usedBytes());

    listDir(SPIFFS, "/", 0);
}

loop() {
}

First I'm creating the 1MB binary image with the contents from the Data folder:

mkspiffs.exe -c .\Data -b 4096 -p 256 -s 0x100000 spiffs.bin

output:

/assets/icons-Dbz5-pCR.svg
/assets/index-CXsarTAc.js
/assets/index-mliwPROd.css
/favicon.ico
/index.html

Then I'm uploading it to the ESP32:

esptool.exe --chip esp32 --port COM10 --baud 921600 write_flash -z 0x120000 spiffs.bin

output:

esptool.py v4.7.0
Serial port COM10
Connecting.......
Chip is ESP32-D0WD-V3 (revision v3.1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: fc:b4:67:f3:47:b8
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 921600
Changed.
Configuring flash size...
Flash will be erased from 0x00120000 to 0x0026ffff...
Compressed 1376256 bytes to 269653...
Wrote 1376256 bytes (269653 compressed) at 0x00120000 in 7.6 seconds (effective 1456.5 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

That seems to run without issues. But if I reset the ESP32 (or upload my sketch in the IDE) I get an error as if it's not aware of the SPIFFS file system image I uploaded. I have "Erase all flash before sketch upload" = Disabled.

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1344
load:0x40078000,len:13964
load:0x40080400,len:3600
entry 0x400805f0
E (9) SPIFFS: mount failed, -10025
SPIFFS Mount Failed

I'm assuming I'm missing something, such as maybe needing to upload my sketch via the command line instead of the IDE. Is my sketch overwriting part of the file system image or the partition isn't set correctly?

Have you tried this example?

you may need to use the earlier (1.8) IDE

I tried a similar one which covered the command line tools, but I'm going to look over that one now and see if I can get it working. I also used the example sketch which is basically that exact code in the article. Looks like I should switch to LittleFS instead and see if I have any luck there.

You can use Arduino IDE 2.x, the procedure is a bit different than described in the tutorial you linked, but fortunately they have also produced a tutorial for Arduino IDE 2.x (which is linked to from the instructions in the tutorial you shared):

3 Likes

I was able to get it to work using LittleFS in the 1.8.* IDE using the plugin described at https://randomnerdtutorials.com/esp32-littlefs-arduino-ide/

I'm going to try the one you just recommended and hopefully get it working in the 2.3.* IDE. Not sure why the command line tools weren't working, I must have had some parameter wrong.

After a fair bit of fiddling around and thanks to the link you provided me I now have it working in Arduino IDE 2.3.*.

I placed all my files in the data\ folder of my sketch, then CTRL-SHIFT-P to open the plugin tool (since 2.3.* doesn't add plugins to the UI) and executed Upload LittleFS to Pico/ESP32....

I can't upload the data files in the IDE unfortunately, as esptool is being passed a board/chip identifier esp32da that esptool doesn't understand. Fortunately however it does output the command line that it runs, and I was able to override the chip setting to esp32 instead of esp32da.

Executed by the IDE:

C:\Users\mikeb\AppData\Local\Arduino15\packages\esp32\tools\esptool_py\4.5.1/esptool.exe --chip esp32da --port COM10 --baud 921600 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 80m --flash_size detect 2686976 C:\Users\mikeb\AppData\Local\Temp\tmp-15912-DR3empe702jP-.littlefs.bin

modified the --chip parameter as follows:

C:\Users\mikeb\AppData\Local\Arduino15\packages\esp32\tools\esptool_py\4.5.1/esptool.exe --chip esp32 --port COM10 --baud 921600 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 80m --flash_size detect 2686976 C:\Users\mikeb\AppData\Local\Temp\tmp-15912-DR3empe702jP-.littlefs.bin

and all working. Thanks for your help!

1 Like

actually can mostly scrub my previous comment. Plugin version 1.1.5 VSIX works just fine, I had downgraded to 1.1.3 when trying to debug why it didn't show up (have to use command palette to run it) and forgot to put the latest version back in place. noice!

1 Like

Nice work! I was just about to reply that I thought this bug had been fixed already:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.