Unable to write anything to SPIFFS from code
Posted: Mon Jul 02, 2018 6:05 am
Hi,
I bought a couple of ESP8266-01 powered relays and set about writing code to connection to my wifi and switch on and off using MQTT messages.
It all works apart from 1 thing. I cannot write my app config to SPIFFS. If I transfer it from the Arduino IDE using the Tools->Sketch Data Upload method the file is written to the flash and I can read correctly.
But programatically writing the same config file from my sketch results in garbage in the file which crashes the JSON parser leading to a stack dump.
My understanding is that all ESP8266 modules can use a reserved piece of Flash memory for a pseudo filesystem based on section 4.1 of the Arduino docs - https://media.readthedocs.org/pdf/arduino-esp8266/2.4.0/arduino-esp8266.pdf
In the Arduino IDE, I ran the CheckFlashConfig and found that my modules where 1M (8Mbit), so I selected Generic 8266 - 1M(64K SPIFFS).
ASCII written to a file using println result in nothing readable coming back using
If I use ReadBytes, I get junk
One thing I have noticed is that the filesize in bytes matches what I'm trying to write, but the data is not written.
I've tried formatting, with no effect, and it's weird that only the app is having the problem. Files transferred over from my data folder are okay.
I'm using the latest esp8266 Arduino library 2.4.1 and flashed the latest firmare to the module.
I've also tried 2 modules with the same results.
I've also noticed that while resding or writing to the flash memory, you cannot upload a new sketch, so I've had to put big delays at the beginning of the sketch to give me time to overwrite the sketch with Blink.
The same code worked fine on an ESP8266-12 NodeMCU board I used for development.
Here's my SPIFFS test code.
I bought a couple of ESP8266-01 powered relays and set about writing code to connection to my wifi and switch on and off using MQTT messages.
It all works apart from 1 thing. I cannot write my app config to SPIFFS. If I transfer it from the Arduino IDE using the Tools->Sketch Data Upload method the file is written to the flash and I can read correctly.
But programatically writing the same config file from my sketch results in garbage in the file which crashes the JSON parser leading to a stack dump.
My understanding is that all ESP8266 modules can use a reserved piece of Flash memory for a pseudo filesystem based on section 4.1 of the Arduino docs - https://media.readthedocs.org/pdf/arduino-esp8266/2.4.0/arduino-esp8266.pdf
In the Arduino IDE, I ran the CheckFlashConfig and found that my modules where 1M (8Mbit), so I selected Generic 8266 - 1M(64K SPIFFS).
ASCII written to a file using println result in nothing readable coming back using
Code: Select all
.readStringUntil()
If I use ReadBytes, I get junk
Code: Select all
WriteConfigFile: contents of config.json =þïïþþïïþþïïþþïïþþïïþþïïþþïïþþïïþþïïþþïïþþïïþþïïþþïïþþïïþþïïþþïïþþïïþþïïþþïïþþïïþþïïþþïïþþïïþþïïþþïïþþ
One thing I have noticed is that the filesize in bytes matches what I'm trying to write, but the data is not written.
I've tried formatting, with no effect, and it's weird that only the app is having the problem. Files transferred over from my data folder are okay.
I'm using the latest esp8266 Arduino library 2.4.1 and flashed the latest firmare to the module.
I've also tried 2 modules with the same results.
I've also noticed that while resding or writing to the flash memory, you cannot upload a new sketch, so I've had to put big delays at the beginning of the sketch to give me time to overwrite the sketch with Blink.
The same code worked fine on an ESP8266-12 NodeMCU board I used for development.
Here's my SPIFFS test code.
Code: Select all
/* SearchSPIFFS.ino 6 May 2017
SPIFFS plain text file has 100 lines, each line approx 1kb
After reading 30 lines (30kb) in less than 2 seconds it takes several seconds PER line
*/
#include "FS.h"
#define HOSTNAME "ESP8266-OTA-"
bool do_loop = true;
void testRead(int j) {
String line;
unsigned int lineNumber = 1;
if (SPIFFS.exists("/SomeData.txt") ) {
File MyFile = SPIFFS.open("/SomeData.txt", "r");
while ((MyFile.available()) & (lineNumber<9)) { // we could open the file, so loop through it to find the record we require
Serial.println( j + "->" + lineNumber); // show line number of SPIFFS file
line = MyFile.readStringUntil('\n'); // Read line by line from the file
Serial.println("Read :" + line);
lineNumber++;
}
MyFile.close();
} else {
Serial.println("File not Found error");
}
}
bool testWrite()
{
// Open config file for writing.
File configFile = SPIFFS.open("/SomeData.txt", "w");
if (!configFile)
{
Serial.println("Failed to open SomeData.txt for writing");
return false;
} else {
Serial.println("Opened SomeData.txt for writing.");
// Save SSID and PSK.
configFile.println("Line 1");
configFile.println("Line 2");
configFile.println("Line 3");
configFile.println("Line 4");
configFile.println("Line 5");
configFile.println("Line 6");
configFile.println("Line 7");
configFile.println("Line 8");
configFile.close();
Serial.println("Closed SomeData.txt.");
}
return true;
} // testWrite
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println(F("\nStarted"));
delay(2000);
yield();
delay(4000);
yield();
delay(2000);
yield();
delay(4000);
yield();
Serial.println("\r\n");
Serial.print("Chip ID: 0x");
Serial.println(ESP.getChipId(), HEX);
// Set Hostname.
String hostname(HOSTNAME);
hostname += String(ESP.getChipId(), HEX);
// Print hostname.
Serial.println("Hostname: " + hostname);
// Initialize file system.
if (SPIFFS.begin()) {
SPIFFS.format()
if (testWrite()) {
testRead(0);
}
} else {
Serial.println("Failed to mount file system");
return;
}
// find line 32 of plain text SPIFFS file
}
void loop() {
yield();
if (do_loop) {
do_loop = false;
for (int i = 0; i < 3; i++) {
// Initialize file system.
if (SPIFFS.begin()) {
if (testWrite()) {
testRead(i);
}
} else {
Serial.println("Failed to mount file system");
}
Serial.println("Waiting...");
delay(4000);
yield();
}
}
}