How to deploy CircuitPython powered device without it going into safe mode after powering off and/or needing human reset button intervention?
@bradspry I have a fair amount of experience working with CircuitPython and deploying it in the field.
Can you describe the scenario(s) that cause the MCU to enter into safe mode? In my experience, CP only goes into safe mode when the filesystem gets corrupted. Does your application regularly write files to the filesystem? Are you safely removing the CircuitPython drive from your computer before you unplug the microcontroller?
If you write files to the filesystem regularly, then it is likely that the device is suddenly losing power or browning out in the field. I would highly recommend adding at least a small LiPo battery to the system to prevent sudden power downs of the microcontroller (which, if it’s in the middle of writing a file to the disk, can cause the filesystem corruption that forces CP into safe mode). You can add a routine to your application that checks if you’re running off battery power and, if so, to ensure that you do not write to the filesystem to avoid corruption.
@JordanAbacus thank you for your reply!
My device is a “take home” device and meant to be given to someone to take home to plug in. The moment I unplug a device to give it to someone, CircuitPython considers the event a brown-out. Upon next device plug in, device will be in safe mode due to brown out condition. One would need to press the MCU’s reset button to exit safe mode and begin running code.py normally. Code.py runs great when not in safe mode. I do have a small LiPo battery attached, but haven’t crunched the numbers on the amount of power needed to run the device for a period of time between plug-ins… Maybe on-detection of running on battery power, I could shut down unneeded peripherals and save as much power as possible until device is plugged back in? I just worry about LiPo battery deployment safety.
Sorry for my delayed response, @bradspry
When you unplug the device on your end before giving it to someone, are you unplugging it from your computer? If so, are you safely ejecting the CIRCUITPY drive before unplugging the device? It sounds like the CircuitPython filesystem is getting corrupted (i.e., a write operation is being interrupted) which causes CircuitPython to go into safe mode.
Also, does your application write to the filesystem? If not, I would highly recommend that you mount the filesystem as read-only in your
boot.py file using the following:
See Adafruit’s guide on CircuitPython storage for more info.
In my experience, using a read-only file system allows you to cut the power to your device without risk of filesystem corruption. I have deployed over a hundred devices that lose power periodically and have never had an issue with the devices going into CircuitPython’s safe mode due to filesystem issues.
If you are writing to the filesystem regularly, I would ask – why? Is it necessary? If you’re logging a lot of information, consider adding an SD card to the system. If you only write a small amount of info, consider using NVM if it’s available on your MCU’s CircuitPython build, or use the Notecard’s database feature as off-MCU storage there are many safer options than writing directly to CircuitPython’s filesystem that will increase reliability
Amazing reply, thank you so much! I will test every suggestion, thank you again!