0

I try to do some basic read/write file on SPIFFS on ESP8266 ESP-01. After writing a file, if I try to read it, it always returns 0 read bytes. The same code works well on NodeMCU 10 (ESP12E Module).

It is interesting, that I can upload files to ESP8266-01 SPIFFS (I use vMicro in Visual Studio for development and "Publish Server Data Files (spiffs)") and read them OK with my code. It is only if I write a file, the read does not work for that file. I tried different combinations for ESP-01 "Generic ESP8266 Module" (Flash sizes 1M, 512K, SPIFFS sizes 32K, 64K, 128K) with same result.

The sketch code is here (including many debug statements):

#include <FS.h>

const char* fileName = "/abc.txt";

void setup() {
    Serial.begin(115200);
    delay(5000);

    Serial.println("\n\nStarting...");

    if (SPIFFS.begin()) {
        Serial.println("begin OK");

        if (SPIFFS.format()) {
            Serial.println("format OK");

            auto fW = SPIFFS.open(fileName, "w");
            if (fW) {
                Serial.println("Open W OK");

                auto nWritten = fW.write((byte*)"aaaAAA", 6);
                Serial.printf("Written: %d\n", nWritten);

                fW.close();
                Serial.println("Closed W OK");
            }
            else {
                Serial.println("Failed opening W");
            }

            delay(5000);
            Serial.println("");

            auto fR = SPIFFS.open(fileName, "r");
            if (fR) {
                Serial.println("Open R OK");

                auto fSize = fR.size();
                Serial.printf("Size: %d\n", fSize);

                Serial.printf("Position: %d\n", fR.position());

                if (!fR.seek(0)) {
                    Serial.println("****Seek failed!");
                }
                Serial.printf("Position: %d\n", fR.position());
                Serial.printf("Available: %d\n", fR.available());

                char buffer[20] = { 0 };
                auto nRead = fR.readBytes(buffer, fSize);
                Serial.printf("nRead: %d\n", nRead);
                Serial.printf("Data: [%s]\n", buffer);

                fR.close();
                Serial.println("Closed R OK");
            }
            else {
                Serial.println("Failed opening R");
            }
        }
    }
}

void loop() {
}

Output is here. Note that size is 6 (which is correct) but nRead is 0, and Data in between [] is empty.

Starting...
begin OK
format OK
Open W OK
Written: 6
Closed W OK

Open R OK
Size: 6
Position: 0
Position: 0
Available: 6
nRead: 0
Data: []
Closed R OK

If I comment out SPIFFS.format() statement (because on second load of the sketch the SPIFFS is already formatted and the file is already there) I cannot even open the file for writing.

So, does SPIFFS on ESP-01 has writing problems, and what can I do to be able to write files there successfully?

sabs
  • 1
  • some ESP01 modules have a cheaper flash chip that doesn't work with the normal ardunio lib. It's fixable though; you have to patch a file in the hardware folder and that should get you up and running. i used the code at https://github.com/esp8266/Arduino/issues/4061#issuecomment-428007580 – dandavis Nov 02 '18 at 19:44
  • Thank you, that worked! – sabs Nov 03 '18 at 21:33

0 Answers0