Skip to content

Data logger file system is corrupted when power is lost #7065

@willtalmadge

Description

@willtalmadge

CircuitPython version

adafruit-circuitpython-adafruit_qtpy_rp2040-en_US-7.3.3.uf2
adafruit-circuitpython-raspberry_pi_pico-en_US-7.3.3.uf2

Code/REPL

import time
import busio
import board
import adafruit_mcp9808
import microcontroller, digitalio, time
import storage

d = digitalio.DigitalInOut(microcontroller.pin.GPIO21)
i2c = busio.I2C(scl=board.GP1, sda=board.GP0)
mcp = adafruit_mcp9808.MCP9808(i2c)
try:
    storage.remount("/", False)
    can_write_to_file = True
except:
    can_write_to_file = False

while True:
    tempC = mcp.temperature
    tempF = tempC * 9 / 5 + 32
    print("Temperature: {} C {} F ".format(tempC, tempF))
    tempBytes = int(tempC*1000).to_bytes(2, 'big')
    print(int.from_bytes(tempBytes, 'big'))
    
    if can_write_to_file:
        with open("/temps.txt", "ab") as fp:
            fp.write(time.time().to_bytes(4, 'big'))
            fp.write(tempBytes)
        
    time.sleep(1)

Behavior

The code writes temperatures to the serial logger only when attached to USB, it logs temperatures to a file when run on battery power alone.

Description

Occasionally when I pull the power, the whole file system is corrupt. When I plug the device into my mac, it mounts with a disk called "NO NAME" and has only the "boot_out.txt" file on the drive.

Restoring the uf2 file seems to have no effect in reseting the system to a state where I can install code. If I attempt to save "code.py" I get the error:

Error saving file to disk. Ensure you have permission to write the file and sufficient disk space.

My guess is that the device gets corrupted either when the file is open or it's in the middle of writing to the file and power is lost. If this is the case, Is there a way to use circuit python as a data logger without the whole file system getting corrupted when the battery runs out?

Additional information

I've encountered this same issue on a QT PY and a Raspberry Pi Pico so it seems to be related to circuit python

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions