Tags:
create new tag
view all tags

Compiling Micropython

Forcing the correct module versions

When trying to compile the ESP32 port of MicroPython on an Ubuntu-20.04 system, I see the following error:

compileError.png

As the message states, we are running a version of pyparsing which is too recent for the ESP_IDF development environment and we must create a virtual environment for Python3 with an older version of pyparsing.

This is how I did it:

First I created a directory ~/pythonEnvironments in my home directory.
Then I created a virtual environment mp_env for MicroPython compilation:

python3 -m venv mp_env

Once the environment is created, I can activate it:

source mp_env/bin/activate

In my new environment I can use pip to load the modules I need with the correct version numbers:

pip3 install pyparsing==2.3.1
pip3 install pyserial

When this is accomplished compilation of MicroPython works as expected:

virtualEnv.png

As soon as I am done I can deactivate the environment and return to the standard Python environment with the deactivate command.

Freezing modules into the MicroPython binary

We want to give students easy access to drivers for the sensors that we use during the course. If we compile and freeze these modules into the MicroPython binary, then these modules are available for import without the necessity to upload them to the ESP32 before use. in addition, this method frees valuable RAM space, a resource that is very scarce on the machine.

Freezing of MicroPython modules into the binary is pretty easy: We must simply copy the Python code into the ports/esp32/modules directory of the MicroPython source before compilation. Here is the list of additional modules that are currently included:

modules.png

Including user C modules

Sometimes it may be needed to write modules in C, which can be called by MicroPython. A typical example is the readout of the DHT11 when we want to get access to the raw data sent through the DHT11 proprietary protocol. The serial data bits come in too fast to be treated by Python code. To include such a module we add a user_module (could be any name) directory in which we create the module directory (dht11Raw in case of the DHT11). Into this directory we put the C code and a micropython.mk file.

In order to make the user module known and enabled by MicroPython, we must compile the interpreter with the following command (valid for the dht11Raw module):

make USER_C_MODULES=../../../user_modules CFLAGS_EXTRA=-DMODULE_DHT11RAW_ENABLED=1

For more details about C user modules in MicroPython see https://docs.micropython.org/en/latest/develop/cmodules.html and
https://micropython-usermod.readthedocs.io/en/latest/usermods_01.html

The source code for the example module described in the docs and the dht11Raw module can be found in this gzipped tar archive:

https://iotworkshop.africa/pub/IoT_Course_English/CompilingNopMicroPython/user_modules.tar.gz

Debugging MicroPython

When using the ESP32 port of MicroPython you can use the logging facilities provided by ESP-IDF. Typically you define a DEBUG_printf function of this type:

debugPrintf.png

The ESP-IDF logging facility knows 5 levels of error logging:

  • ESP_LOGE: for errors
  • ESP_LOGW: for warnings
  • ESP_LOGI: for information messages (this level has been chosen in our example above)
  • ESP_LOGD: for debugging
  • ESP_LOGV: for verbose
You can set the logging level up to which you want to see the messages with

esp.osdebug(0,esp.LOG_INFO)

setting the level to "information". In this case LOGE, LOGW and LOGI messages will be shown but not LOGD and LOGV messages. If you set

esp.osdebug(None)

no messages will be shown.

The screen dump below shows the output of help(esp) with all the methods and constants defined in this module.

espModule.png

-- Uli Raich - 2020-04-29

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng compileError.png r1 manage 59.7 K 2020-04-29 - 18:02 UliRaich  
PNGpng debugPrintf.png r1 manage 19.7 K 2020-05-13 - 15:23 UliRaich  
PNGpng espModule.png r1 manage 49.3 K 2020-05-13 - 15:32 UliRaich  
PNGpng modules.png r1 manage 34.3 K 2020-04-29 - 18:58 UliRaich  
PNGpng upysh.png r1 manage 15.7 K 2020-05-07 - 08:55 UliRaich  
Unknown file formatgz user_modules.tar.gz r1 manage 2.2 K 2020-05-02 - 08:31 UliRaich  
PNGpng virtualEnv.png r1 manage 161.3 K 2020-04-29 - 18:18 UliRaich  
Edit | Attach | Watch | Print version | History: r9 < r8 < r7 < r6 < r5 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r9 - 2020-10-12 - UliRaich
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback