Filesystem corruption

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
br0kenpixel
Posts: 5
Joined: Sun Dec 06, 2020 3:15 pm

Filesystem corruption

Post by br0kenpixel » Sat Jan 30, 2021 2:26 pm

Hi. I'm working on a school project - a weather station.
I started writing the code in Arduino IDE however I decided to switch to MicroPython since I like Python a lot more than C++ and it allows me to implement some more complicated features easier. I finished writing the code for the project (it still has some minor bugs but it's quite stable). The way it works is simple, the ESP32 starts up, measures temperature, humidity, etc., sends it to a Blynk server, and then it goes to deep sleep for a few minutes, and the whole thing repeats. The issue is that the node only works for about 1-2 hours, and after that, it stops working. After connecting it to my PC, I was greeted with a "FAT filesystem appears to be corrupted, ....." message. I did a bit of googling and found out that FAT is not really reliable, and I could try using LittleFS instead. So I did.
This is the code I used to convert the filesystem to LittleFS v2:

Code: Select all

import os
os.umount('/')
os.VfsLfs2.mkfs(bdev)
os.mount(bdev, '/')
(from https://docs.micropython.org/en/latest/ ... l#littlefs)
Before I continued, I wanted to verify that the filesystem is actually LittleFS and not FAT, however, I could not find out how. Is this even possible? Is it possible to detect the filesystem in MicroPython?
I'm not sure if it actually converted the filesystem correctly because after I let the node run for a while, after about 2 and a half hours, it stopped working, and I got the same ("FAT filesystem appears to be corrupted, .....") message. Does this mean that the filesystem wasn't actually LittleFS? Or what happened.

Ricardo
Posts: 11
Joined: Mon Dec 09, 2019 9:49 pm

Re: Filesystem corruption

Post by Ricardo » Tue Feb 02, 2021 2:04 am

Hi there!

I did something similar a while ago, but writing the data in the filesystem instead of sending it to a server. It worked fine, but sometimes when power was lost the littleFS filesystem got corrupted...

Is your code writing anything to the filesystem? If it is, is it closing the file(s) before finishing the script?

If you want to see how to detect if your filesystem is littleFS, maybe this thread can help (be sure to view this post as well for the code).

br0kenpixel
Posts: 5
Joined: Sun Dec 06, 2020 3:15 pm

Re: Filesystem corruption

Post by br0kenpixel » Tue Feb 02, 2021 8:05 am

Thank you! Yes, there is a debug feature in my code that logs error messages into a file. However, the file is closed before the deep sleep starts. I decided to add an extra line that unmounts the filesystem before deep sleep.

Code: Select all

def goToSleep(reason="OK"):
    global netman, PowerManagement, rtc, runtime_s, log, umount
    if not maintenance_switch.value():
        log.info("Maintenance mode, preventing sleep")
        LED_BUILTIN.off()
        sleep(1)
        LED_BUILTIN.on()
        return
    if netman.connected:
        netman.disconnect()
    log.disableLoggingToFile() #Close the log file
    netman.wlan.active(False)
    rtcmem["runtime"] = str(runtime_s + settings["deepSleepDuration"]["value"] * 60)
    if reason == "OK":
        log.info("Node successfully finished all jobs, goodbye")
    #log.info("simulating shutdown due to " + reason)
    #sleep(3)
    log.debug("Unmounting filesystem")
    umount("/")
    PowerManagement.deepSleep(int(settings["deepSleepDuration"]["value"] * 6e4), reason)
This still did not fix the issue.
I was experimenting with the MicroPython source code from GitHub. I downloaded and compiled the latest source code (and I also noticed that LittleFS is now the default filesystem). I don't know why, but I haven't seen that "filesystem corrupted" message since then. And the debug logging feature is still enabled.

Post Reply