Problem with SPIFFS
Posted: Wed Mar 21, 2018 3:01 am
Hi
I'm testing with the ESP-01 module.
I'm following the instruction find at: https://tttapa.github.io/ (A-Beginner's-Guide-to-the-ESP8266)
All ok until "Uploading files to SPIFFS"
The problem is that I can't read the file uploaded with a browser.
Trying to isolate the problem I write this code where I do:
- Format the SPIFFS
- Show info about SPIFFS
- Open in write mode the file test.html
- Write 4 bytes
- Close
- Show again info about SPIFFS
Until here all seems to be ok, but when I try to open the file I receive the error -10002 (SPIFFS_ERR_NOT_FOUND)
In a more complex sketch I receive also the error -10012 (SPIFFS_ERR_IS_FREE) and when I try to write a second time the
file test.html it disappears from the directory.
Someone can help me?
Thank you
Livio
The output on the monitor:
I'm testing with the ESP-01 module.
I'm following the instruction find at: https://tttapa.github.io/ (A-Beginner's-Guide-to-the-ESP8266)
All ok until "Uploading files to SPIFFS"
The problem is that I can't read the file uploaded with a browser.
Trying to isolate the problem I write this code where I do:
- Format the SPIFFS
- Show info about SPIFFS
- Open in write mode the file test.html
- Write 4 bytes
- Close
- Show again info about SPIFFS
Until here all seems to be ok, but when I try to open the file I receive the error -10002 (SPIFFS_ERR_NOT_FOUND)
In a more complex sketch I receive also the error -10012 (SPIFFS_ERR_IS_FREE) and when I try to write a second time the
file test.html it disappears from the directory.
Someone can help me?
Thank you
Livio
Code: Select all
//-----------------------------------------------------------------------
// ESP8266 test FS
//-----------------------------------------------------------------------
#include <FS.h>
#define Monitor Serial1
FSInfo fs_info;
char bufferTemp[300];
String filename;
File fsFile;
void ShowDIR(void);
void ShowInfo(void);
void ReadFile();
//-----------------------------------------------------------------------
void setup() {
Monitor.begin(115200); // Debug console (only Tx on ESP-01)
SPIFFS.begin(); // Start the SPI Flash Files System
Monitor.println("init SPIFFS");
if(SPIFFS.format())
Monitor.println("FORMAT OK");
ShowInfo();
filename = "test.html";
Monitor.print("OPEN FILE in Write: "); Monitor.println(filename);
fsFile = SPIFFS.open(filename, "w"); // Open the file for writing in SPIFFS (create if it doesn't exist)
if(fsFile == false)
Monitor.println("ERROR opening file");
else{
Monitor.println("FILE WRITE");
fsFile.write('A');
fsFile.write('B');
fsFile.write('C');
fsFile.write('D');
fsFile.close(); // Close the file again
Monitor.println("FILE CLOSE\n");
}
delay(1000);
ShowInfo();
ShowDIR();
ReadFile();
}
void loop() {
delay(1000);
}
void ShowInfo(void){
SPIFFS.info(fs_info);
sprintf(bufferTemp, "Total Bytes: %d", fs_info.totalBytes);
Monitor.println(bufferTemp);
sprintf(bufferTemp, "Used Bytes: %d", fs_info.usedBytes);
Monitor.println(bufferTemp);
sprintf(bufferTemp, "Block Size: %d", fs_info.blockSize);
Monitor.println(bufferTemp);
sprintf(bufferTemp, "Page Size: %d", fs_info.pageSize);
Monitor.println(bufferTemp);
sprintf(bufferTemp, "Max open files: %d", fs_info.maxOpenFiles);
Monitor.println(bufferTemp);
sprintf(bufferTemp, "Max path lenght: %d", fs_info.maxPathLength);
Monitor.println(bufferTemp);
Monitor.println("");
}
void ShowDIR(void){
Monitor.println("\n--------------------------------");
Monitor.println("Directory:");
Dir dir = SPIFFS.openDir("");
while (dir.next()) {
Monitor.print(dir.fileName());
File f = dir.openFile("r");
Monitor.print("\t\t");
Monitor.println(f.size());
}
Monitor.println("--------------------------------");
Monitor.println("");
}
void ReadFile(void){
fsFile = SPIFFS.open(filename, "r");
if(fsFile == false){
Monitor.print("ERROR opening file: "); Monitor.println(filename);
}
else{
Monitor.print("\n\nRead file: "); Monitor.println(filename);
int lunghezza = fsFile.readBytes(bufferTemp, 300);
Monitor.print("Lenght: "); Monitor.println(lunghezza);
if(lunghezza > 0){
Monitor.println("-----------------------------------------------");
Monitor.println(bufferTemp);
Monitor.println("-----------------------------------------------");
}
}
fsFile.close();
}
The output on the monitor:
Code: Select all
SDK:2.2.1(cfd48f3)/Core:2.4.1/lwIP:2.0.3(STABLE-2_0_3_RELEASE/glue:arduino-2.4.1)
SPIFFSImpl: allocating 512+240+1400=2152 bytes
SPIFFSImpl: mounting fs @7b000, size=80000, block=2000, page=100
SPIFFSImpl: mount rc=0
init SPIFFS
SPIFFSImpl: mounting fs @7b000, size=80000, block=2000, page=100
SPIFFSImpl: mount rc=0
SPIFFSImpl: mounting fs @7b000, size=80000, block=2000, page=100
SPIFFSImpl: mount rc=0
FORMAT OK
Total Bytes: 482673
Used Bytes: 0
Block Size: 8192
Page Size: 256
Max open files: 5
Max path lenght: 32
OPEN FILE in Write: test.html
FILE WRITE
SPIFFS_close: fd=1
SPIFFS_close: fd=1
FILE CLOSE
Total Bytes: 482673
Used Bytes: 502
Block Size: 8192
Page Size: 256
Max open files: 5
Max path lenght: 32
--------------------------------
Directory:
test.html 4
SPIFFS_close: fd=2
--------------------------------
SPIFFSImpl::open: fd=-10002 path=`test.html` openMode=0 accessMode=1 err=-10002
ERROR opening file: test.html