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.
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
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).
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.
- Initial Release: 26 December 2020