How can I tell what caused an ESP32 board to restart?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
timg11
Posts: 19
Joined: Sun Feb 14, 2021 10:51 pm

Re: How can I tell what caused an ESP32 board to restart?

Post by timg11 » Mon Jul 05, 2021 4:07 pm

The board was rebooting several times a day in its operational mode.
I have a "development board" where I can plug in an ESP32 module, and test the code with pushbuttons to simulate expected and unexpected input, and LEDs for output. It never reboots in that board.

I was able to put a notebook computer nearby the operational unit, connected to the USB-serial port on the ESP32. I used PUTTY to log the console output. After a day, zero reboots. So having a USB cable connected somehow causes it to work better? I'll have to think about that - maybe the issue is power-related since the USB cable also supplies +5V power?

Next I'm starting to learn about micropython file systems and logging. I saw this post about logging, and this reference on the file system.

Any better examples to work from?

timg11
Posts: 19
Joined: Sun Feb 14, 2021 10:51 pm

Re: How can I tell what caused an ESP32 board to restart?

Post by timg11 » Mon Jul 05, 2021 5:25 pm

The code example shows how to implement a ramdisk on the ESP-32.

Code: Select all

import os

bdev = RAMBlockDev(512, 50)
os.VfsFat.mkfs(bdev)
os.mount(bdev, '/ramdisk')
I can add this code and write to a file and read it back successfully.

Code: Select all

    with open('/ramdisk/hello.txt', 'w') as f:
        f.write('This file wrote at startup')
    print(open('/ramdisk/hello.txt').read())
A ramdisk does not maintain contents through a boot. For my goal of logging exceptions that cause a reboot for diagnosis, I need a flash filesystem.

This article says:
On power-on, MicroPython will attempt to detect the filesystem on the default flash and configure and mount it automatically. If no filesystem is found, MicroPython will attempt to create a FAT filesystem spanning the entire flash.
I have not found any details on how the "automatically" mounted file system is accessed or used, or even how to determine if it exists. How would MicroPython code on an ESP32 access that filesystem to write a small file that can persist between reboots? I'm still learning, so a simple example would be helpful.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: How can I tell what caused an ESP32 board to restart?

Post by davef » Mon Jul 05, 2021 6:56 pm

timg11,

I have been fighting the same problem for the last year.
I have not found any details on how the "automatically" mounted file system is accessed or used, or even how to determine if it exists. How would MicroPython code on an ESP32 access that filesystem to write a small file that can persist between reboots? I'm still learning, so a simple example would be helpful.
A littlefs filesystem is automatically mounted when you load in the images on the download page. What I ended up doing, so I wouldn't lose import datalogging information is every hour append the contents of /ramdisk/datalog.csv to datalog.csv (in flash). As ramdisk is limited I do the following:

Code: Select all

#  save data
        try:
            with open('/ramdisk/datalog.csv', 'a') as outfile:
                outfile.write(time + ',')
                outfile.write(str(Battery_voltage) + ',')
                outfile.write('\n')
        except OSError as err:
            try:
                with open('errors.txt', 'a') as outfile:
                    my_error = err.args[0]
                    outfile.write('saving data failed  ' + str(my_error) + '\n')
            except OSError:
                pass


        if (minutes == '00'): # every hour append /ramdisk/datalog.csv to datalog.csv
            src_file = open('/ramdisk/datalog.csv', 'r')
            dest_file = open('datalog.csv', 'a')
            copyfileobj(src_file, dest_file, length = 512)
            src_file.close()
            dest_file.close()

         #  remove /ramdisk/datalog.cvs
            try:
                uos.remove('/ramdisk/datalog.csv')
            except OSError as err:
                try:
                    with open('errors.txt', 'a') as outfile:
                        my_error = err.args[0]
                        outfile.write('removing /ramdisk/datalog.csv failed  ' + str(my_error) + '\n')
                except OSError:
                    pass

marcidy
Posts: 133
Joined: Sat Dec 12, 2020 11:07 pm

Re: How can I tell what caused an ESP32 board to restart?

Post by marcidy » Wed Jul 07, 2021 6:46 am

I have not found any details on how the "automatically" mounted file system is accessed or used, or even how to determine if it exists. How would MicroPython code on an ESP32 access that filesystem to write a small file that can persist between reboots? I'm still learning, so a simple example would be helpful.
Note this extracted from davef's answer. Just wanted to highlight it specifically:

Code: Select all

>>> with open('errors.txt', 'a') as outfile:
....    my_error = err.args[0]
....    outfile.write('saving data failed  ' + str(my_error) + '\n')
Works the same just as from python repl, just use the "open" function and you can access the filesystem.

check out the os module for filesystem tools like os.mkdir, os.listdir, os.rename, etc

you basically have access to a filesystem starting with a root "/" directory.

How are you loading your firmware?

If using the webrepl, you can do

Code: Select all

$ webrepl_cli.py -p password filename.py 192.168.0.10:/path/to/filename.py
for example to put a file at a location on the filesystem (note the directory has to exist already, this won't create directories, only files).

You can deploy as packages and stuff too with __init__.py and everything. relative imports work! Everything works!

Post Reply