ESP32 cannot save JSON file to SD card – Solved

We recently worked on a project that needed ESP32 firmware development using ESP-IDF and we ran into a situation where the ESP32 cannot save JSON file to the SD card. It was strange, given that the ESP32 could save some other files within the same program. On further observatiuon, we figured out that the ESP32 was unable to write files with long file names.

Why ESP32 cannot save JSON files to SD card

We should point out that this issue is not just with JSON files, it would happen any time you tried to save a file that has more than 3 characters in the file extension. JSON simply happens to be one of those common 4-character extension file that you use with the ESP32.

The reason for this is that the menuconfig settings have FAT32 long filename support disabled by default.

When long file name support is disabled, you are only able to save 8.3 type files into the SD card, i.e. up to 8 characters of file name and up to 3 characters of file extension.

So with disabled long file name (LFN), we get something like this when attempting to write a long file name to SD cards and it appears as if there is a problem with the file system or memory card itself. It is rather misleading.

esp32 cannot save json file sd card
ESP32 ESP-IDF program cannot save JSON files to sd card

Solution - enable long filename support in menuconfig

To enable long filename support within menuconfig settings for your project, run menuconfig.

In our Windows environment, within the project directory, we simply use menuconfig

Navigate to
Component config -> FAT filesystem support -> Long filename support

We prefer to set the long filename support on the stack (no point fragmenting memory by allocating it on the heap, it does not take up much space).

esp-idf enable fat32 long filename support menuconfig
Enable long filename support in ESP-IDF using menuconfig

Now that you have enabled long filename support for ESP32 using the menuconfig utility of ESP-IDF, you should be able to work with JSON files or any other long-extention type files within ESP-IDF that use an SD card.

When we recompiled with the modification, we were able to confirm that it works.

esp32 with long filename support
ESP32 ESP-IDF program with long filename support enabled - works!
Change Log
  • Initial Release: 26 December 2020


You may also like

Leave a Comment

3 × four =