In the first part of the article , we’ve covered Micropython usage with UiFlow and VS Code environments. In the second one, we use C/C++ with more granular device control. This time, we’ll try to run Micropython with an external library added.
Micropython with interactive console
There are two projects that enable Micropython for ESP32 to run external libraries. The first one is M5Stack-official https://github.com/m5stack/Core2forAWS-MicroPython, and the second one is unofficial https://github.com/mocleiri/tensorflow-micropython-examples. The common part is – you can run an interactive Micropython console on the controller using the serial port. Unfortunately, this is the only way to go. There is no IDE and you can’t upload a complex, multi-file application.
To open the console, you can simply use Putty and connect to the proper COM port (COM3, in my case) with 115200 speed.
To run the first project, the best way is to follow the official README documentation, but there is a bug in the code here:
One file is opened in ‘w’ (write) mode and another in ‘rb’ (read bytes). You need to change ‘w’ to ‘wb’ to run any example from the readme. It’s a good codebase because it’s small and M5Stack official. It contains upip, so you can include more official libraries after connecting to the Internet. You can also extend the codebase with more libraries before the build (some extra libraries are available in another official repository https://github.com/m5stack/micropython-lib). However, TensorFlow is a complex library with multiple dependencies, so using the unofficial project is easier.
The Tensorflow Micropython Examples project offers pre-built images to download directly from GitHub. For our controller, you need the ESP32 version (no ESP32 S3) for 16MB memory.
Just open the GitHub Actions page https://github.com/mocleiri/tensorflow-micropython-examples/actions/workflows/build_esp32.yml, pick the newest green build and download the latest version.
Then extract the zip package and burn it using command ‘esptool.py -p /dev/ttyUSB0 -b 460800 –before default_reset –after hard_reset –chip esp32 write_flash –flash_mode dio –flash_size detect –flash_freq 40m 0x1000 bootloader/bootloader.bin 0x8000 partition_table/partition-table.bin 0x10000 micropython.bin’. Ensure you have passed the USB port to WSL using usbip and allowed all users to use it with chmod (see the first part for details).
With this project, you can import the microlite library, which is exactly the one you need – TensorFlow Lite for Microcontrollers. If you want to extend this project with your own libraries, you can add those to the cloned source code and build it with the following commands:
git submodule init git submodule update --recursive cd micropython git submodule update --init lib/axtls git submodule update --init lib/berkeley-db-1.xx cd .. source ./micropython/tools/ci.sh && ci_esp32_setup_helper v4.3.1 source ./esp-idf/export.sh #as you can see, esp-idf is already included pip3 install Pillow pip3 install Wave rm -rf ./micropython-modules/microlite/tflm cd ./tensorflow ../micropython-modules/microlite/prepare-tflm-esp.sh cd ../micropython make -C mpy-cross V=1 clean all cd ../boards/esp32/MICROLITE_SPIRAM_16M rm -rf build idf.py clean build
Your binaries are located under the boards/esp32/MICROLITE_SPIRAM_16M/build directory.
This way, you can add more libraries or your own program to the image, but you still need to use the interactive console to run it.
This three-part workshop aimed to utilize M5Stack Core2 for AWS controller with AWS IoT connection and ML included. Let’s try to sum up all possible ways to do that.
|Micropython||UiFlow||Python Low learning curve/easy UI OTA updates||No Tensorflow available Not adequate for complex use cases|
|VS Code with vscode-m5stack-mpi plugin||Python Full IDE (Visual Studio Code)||No Tensorflow available|
|Tensorflow Micropython Examples||Micropython with TensorFlow Lite for Microcontrollers Capability to build the project with more libraries or custom code included||Necessity to run the code from interactive Python console.|
|C/C++||VS Code with PlatformIO and FreeRTOS||All libraries available||Complex code (C/C++) Complex configuration|
|Arduino IDE||All libraries available Easy and powerful enough IDE||Arduino control loop limitation|
|ESP-IDF||Small solution, without even a dedicated IDE (plugins for CS Code or Eclipse available)||Complex dependency Management|
As you can see, we’ve tried various approaches and discovered their advantages and disadvantages. Now, you can decide if you wish to have complete control and use pure C/C++, or maybe you prefer much more friendly Python. You can choose which IDE or at least plugin you’d like to use, and whether you want to utilize OTA to update an entire firmware or only to transfer data between your devices and the cloud.