I have a simple HTML file that I have uploaded as sketch data using the ESP8266 sketch data upload tool in arduino IDE. The file is 7k in size.
In the set up, I refer to the following function to read the file:
String readTemplatefile(){
String retval ="";
File fr = SPIFFS.open("/templatecfg.htm", "r");
if(!SPIFFS.exists("/templatecfg.htm")){
Serial.println("Template file not available ");
}
while (fr.available()){
retval += char(fr.read());
}
fr.close();
Serial.println("Template file = ");
Serial.println(retval);
return retval;
}
I always get 'Template file not found'. I have
SPIFFS.begin();
at the beginning of the setup function
My board is an ESP8266EX. I have wondered if it the flash size settings in the IDE that could cause this not to be found. Can anyone offer any advice on this issue. I have tried lots of different combinations of Flash size and SPIFF sizes without any luck.
Thanks in advance to any help.
2 Answers 2
SPIFFS will always claim to successfully open a file that begins with a slash even if that file does not exist. Remove the leading slash.
@dandavis Yes, I had SPIFFS.begin() in my setup routine.
I ran a sample sketch and was able to see the a file on the device, so I routed round the code some more and found than an included library was ending SPIFFS after it had performed a file read. This was the issue. Thanks all for your consideration, but the code I had was fine, just the included library was mis-behaving.
-
what library? so we learn2018年04月13日 14:11:03 +00:00Commented Apr 13, 2018 at 14:11
SPIFFS.begin()
? Have you tried opening the "/" directory and listing the files, so you can confirm what your sketch sees as the available files?ESP.getFlashChipSize()
) and the physical sizes (ESP.getFlashChipRealSize()
) match up. google the esp lib functions for docs