Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By paulfer
#52214 Hi Guys

Thanks for a great service!

Can you please confirm my suspicions? I have a file f.txt, that I write to, during the execution of my sketch.
However, it would appear that the f.txt gets overwritten when I upload a new sketch to the esp8266.

Is there anyway I could NOT over write that or do I have to upload the f.txt everytime?

The reason I ask is that if one day, I venture to OTA, the device would lose the user data in the f.txt

Regards and many thanks!

Paul
User avatar
By paulfer
#52240
martinayotte wrote:I've never seen such behavior. SPIFFS remains intact after sketch upload or OTA, except if you explicitly do a flash erase.


Hi Martin, thanks a lot, for the response.

I have been trouble shooting this all night. I have discovered the following:

1. The f.txt file is not being deleted. After I upload a new sketch. However;
2. I am simply reading 4 bytes from the f.txt with .readbytes() and if I read after a new upload of a sketch, the 4 bytes become corrupted.

Here is my code:

Code: Select allvoid loadFSUserData(char* strDAT,int dataLen, byte what)
{
  if(SPIFFS.begin())
  {
    if(what==READFS)
      {
        File f = SPIFFS.open("/f.txt", "r");
        if (!f)
        {
          Serial.println("f.txt open failed");
        }
        else
        {
            //read all bytes into buffer
            f.readBytes(strDAT,dataLen);    //read the contents into the strDAT
            delay(10);
            f.close();
            delay(10);     
        }
      }
    if(what==SAVEFS)
      {
        File f = SPIFFS.open("/f.txt", "w");
        if (!f)
        {
          Serial.println("f.txt save failed");
        }
        else
        {
            //write to f.txt (truncated to 0)
            f.println(strDAT);
            delay(10);
            f.close();
            delay(10);
        }
      }
           
    SPIFFS.end();
  }
  else
  {
    Serial.println("SPIFFS fail.");
  }
}
User avatar
By martinayotte
#52272 I don't see where the problem reside.
It must be something else in your code, but I don't see it in the above code.

BTW, just for code cleanness, you don't need all those delay() at all. Also, just to be consistent with your f.readBytes(), you should use f.write() instead of f.println().