0

I writing ESP32 program and using SPIFFS to save some data because I do not want to loose it after I power down the device.

I have 2 functions:

char* readFile(fs::FS &fs, const char *path)
{
    Serial.printf("Reading file: %s\n", path);

    File file = fs.open(path);
    if (!file || file.isDirectory())
    {
        Serial.println("Failed to open file for reading");
        return "FAIL";
    }

    Serial.print("Read from file: ");
    while (file.available())
    {
        Serial.write(file.read());
        delayMicroseconds(100);
        //TODO
        //append all characters and return it as a list of char arrays at the end of reading
    }
    file.close();
    
}


void writeFile(fs::FS &fs, const char *path, const char *message)
{
    Serial.printf("Writing file: %s\n", path);

    File file = fs.open(path, FILE_WRITE);
    if (!file)
    {
        Serial.println("Failed to open file for writing");
        return;
    }
    if (file.print(message))
    {
        Serial.println("File written");
    }
    else
    {
        Serial.println("Write failed");
    }

    file.close();
}

First of all, I run my program and call writeFile() and then followed by readFile():

    int n = 0;
    Wifi_setup();
    Spiffs_setup();
    n = scan_wifi_networks();

    
    Serial.print("list_strings outside scan function = ");
    for (int i = 0 ; i<=n ; i++){
      Serial.println(found_networks[i]);
    }
    //compare list_strings with preset wifi networks. If match, everything is fine, if not, problem!


    writeFile(SPIFFS, "/wifi.txt", "Telia-33F8A3-Greitas");
    char* known_networks = readFile(SPIFFS, "/wifi.txt");

 
    //send USD to the server, go back to sleep
    //initialize_deep_sleep();
}

And that part works fine. From the serial monitor, I can see that it have sucesfully written my text to wifi.txt.

After I run write/read functions

Next, I comment out write function and only leave read function.I run the code again and it is not able to read back my text:

Only readFile

Can someone help me understand why is that happening? I thought that if I write to spiffs once, I will be able to access it afterwards but that is not the case. I have previously used EEPROM and that seemed to work. I can write to EEPROM address and simply access the same address later and the value will still be there after the power off. Any help is appreciated. Thanks in advance.

UPDATE1

I have managed to read back data from my SPIFFS after I wrote to it. I had missed one crucial step: https://randomnerdtutorials.com/install-esp32-filesystem-uploader-arduino-ide/

However, now, I am encountering another issue:

In my project folder, I have created a folder "data" which is supposed to be accessed by SPIFFS. In there, I create 2 files :

  1. wifi.txt
  2. update.bin

update.bin for now is not relevant. Lets talk about wifi.txt

After I have written to Spiffs, I now comment out the writeFile function and only leave out readFile. The result from my serial monitor:

load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:10944
load:0x40080400,len:6388
entry 0x400806b4
Listing directory: /
  FILE: /update.bin  SIZE: 0
  FILE: /wifi.txt  SIZE: 20
Reading file: /wifi.txt
Read from file: Telia-33F8A3-Greitas

As you can see from the serial monitor above, my program recognises the text that I have written to my spifs /wifi.txt file. Howeverm when I go to my actual project directory and open data/wifi.txt it is emtpy:

wifi.txt empty

How can this happen? My program recongises that theres data inside but it wont show up in the file.

UPDATE2

I have done some further testing of SPIFFS.

In my data folder, I have created another txt file test.txt. In there, I have manually put some text "this is text message".

In my program, I have called function:

char* returned_data = readFile(SPIFFS, "/test.txt");

And the serial monitor sucesfully printed the message that I have put. So that proves that the SPIFFS is able to read back from the files without any issues.

Then, I have modified my program:

    writeFile(SPIFFS, "/test.txt", "hello123");
    char* returned_data = readFile(SPIFFS, "/test.txt");

The code above should overwrite whatever I have written to my txt file with "hello123" and then my program should read the "hello123" back from Spiffs. The serial monitor response:

Listing directory: /
  FILE: /update.bin  SIZE: 0
  FILE: /wifi.txt  SIZE: 2
  FILE: /test.txt  SIZE: 8
Writing file: /test.txt
File written
Reading file: /wifi.txt
Read from file: 
Reading file: /test.txt
Read from file: hello123

As you can see, it is able to sucesfully read back "hello123".

HOWEVER, when I go to my project folder, open the data/test.txt, I can still see my initial text not replaced with "hello123". enter image description here

I do not understand how is this happening..

Lukas Petrikas
  • 65
  • 2
  • 11
  • Please post serial output as text (using code tags), not screenshots. Anyway, how have you set up your file system partition? – Tarmo Apr 16 '21 at 09:26
  • Thanks for the response. I have managed to solve the issue. I was simply missing one crucial step which is described here: https://randomnerdtutorials.com/install-esp32-filesystem-uploader-arduino-ide/. Anyways, I am having another issue with Spiffs. I have updated my initial post to describe it – Lukas Petrikas Apr 16 '21 at 10:00
  • why does it say size 1KB on wifi.txt and it's empty? – ahmed Apr 16 '21 at 10:15
  • There is data in your file, but Notepad doesn't show it. Use a hex editor to see the actual content. – Tarmo Apr 16 '21 at 10:15
  • There's an online hex editor here if you don't feel like installing one... https://hexed.it – Mark Setchell Apr 16 '21 at 10:20
  • Okay. I have installed hex editor neo. I have opened my wifi.txt with it. It shows 2 bytes of data: 0d 0a.https://imgur.com/a/jPbkOua. It is supposed to show text that I have written "Telia-33F8A3-Greitas" . – Lukas Petrikas Apr 16 '21 at 10:25
  • `0d 0a` is Carriage Return, Line Feed. So, you have and empty line followed by *"end of line"*. – Mark Setchell Apr 16 '21 at 10:46
  • I have done some further testing. I am so confused how SPIFFS works. I have updated my initial question check "UPDATE2" – Lukas Petrikas Apr 16 '21 at 10:52
  • 1
    Are you sure that what you see in your computer is the file created by your ESP32 code? I don't know how the Arduino parts there work, but I'd expect file system on internal Flash to not be visible at all via USB or some similar mechanism. – Tarmo Apr 16 '21 at 13:11
  • What @Tarmo said. SPIFFS is local to the ESP32. The contents of the filesystem will not be visible on your computer. You can use a program like [esptool](https://github.com/espressif/esptool) to write or read an image of a SPIFFS filesystem but you won't be mounting the filesystem on your computer. This may change with future versions of the ESP32 but it sounds like you're expecting it to do something now that it just doesn't do. – romkey Apr 16 '21 at 19:14
  • Oh okay.. But the file system on my computer is visible from the ESP32 device. For example in my data folder, I can create many folders and they will be found on ESP32 device. But when the ESP32 device writes into any of them, that will not be visible on my computer, only the ESP32 device will be able to see them. However, If I write into the files from my computer, the ESP32 will see them... So I can change the file system from the computer and the ESP32 will see the changes but not vice versa. – Lukas Petrikas Apr 19 '21 at 07:32

0 Answers0