Working with microcontrollers is a bit trickier than working with a traditional computer due to the limited memory and processing power us programmers need to keep in mind. This can lead to some very tricky bugs that are close to impossible to catch merely using the naked eye. I have been using Micropython on the ESP8266 for the past couple of months now and must say that I am absolutely loving it. However, the documentation does not show or explain how to properly log your programs. Here is an example of how I log my Micropython programs:
from sys import print_exception try: foo() except Exception as e: with open(file='crash_logs.txt', mode='w') as f: print_exception(e, f)
The print_exception function is a part of Micropython's sys module and provides system-specific functions. The open function is a built-in python function used to open files. The first parameter is used to define the name of the file, the second parameter defines the mode. For example 'r' is read. In this case, we use 'w' since we want to write to the file. The code above shows an example of using the print_exception function which takes two parameters: An exception object, and a file descriptor. The print exception function will not only write the exception to that file but also "Print exception with a traceback" which makes debugging complex programs in the field 1000x easier. This technique utilizes Micropythons internal filesystem and allows you to deploy microcontrollers and preserve any crash logs (rename file for different functions).
Feras | June 29, 2019, 8:17 a.m.