
On this sequence of articles, we contemplate the professionals and cons of various methods to handle an M5Stack controller to construct a machine studying utility related to the AWS IoT cloud. The sequence includes 6 manuals with totally different languages, instruments, and methods.
Introduction to the M5Stack controller
M5Stack Core2 ESP32 IoT Growth Equipment for AWS IoT EduKit is a small controller based mostly on ESP32 twin core ESP32-D0WDQ6-V3 processors with 8MB PSRAM and 16MB flash on board, devoted to the AWS IoT EduKit program. It’s small, not-so-powerful, however it has a small touchscreen, card reader, microphone, speaker, vibration engine, accelerometer, gyroscope, 2.4Ghz Wi-Fi, and a few colourful LEDs, so it’s good for use as an IoT system for training.

Sadly, as a consequence of ESP32 structure, it additionally has a number of important limitations. The controller can’t be used as an IoT edge system for AWS Greengrass as a result of there isn’t any AWS Greengrass Core library for ESP32 to date. Apart from that, it can also’t deal with TinyML out of the field – all well-known studying libraries (like Tensorflow Lite or scikit-learn) require not less than ARM processors to function. The very best you are able to do is to make use of Tensorflow Lite for Microcontrollers in C/C++ code or write your individual ML logic in Micropython. If you wish to use M5Stack to construct a “study on IoT edge” movement, it’s worthwhile to have not less than an ARM-based Raspberry Pi (or comparable) in your IoT community and use M5Stack to gather and pre-process knowledge solely.
Easy methods to work with M5Stack? The system is completely open, and you may flash the inner reminiscence with all the things you need. In fact, it’s worthwhile to cross-compile your software program for ESP32, and it’s sensible so as to add some M5Stack-related modules to deal with not less than the display screen or some LED to know if it really works. In accordance with the documentation, there are a number of really useful methods to handle your M5Stack system, so let’s focus on these prospects and discover the professionals and cons.
UiFlow
The simple method first. UiFlow is a browser-based instrument with a user-friendly block editor that makes use of OTA updates to ship Micropython code to the system. There’s additionally a desktop instrument should you favor to make use of USB cable greater than OTA updates, however it’s already deprecated.
Preparation
To make use of UiFlow, it’s worthwhile to set up correct firmware to M5Stack first. You’ll want to set up a CP210x_VCP driver devoted to your OS in your pc (Home windows, MacOS, and Linux variations can be found) after which obtain and begin a burning instrument (small, moveable app). Yow will discover all of these on the official web page https://docs.m5stack.com/en/quick_start/core2_for_aws/uiflow.
Now you’ll be able to join M5Stack controller to a USB port and confirm the connection. To try this, open the burning instrument and open the “COM Monitor” from the menu within the prime proper nook. If the system is related, you must see the COM port hooked up to it by your OS. In my case, it’s COM3. The identical port ought to be chosen by default within the drop-down menu within the prime left nook.

If one thing is flawed, confirm the driving force set up, verify your antivirus guidelines, and ensure the system is turned on. Typically, if you’ve made some mistake, the system seems like a bricked one (no LED, no display screen), and it’s onerous to acknowledge whether or not it’s really enabled or not.
Utilizing the instrument, you must set up software program ready for Core2 solely. It’s a very good second to mess around and attempt to obtain and set up, e.g., Core2_Tools (to check sensors) or some good outdated recreation ready by the group. Bear in mind that a lot of the firmwares accessible within the burning instrument are written for a fundamental M5Stack single-core controller, so that you’ll be capable to burn it to the system, however it received’t begin in any respect. Don’t fear; in such a case, you’ll be able to all the time burn one other one. The official documentation suggests lowering the bitrate in case of burning errors, however I by no means had to do this.
Please notice the debugging messages throughout burning. The instrument makes use of esptool.py to burn firmware to the system, so generally, you are able to do the identical out of your console if you’d like.
Utilizing UiFlow
Now, let’s burn UIFlow_Core2 firmware.
All it’s worthwhile to do is to select the proper firmware, obtain it and burn. The burning instrument will ask you in your Wi-Fi credentials. Bear in mind, the system doesn’t assist 5Ghz frequency, so it’s worthwhile to have a 2,4Ghz or 2,4/5Ghz combined community to maneuver on.
Now, it’s time to configure the firmware. Underneath the “configuration” button within the burning instrument, you’ll be able to decide certainly one of three modes: App, USB, or Web. Within the App mode, you can begin some utility from the inner reminiscence or configure USB or Web mode straight utilizing the controller’s display screen. Within the USB mode, you’ll be able to add and begin an utility out of your pc (verify Micropython and FreeRTOS sections). Web mode is the way in which to make use of the web-based UiFlow.
After beginning the controller within the Web mode, you must see three vital items of data.

On the prime bar, you’ll be able to see the Wi-Fi icon and cloud synchronization icon. Each ought to be inexperienced. If the primary one is crimson, it’s worthwhile to verify your Wi-Fi configuration and just remember to use a 2,4Ghz community and that the password is appropriate. In some uncommon instances, the system could also be incompatible along with your native router. It shouldn’t occur however effectively… It occurred to me as soon as, and I had to make use of one other router to proceed. If the cloud icon is crimson, possibly your native community isn’t related to the Web, or UiFlow servers are briefly down.
Within the heart a part of the display screen, you’ll be able to see the API KEY. You should utilize this identifier in your browser to replace the system over-the-air. If it’s worthwhile to rotate your API KEY as a result of e.g., you may have compromised the important thing by posting a photograph of your system with it within the weblog article, and now everybody can replace your system OTA, you need to use the “Erase” button within the burning instrument and burn the UiFlow_Core2 picture once more.
Now, you’ll be able to open https://flow.m5stack.com in a browser and begin coding. Throughout the first begin, the applying ought to ask you to offer the API KEY and decide your M5Stack mannequin.

You may modify these settings later utilizing a small button within the backside left nook.
The online instrument is sort of {powerful}.

On the heart of the highest bar, you’ll be able to select whether or not you wish to use the block editor or should you favor to write down the Micropython code by yourself. Within the block mode, utilizing the left bar, you’ll be able to handle further modules in case you have some. Within the code Python mode, you can too add some small information to the system. You may run your code on the backside proper nook or open a Python console (utilizing a COM port, not the Web connection).
The highest left part is the WYSIWYG editor – you’ll be able to drag&drop UI elements to your system’s display screen.
Instance
Let’s use UiFlow to implement some utility. The intention is to attach the controller to the AWS IoT Core. The applying will work in each methods – it can present on the system’s display screen texts despatched from the cloud through MQTT and ship again the system place (from the built-in gyroscope). It’s simply an instance of both-way communication you need to use for extra advanced duties.
Let’s begin from the AWS facet. You should utilize a free tier account for this utility should you don’t have a correct account but.
Open the AWS console after which the IoT Core service. From the left menu, decide Handle -> All gadgets -> Issues.

Create a brand new, single Factor (it’s sufficient for our use case to date). My Factor title is “UiFlow_test”. You should utilize no matter you need – we’re going to make use of the title at a later level. You may go away the remainder of the Factor configuration with the default settings (no shadow, generate certificates, and so forth.). On the insurance policies display screen, create a brand new coverage and copy-paste the next coverage JSON definition:
"Model": "2012-10-17",
"Assertion": [
"Effect": "Allow",
"Action": "iot:*",
"Resource": "*"
]
Utilizing allow-all coverage is redundant for our use-case, so you’ll be able to attempt to slim your permissions if wanted. You’ll want to obtain certificates through the Factor creation wizard, as a result of it received’t be attainable later.

After downloading, rename the certificates and personal key information to one thing shorter, as a result of UiFlow doesn’t like lengthy file names. It’s possible you’ll obtain the Root CA certificates too. These will likely be helpful at a later stage.
OK, let’s create some code.
You’ll want all blocks from “IoTCloud -> AWS” part from the Blockly left menu, and likewise:
- “Occasion -> Loop”,
- “Timer -> Wait (s)”,
- “JSON -> dumps”,
- “Map -> create map”,
- 3x “Textual content -> empty”
- “Hardwares -> IMU -> Get X Gyr”,
- “Hardwares -> IMU -> Get Y Gyr”,
- “Hardwares -> IMU -> Get Z Gyr”,
- “UI -> Label -> Label present”.
The final one will seem within the menu after drag&drop’ing a label element from the very left menu to the controller preview display screen.

Now, let’s do the puzzle and fill within the obligatory knowledge: Factor title, host URL, and matter names. I’ve consumer matter names: IoT_to_AWS and AWS_to_IoT. The endpoint URL is seen on the setting web page within the AWS IoT console.

Please notice it’s worthwhile to use the small “+” button within the AWS initialization block to add the non-public key and certificates information earlier than you’ll be able to decide them for the “keyFile” and “certFile” fields, and you may add message fragments by clicking a much bigger “+” button within the “create map” element. Your block editor ought to seem like that:

The generated code is as follows:
from m5stack import *
from m5stack_ui import *
from uiflow import *
from IoTcloud.AWS import AWS
import json
import time
import imu
display screen = M5Screen()
display screen.clean_screen()
display screen.set_screen_bg_color(0xFFFFFF)
imu0 = imu.IMU()
label0 = M5Label('label0', x=134, y=109, coloration=0x000, font=FONT_MONT_14, father or mother=None)
def fun_AWS_to_IoT_(topic_data):
# international params
label0.set_text(str(topic_data))
go
aws = AWS(things_name="UiFlow_test", host=XXX-ats.iot.eu-west-1.amazonaws.com, port=8883, keepalive=60, cert_file_path="/flash/res/certificates.pem.crt", private_key_path="/flash/res/non-public.pem.key")
aws.subscribe(str('AWS_to_IoT'), fun_AWS_to_IoT_)
aws.begin()
whereas True:
aws.publish(str('IoT_to_AWS'),str((json.dumps(('x':(imu0.gyro[0]),'y':(imu0.gyro[1]),'z':(imu0.gyro[2]))))))
wait(5)
wait_ms(2)
OK, let’s run the applying and see what occurs. If the controller doesn’t reset after your browser’s “resetting” message, it’s worthwhile to restart it manually and check out once more.
If you happen to see the “label0” message on the controller’s display screen, it’s time to open the AWS console and verify the communication.
Go to the MQTT take a look at consumer within the left menu of the AWS IoT service. Now you’ll be able to subscribe to the IoT_to_AWS matter to gather the gyroscope knowledge from the system, and you may publish one thing to the AWS_to_IoT matter to vary the label on the system’s display screen.
It’s all about your creativeness; what do you wish to do with the IoT system related to the AWS cloud?
Micropython
We’ve realized the primary steps with our M5Stack controller within the earlier handbook. This time, let’s go away the UI and use the nice outdated IDE and console solely.
To make use of this method it’s worthwhile to burn the UIFlow_Core2 firmware in the identical method as when configuring UiFlow. Wait a second… We already used this firmware and even wrote a Micropython code, so why does it deserve one other part? This time we’re not going to make use of a elaborate, block, web-based OTA instrument however a very good outdated Visible Studio Code.
Let’s begin with the controller. All it’s worthwhile to do is to burn the UIFlow_Core2 and configure it in USB mode.
Now, let’s set up VSCode and join the controller. You may set up the IDE straight from the official web page https://code.visualstudio.com/. If you happen to’re engaged on a Home windows machine with WSL, the IDE will likely be accessible in each (Home windows and Linux) environments. As we might have some additional libraries, simpler to handle in Linux, let’s use VSCode from WSL. Let’s open WSL and run ‘code’. The latest IDE will likely be downloaded (in case your model is out of date) and run.
Thus far, so good, however there is a crucial impediment when utilizing WSL. First, you want WSL2, so please observe the official handbook to improve: https://learn.microsoft.com/en-us/windows/wsl/install.
The second concern is USB forwarding. By default, USB ports should not accessible within the WSL setting. To vary it, it’s worthwhile to use usbip. This instrument will redirect all USB site visitors through IP protocol to your Linux setting.
In WSL, it’s worthwhile to set up two packages and set the default path for usbip.
sudo apt set up linux-tools-5.15.0-47-generic hwdata
sudo update-alternatives --install /usr/native/bin/usbip usbip /usr/lib/linux-tools/5.15.0-47-generic/usbip 20
Now, please restart your WSL – shut the terminal, name wsl –shutdown from the Home windows command line and open the WSL terminal once more. Please notice, that the latest kernel model for me is 5.15.0-47 however you’ll be able to have one other one put in in your machine. Furthermore, it’s worthwhile to set up linux-tools-xxx-generic. You’ll see latest packages for not-generic utilization accessible, however it’s not you’re searching for.
Now, utilizing PowerShell console it’s worthwhile to set up usbip and fasten the proper port. It’s type of unusual, however you’ll be able to set up the instrument and redirect the port utilizing commonplace consumer privileges, however to detach the already hooked up port, it’s worthwhile to open PowerShell as Administrator. Subsequently, it’s simpler to begin the PowerShell console as Administrator and name the next instructions.
winget set up usbipd
usbipd record
The output ought to seem like that:
PS C:WINDOWSsystem32> usbipd record
Linked:
BUSID VID:PID DEVICE STATE
1-1 10c4:ea60 Silicon Labs CP210x USB to UART Bridge (COM3) Not shared
1-2 046d:c52b Logitech USB Enter Gadget, USB Enter Gadget Not shared
1-10 0a5c:5834 Dell ControlVault w/ Fingerprint Contact Sensor, Microsoft... Not shared
1-11 0c45:6717 Built-in Webcam Not shared
1-14 8087:0aaa Intel(R) Wi-fi Bluetooth(R) Not shared
Endured:
GUID DEVICE
usbipd: warning: Unknown USB filter 'edevmon' could also be incompatible with this software program; 'bind --force' could also be required.
The warning within the final line is typical and there’s nothing to be apprehensive about. What we’d like is the busid of the CP210x system. In my case, the id is 1-1.
To connect and detach the controller to and from WSL setting, use following instructions within the PowerShell console.
usbipd wsl connect –busid 1-1
usbipd unbind -b 1-1
If the system is hooked up, open your WSL console and run ‘lsusb’ for verification. The output ought to incorporates one thing much like:
Bus 001 Gadget 002: ID 10c4:ea60 Silicon Labs CP210x UART Bridge
The system is related, however to date, it’s accessible for root solely.
You’ll want to name ‘sudo chmod 666 /dev/ttyUSB0’ in your WSL to permit all customers (and the IDE) to make use of it. If it doesn’t work, name ‘ls /dev/ttyUSB*’ to verify the right title of your USB connector.
Now you’ll be able to open VSCode (with ‘code’ command in WSL) and set up the official vscode-m5stack-mpy plugin. Make certain, you may have additionally the ‘WSL’ extension put in (it ought to be there by default).

When your IDE is ready up, you must see a brand new button “Add M5Stack” within the backside menu.

Hit it and decide the proper /dev/ttyUSB* system from the record. Now, within the Explorer view within the IDE, there’s a new part known as “M5Stack system”. Right here you’ll be able to straight handle the system reminiscence utilizing the Micropython language.
You should utilize the code from the UiFlow description for testing functions. Simply paste it to the primary.py file on the controller and put the certificates and the non-public key information within the correct listing (in line with the trail in your code).
To run the code, click on “Run in M5Stack” button within the prime proper nook.

Abstract
UiFlow is a user-friendly web-based IDE with a block editor, WYSIWYG display screen designer, and easy code administration. It’s {powerful}; nonetheless, it’s additionally restricted to superior utilization. You may also use VS Code as a correct, fashionable IDE to create your utility. You may mess around with it, blink some LED in your controller or gather some additional knowledge from exterior modules (in case you have some). You’ll want to bear in mind that Micropython just isn’t the total Python however nonetheless preserves a developer-friendly syntax and a few frequent libraries. Nevertheless, however, there isn’t any approach to set up any additional library to your code, so we are able to’t use TensorFlow with Micropython this manner. We’ll attempt to tackle this concern within the subsequent chapter of this text.