Code hangs up on LittleFS.open()?

[PlatformIO, LittleFS] Code getting hung up on LittleFS.open(file)…?

In the setup() of my main.cpp file, I have the following code to setup the LittleFS framework:

  Serial.begin(115200); // initialize

  // set up file system
  if(!LittleFS.begin(true)) {Serial.println("LittleFS mount failed.");} else {Serial.println("LittleFS mount succeeded");}

And of course I have the preprocessing directive:

#include "LittleFS.h"

I then have this bit to try and check to see if a file exists and open it. If not, then create it:

      std::string filename = "/folder1/plantdata.json";
 -->  if(!LittleFS.open(String(filename.c_str()), "r")) {
        Serial.println("Open attempt failed");
        WaterSoil::createFirstJSONFile(filename);
      }

Now when I test it, it gets hung up on the line with the arrow and doesn’t even show the “open attempt failed”, even though in the past it did not and was able to actually enter this block.

Any idea why? I feel like it should be obvious.

What’s the content of your platformio.ini?
Especially which board and partition settings do you use?

Did you try the LittleFS basic example sketch?

I’m a bit embarrassed to admit, I’ve seen that example sketch and it def looks helpful, bur I’m not sure how I would try that in my project. Do i just download it and add it to my project as a library?

Will get back to you on the platformio.ini config.

This library is part of the Espressif Arduino framework and already available on your computer. No download required.

You can simply copy & paste the code from the github page.

But as the code is also available on your computer, you can navigate to the example file:
Ctrl + left click on “LittleFS.h” in the #include <LittleFS.h> line.
This will bring you to the LittleFS.h source file on your computer.
Now click on src and navigate to examples / LITTLEFS_test / LITTLEFS_test.ino:

Now you can copy and paste the code from here.

Here is my platformio.ini code

[env:esp32dev]
platform = https://github.com/pioarduino/platform-espressif32/releases/download/54.03.20/platform-espressif32.zip
board = esp32dev
framework = arduino
monitor_speed = 115200
monitor_eol = CRLF
lib_deps = 
	bblanchon/ArduinoJson@^7.4.1
	rw0402/littlefs@^2.9.3

This must be removed!
LittleFS is part of the Espressif Arduino Framework.

OK. Is it a problem if this is included? Like is it an issue if there’s that redundancy?

Remove that library!
Espressif has its own LittleFS implementation which is especially fits the ESP32!

So I’ve removed the library. In addition:

  • I copy-pasted the readFile() function from the examples into my main.cpp
  • I added “board_build.filesystem = littlefs” to plaformio.ini
  • Did the “Build Filesystem Image” and “Upload Filesystem Image”
  • added a “data” folder to the project

It is still stalling on the “open” command.

File file = fs.open(path);

What is the result from the example code?
Not just a snippet of the example but the full example.

The call in the main function is this

      std::string filename = "/folder1/plantdata.txt";
      Serial.println("Attempting to open...");
      readFile(LittleFS, "/folder1/plantdata.txt");

The example code I copied is this function:

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

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

Output is

Attempting to open...
Reading file: /folder1/plantdata.txt

And on the task list the “Monitor” task keeps swirling:

Could it have something to do with a missing “config.json” file from data? Or the fact that I have included both “FS.h” and “LittleFS.h”?

Please run the full LittleFS basic example sketch I mentioned before and show the results.

No, that’s not the reason.

How can I run it? Do I need to copy the file into one of the folders in the project directory? Or is it like a CLI command? Sorry, I’m not used to running anything other than my main.cpp file on an ESP. Some step-by-step instructions would help.

Create a new project.
Copy and paste the example code into the main.cpp.

1 Like
SPIFFS-like write file to new path and delete it w/folders
Create missing folders of: /new1/new2/new3/hello3.txt
Writing file to: /new1/new2/new3/hello3.txt
- file written
Listing directory: /
  DIR : folder1
Listing directory: /folder1
  FILE: plantdata.txt   SIZE: 0
  DIR : new1
Listing directory: /new1
  DIR : new2
Listing directory: /new1/new2
  DIR : new3
Listing directory: /new1/new2/new3
  FILE: hello3.txt      SIZE: 6
  FILE: test.txt        SIZE: 0
Deleting file and empty folders on path: /new1/new2/new3/hello3.txt
- file deleted
Removing all empty folders on path: /new1/new2/new3/hello3.txt
Listing directory: /
  DIR : folder1
Listing directory: /folder1
  FILE: plantdata.txt   SIZE: 0
  FILE: test.txt        SIZE: 0
Creating Dir: /mydir
Dir created
Writing file: /mydir/hello2.txt
- file written
Listing directory: /
  DIR : folder1
Listing directory: /folder1
  FILE: plantdata.txt   SIZE: 0
  DIR : mydir
Listing directory: /mydir
  FILE: hello2.txt      SIZE: 6
  FILE: test.txt        SIZE: 0
Deleting file: /mydir/hello2.txt
- file deleted
Removing Dir: /mydir
Dir removed
Listing directory: /
  DIR : folder1
Listing directory: /folder1
  FILE: plantdata.txt   SIZE: 0
  FILE: test.txt        SIZE: 0
Writing file: /hello.txt
- file written
Appending to file: /hello.txt
- message appended
Reading file: /hello.txt
- read from file:
Hello World!
Renaming file /hello.txt to /foo.txt
- file renamed
Reading file: /foo.txt
- read from file:
Hello World!
Deleting file: /foo.txt
- file deleted
Testing file I/O with /test.txt
- writing................................................................
 - 1048576 bytes written in 15214 ms
- reading................................................................
- 1048576 bytes read in 310 ms
Deleting file: /test.txt
- file deleted
Test complete

So it looks like it’s otherwise working properly in this project. So it’s probably not a hardware or library issue, but rather something with my earlier project.

1 Like

That proves that LittleFS.open works without issues and the issue must be something in your code. Without knowing your code it is hard to find out what the cause is. Maybe you can publish your code, like on github?

I can try. Unfortunately I got locked out of my Gitlab account and Gitlab isn’t sending the email password reset emails…I can try to find an alternate.

Ok, found a workaround. Seems GitHub is a bit more responsive than GitLab:

1 Like

OK, so I created a new platformio project, copying only the “main.cpp” and “platformio.ini” from the original project (plus dependencies ArduinoJson and NTPClient, which I don’t think are the problem), then running it again.

It seems it’s stuck on the same command (stalls on the “fs.open(path)” portion):

File file = fs.open(path);

This tells me that the problem is isolated to the “main” method, or to the platformio.ini. Or perhaps to some sort of configuration in my files.