Avoid code crash and exit

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
ajocius
Posts: 83
Joined: Mon Feb 19, 2018 6:31 am

Avoid code crash and exit

Post by ajocius » Tue Dec 31, 2019 1:26 pm

i do have a code that measures temperature from two sensors : DHT22 and DS1820. Both work fine until something happens. Either it does not receive reading back or get's response in a wrong format and then code crashes and exists. I do then restart ESP32 via reset button and all works fine again until next crash.

I would like the code not to exit if the reading is not received or it is in wrong format. I just want to disregard it. I do readings pretty often, so missing one cycle is not a problem at all. Bigger problem is that the whole process is down that might have bad result. I am using it in small in house greenhouse with LED grow lights and have temperature control using PC fans. So if the code exits and LED lamps are on, there is a risk that temperature will go too high, something I need to avoid.

I use command "sensor.measure()" to read from DHT22 sensor. And I use following code for DS1820:

Code: Select all

roms = ds.scan()
print('found devices:', roms)
addr = roms.pop()
ds.convert_temp()
 temp = ds.read_temp(addr)
How can I tell code to disregard wrong readings?

Also, is there any way to write log into the file on ESP32? something that would tell me what line of the code it crashed, so that I can hopefully address it?

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Avoid code crash and exit

Post by stijn » Tue Dec 31, 2019 4:18 pm

Assuming 'crash' actually means 'unhandled exception' and not something else you'd use standard try/except:

Code: Select all

try:
  ds.convert_temp()
  temp = ds.read_temp(addr)
except Exception as e:
  print('something wrong, reason:', e)
Printing the exception will show what went wrong and where.

No idea if ESP32 has a filesystem but if so there are many ways to write into it. You can just pass a file to print() for instance. Something like:

Code: Select all

with open('/path/to/logfile.log', 'w') as logFile:
  print('something wrong, reason:', e, file=logFile)
Depending on the application you might open the file each time, or open once at the beginning then keep it open as long as the application lives.
Or use the standard logging library from micropython-lib, which would allow logging messages to console/file/.. at the same time.

Another way to debug would be to copy/paste your code on the REPL: upon the first exception it will stop and show the exception.

User avatar
Mike Teachman
Posts: 155
Joined: Mon Jun 13, 2016 3:19 pm
Location: Victoria, BC, Canada

Re: Avoid code crash and exit

Post by Mike Teachman » Tue Dec 31, 2019 4:29 pm

Pretty much the same as previous poster ... here is a technique I find useful to identify where application code is crashing on an unhandled exception.
  • wrap the whole application in a try statement
  • log the exception to a text file, using append mode so you can log multiple exceptions
  • force a hardware reset so the application restarts
Here is some example code:
Note: updated as per suggestion from @stijn (removed explicit file create)

Code: Select all

import sys
import machine

try:
  [your application code here]
except Exception as e:
    # "should" never get here.  
    # Save exception to a file and force a hard reset
    emsg = 'Unexpected Exception {} {}\n'.format(type(e).__name__, e)
    print(emsg)
    with open('exception.txt', mode='at', encoding='utf-8') as f:
        f.write(emsg)
    machine.reset()
Last edited by Mike Teachman on Tue Dec 31, 2019 6:03 pm, edited 3 times in total.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Avoid code crash and exit

Post by stijn » Tue Dec 31, 2019 5:30 pm

There shouldn't be any need to create the file if it doesn't exits, using an open mode like 'a+' does that but using way less code.

User avatar
Mike Teachman
Posts: 155
Joined: Mon Jun 13, 2016 3:19 pm
Location: Victoria, BC, Canada

Re: Avoid code crash and exit

Post by Mike Teachman » Tue Dec 31, 2019 5:44 pm

good suggestion @stijn. I'll update my post to remove the file create bit - left the mode as 'at' --- '+' is for reading and not needed in this use case

ajocius
Posts: 83
Joined: Mon Feb 19, 2018 6:31 am

Re: Avoid code crash and exit

Post by ajocius » Wed Jan 01, 2020 3:04 pm

thank you guys, will test suggestions out,

Post Reply