Code hangs up on LittleFS.open()?

What do you see in the serial monitor when running this sketch?

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:4660
load:0x40078000,len:15568
ho 0 tail 12 room 4
load:0x40080400,len:4
load:0x40080404,len:3152
entry 0x400805a0
LittleFS mount succeeded
Attempting to open...
Reading file: /folder1/plantdata.txt

And it just hangs up on that last line, the task list showing that the monitor is still operating with the little swirly circle icon.

How do you know it hangs? There is no other output in your sketch…
For me the sketch is running.

Add the following lines at the end of your loop() function:

    static unsigned long lastMillis;
    unsigned long currentMillis = millis();
    if (currentMillis - lastMillis >= 1000) {
      lastMillis = currentMillis;
      Serial.printf("alive (%d)\r\n", currentMillis / 1000);
    }

Here is my output (without uploading the filesystem):

LittleFS mount succeeded
"We've got Bluetooth yo!" has been started. It can now be paired via Bluetooth.
Attempting to open...
Reading file: /folder1/plantdata.txt
[   533][E][vfs_api.cpp:99] open(): /littlefs/folder1/plantdata.txt does not exist, no permits for creation
- failed to open file for reading
alive (1)
alive (2)
alive (3)
alive (4)
alive (5)
alive (6)
alive (7)
alive (8)
...

And this is my output with the filesystem uploaded:

LittleFS mount succeeded
"We've got Bluetooth yo!" has been started. It can now be paired via Bluetooth.
Attempting to open...
Reading file: /folder1/plantdata.txt
alive (1)
alive (2)
alive (3)
alive (4)
alive (5)
...

Ah you’re right.

I tried adding another line of code in the readFile method:

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

  File file = fs.open(path);
  Serial.printf("Opened: %s\r\n", path); // <-- added this line
  if (!file || file.isDirectory()) {
    Serial.println("- failed to open file for reading");
    return;
  }
}

And it did print out. I think I was expecting it to print a message that the file did in fact open, even though no such confirmation actually exists.
I completely forgot that there’s no other output that gets executed in my code and it actually worked out well.

At some subconscious level I think I’m also so used to things going wrong that I was just expecting an error message!

I also added the lines of code you suggested and got the “alive(1)”, etc. outputs.

Well, I feel stupid having missed something so simple and spending so much time over something that wasn’t a problem lol. But thank you so much for your help. :smiley: I appreciate it greatly!

No problem.
You’re welcome. :wink:

1 Like