Try to read and write files from a sketch using SPIFFS on ESP32
Contents
SPIFFS
There was a time when I wanted to save a configuration file on ESP32-WROOM-32 and load it from a sketch program.
This article describes how I tried to use SPIFFS (SPI Flash File System).
The saved file is stored in the user area of the ESP32 flash memory.
Environment
The following environment was tested.
OS |
Windows 10 Home(64 Bit) |
Arduino IDE |
1.8.13 |
Module |
ESP32-WROOM-32 ESP32 with Wi-Fi and Bluetooth in one module |
Install the uploader
Install the uploader in the Arduino IDE.
Download
Go to this site, scroll down, and click “releases page” under Download the tool archive from releases page.
Download ESP32FS-1.0.zip.
Unzip the file
When you unzip the downloaded file, you will see the following folder structure.
├─ESP32FS-1.0
│ │
│ ├─ESP32FS
│ │ │
│ │ ├──tool
│ │ │ esp32fs.jar
│ │ │
Check the Arduino IDE environment
Start the Arduino IDE and check the sketchbook location.
Select File > Preferences.
Check the sketchbook location.
The default location is C:¥Users¥Username¥Documents¥Arduino.
tools
Create a “tools” folder under the above folder, and copy the folder and files under ESP32FS that you unzipped.
Check
After exiting the Arduino IDE and starting it up, I found that “ESP32 Sketch Data Upload” was added to the Tools menu.
Use this menu to write the file to the flash memory of ESP32.
Partition Scheme
Select a Partition Scheme that has SPIFFS.
When I selected a Partition Scheme without SPIFFS (e.g. Default 4MB with ffat (1.2MB APP/1.5MB FATTS)) and tried to Upload, I got “SPIFFS Error: partition size could not be found! SPIFFS Error: partition size could not be found!
Uploading
Upload the file manually and try to read it from the sketch.
Prepare the files
I created a “data” folder under the folder where the sketch is saved and put the file I want to copy (config.ini).
The name of the “data” folder seems to be “predetermined”.
config.ini
client_id=SKSPI0001
flg1=1
flg2=0
flg3=0
upload
When I selected Tools > ESP32 Sketch Data Upload, the message “SPIFFS image Uploaded” appeared and the config.ini file under the data folder was written to ESP32.
The message is as follows.
It turns out that config.sys, desktop.ini (a file created automatically by Windows to store desktop display information), and spiffs.spiffs.bin files under the data folder have been written.
[SPIFFS] data : D:\GoogleDriveD\M2B\Program\ArduinoProgram\spiffsen\data
[SPIFFS] start : 2686976
[SPIFFS] size : 1472
[SPIFFS] page : 256
[SPIFFS] block : 4096
/config.ini
/desktop.ini
[SPIFFS] upload : C:\Users\UserName\AppData\Local\Temp\arduino_build_197820/spiffsen.spiffs.bin
[SPIFFS] address: 2686976
[SPIFFS] port : COM7
[SPIFFS] speed : 115200
[SPIFFS] mode : dio
[SPIFFS] freq : 80m
esptool.py v3.0-dev
Serial port COM7
Connecting....
Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: f0:08:d1:d2:7b:20
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 1507328 bytes to 3025...
Writing at 0x00290000... (100 %)
Wrote 1507328 bytes (3025 compressed) at 0x00290000 in 0.3 seconds (effective 42913.3 kbit/s)...
Hash of data verified.
Leaving...
Hard resetting via RTS pin...
Skecth
Loading
A sketch that reads and displays the config.ini file under the data folder.
spiffs.ino
/*
* SPIFFS Test Scketch
*
*/
#include "SPIFFS.h"
void setup() {
Serial.begin(115200);
SPIFFS.begin();
File fr = SPIFFS.open("/config.ini", "r");
while(fr.available()){
String readStr = fr.readStringUntil('\n'); // Read one line
Serial.println(readStr);
}
fr.close();
}
void loop() {
}
Execution Results
The execution result of the serial monitor is as follows.
Delete
A sketch to remove unnecessary desktop.ini that was uploaded along with the manual upload.
/*
* SPIFFS Test Scketch
*
*/
#include "FS.h"
#include "SPIFFS.h"
void setup() {
Serial.begin(115200);
SPIFFS.begin();
fs::FS fs = SPIFFS;
fs.remove("/desktop.ini");
}
void loop() {
}
Write
A sketch that writes and rereads a file.
/*
* SPIFFS Test Scketch
*
*/
#include "SPIFFS.h"
void setup() {
Serial.begin(115200);
SPIFFS.begin();
File fr = SPIFFS.open("/config2.ini", "w"); // Write Mode
fr.println("confing2 test line1");
fr.println("confing2 test line2");
fr.close();
File frr = SPIFFS.open("/config2.ini", "r"); // Read Mode
while(frr.available()){
String readStr = frr.readStringUntil('\n');
Serial.println(readStr);
}
frr.close();
}
void loop() {
}
Execution Result
The two lines that have been written are displayed.
This concludes this article.
I hope this article will be useful to someone somewhere.
Recent Comments