So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Barry Scott
#76786 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.

Image

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 allreadStringUntil()
.
If I use ReadBytes, I get junk
Code: Select allWriteConfigFile: 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();
    }
  }
}

User avatar
By Barry Scott
#76823 Thanks a million. For some reason, none of my Google searches brought me to the issues in the Arduino github.
I do have Puya flash on my ESP, and it seems all newer ESP's are moving to that brand.

Here's a link to the issue and patch for others who might find this thread.
https://github.com/esp8266/Arduino/issues/4061

As of 2.4.1, there's been a lot of discussion about the issue and a fix, but users are manually patching their ESP.cpp.
The code change does not seem to have been commited to the Arduino codebase.