September 23, 2023

Within the first a part of the article , we’ve lined Micropython utilization with UiFlow and VS Code environments. In the second, we use C/C++ with extra granular machine management. This time, we’ll attempt to run Micropython with an exterior library added.

Micropython with interactive console

There are two initiatives that allow Micropython for ESP32 to run exterior libraries. The primary one is M5Stack-official https://github.com/m5stack/Core2forAWS-MicroPython, and the second is unofficial https://github.com/mocleiri/tensorflow-micropython-examples. The widespread half is – you possibly can run an interactive Micropython console on the controller utilizing the serial port. Sadly, that is the one option to go. There isn’t any IDE and you’ll’t add a fancy, multi-file software.

To open the console, you possibly can merely use Putty and hook up with the right COM port (COM3, in my case) with 115200 velocity.

To run the primary challenge, one of the best ways is to observe the official README documentation, however there’s a bug within the code right here:

https://github.com/m5stack/Core2forAWS-MicroPython/blob/master/ports/esp32/makelfs2.py#L20.

One file is opened in ‘w’ (write) mode and one other in ‘rb’ (learn bytes). You might want to change ‘w’ to ‘wb’ to run any instance from the readme. It’s an excellent codebase as a result of it’s small and M5Stack official. It incorporates upip, so you possibly can embody extra official libraries after connecting to the Web. You may also prolong the codebase with extra libraries earlier than the construct (some further libraries can be found in one other official repository https://github.com/m5stack/micropython-lib). Nonetheless, TensorFlow is a fancy library with a number of dependencies, so utilizing the unofficial challenge is less complicated.

The Tensorflow Micropython Examples challenge presents pre-built photographs to obtain instantly from GitHub. For our controller, you want the ESP32 model (no ESP32 S3) for 16MB reminiscence.

Simply open the GitHub Actions web page https://github.com/mocleiri/tensorflow-micropython-examples/actions/workflows/build_esp32.yml, choose the latest inexperienced construct and obtain the most recent model.

Micropython and C++

Then extract the zip bundle and burn it utilizing command ‘esptool.py -p /dev/ttyUSB0 -b 460800 –earlier than 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’. Guarantee you will have handed the USB port to WSL utilizing usbip and allowed all customers to make use of it with chmod (see the primary half for particulars).

With this challenge, you possibly can import the microlite library, which is strictly the one you want – TensorFlow Lite for Microcontrollers. If you wish to prolong this challenge with your personal libraries, you possibly can add these to the cloned supply code and construct it with the next instructions:

git submodule init

git submodule replace --recursive

cd micropython

git submodule replace --init lib/axtls

git submodule replace --init lib/berkeley-db-1.xx

cd ..

supply ./micropython/instruments/ci.sh && ci_esp32_setup_helper v4.3.1

supply ./esp-idf/export.sh #as you possibly can see, esp-idf is already included 

pip3 set up Pillow

pip3 set up Wave

rm -rf ./micropython-modules/microlite/tflm

cd ./tensorflow

../micropython-modules/microlite/prepare-tflm-esp.sh

cd ../micropython

make -C mpy-cross V=1 clear all

cd ../boards/esp32/MICROLITE_SPIRAM_16M

rm -rf construct

idf.py clear construct

Your binaries are situated below the boards/esp32/MICROLITE_SPIRAM_16M/construct listing.

This manner, you possibly can add extra libraries or your personal program to the picture, however you continue to want to make use of the interactive console to run it.

Abstract

This three-part workshop aimed to make the most of M5Stack Core2 for AWS controller with AWS IoT connection and ML included. Let’s attempt to sum up all attainable methods to try this.

Language Administration technique Professionals Cons
Micropython UiFlow Python Low studying curve/simple UI OTA updates No Tensorflow obtainable Not sufficient for complicated use instances
VS Code with vscode-m5stack-mpi plugin Python Full IDE (Visible Studio Code) No Tensorflow obtainable
Tensorflow Micropython Examples Micropython with TensorFlow Lite for Microcontrollers Functionality to construct the challenge with extra libraries or customized code included Necessity to run the code from interactive Python console.
C/C++ VS Code with PlatformIO and FreeRTOS All libraries obtainable Advanced code (C/C++) Advanced configuration
Arduino IDE All libraries obtainable Straightforward and highly effective sufficient IDE Arduino management loop limitation
ESP-IDF Small answer, with out even a devoted IDE (plugins for CS Code or Eclipse obtainable) Advanced dependency Administration

As you possibly can see, we’ve tried numerous approaches and found their benefits and downsides. Now, you possibly can resolve if you want full management and use pure C/C++, or perhaps you favor way more pleasant Python. You’ll be able to select which IDE or a minimum of plugin you’d like to make use of, and whether or not you need to make the most of OTA to replace a complete firmware or solely to switch knowledge between your units and the cloud.