June 23, 2024

On this collection of articles, we contemplate the professionals and cons of various methods to handle an M5Stack controller to construct a machine studying utility linked to the AWS IoT cloud. The collection includes 6 manuals with totally different languages, instruments, and strategies.

Introduction to the M5Stack controller

M5Stack Core2 ESP32 IoT Growth Package 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, but it surely 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 gadget for training.

M5Stack controller
Picture from https://store.m5stack.com/collections/m5-controllers/merchandise/m5stack-core2-esp32-iot-development-kit-for-aws-iot-edukit 

Sadly, resulting from ESP32 structure, it additionally has a number of vital limitations. The controller can’t be used as an IoT edge gadget for AWS Greengrass as a result of there isn’t a AWS Greengrass Core library for ESP32 to date. In addition to 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 “be taught on IoT edge” stream, it’s essential 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.

work with M5Stack? The gadget is completely open, and you may flash the inner reminiscence with all the things you need. After all, it’s essential cross-compile your software program for ESP32, and it’s smart 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 just a few really helpful methods to handle your M5Stack gadget, so let’s focus on these prospects and discover the professionals and cons.

UiFlow

The straightforward manner first. UiFlow is a browser-based device with a user-friendly block editor that makes use of OTA updates to ship Micropython code to the gadget. There’s additionally a desktop device when you choose to make use of USB cable greater than OTA updates, but it surely’s already deprecated.

Preparation

To make use of UiFlow, it’s essential set up correct firmware to M5Stack first. You want to set up a CP210x_VCP driver devoted to your OS in your laptop (Home windows, MacOS, and Linux variations can be found) after which obtain and begin a burning device (small, transportable app). You could find 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 device and open the “COM Monitor” from the menu within the prime proper nook. If the gadget is linked, it is best to see the COM port connected to it by your OS. In my case, it’s COM3. The identical port must be chosen by default within the drop-down menu within the prime left nook. 

connect M5Stack controller to a USB port

If one thing is improper, confirm the motive force set up, examine your antivirus guidelines, and ensure the gadget is turned on. Typically, while you’ve made some mistake, the gadget appears to be like like a bricked one (no LED, no display screen), and it’s arduous to acknowledge whether or not it’s really enabled or not.

Utilizing the device, it is best to set up software program ready for Core2 solely. It’s second to mess around and attempt to obtain and set up, e.g., Core2_Tools (to check sensors) or some good outdated sport ready by the neighborhood. Remember that many of the firmwares obtainable within the burning device are written for a fundamental M5Stack single-core controller, so that you’ll be capable of burn it to the gadget, but it surely gained’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 decreasing the bitrate in case of burning errors, however I by no means had to do this.

Please be aware the debugging messages throughout burning. The device makes use of esptool.py to burn firmware to the gadget, so normally, 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 essential do is to choose the right firmware, obtain it and burn. The burning device will ask you to your Wi-Fi credentials. Bear in mind, the gadget doesn’t assist 5Ghz frequency, so it’s essential have a 2,4Ghz or 2,4/5Ghz combined community to maneuver on.

Now, it’s time to configure the firmware. Beneath the “configuration” button within the burning device, you’ll be able to choose one in all 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 laptop (examine Micropython and FreeRTOS sections). Web mode is the best way to make use of the web-based UiFlow.

After beginning the controller within the Web mode, it is best to see three necessary items of data.

On the prime bar, you’ll be able to see the Wi-Fi icon and cloud synchronization icon. Each must be inexperienced. If the primary one is pink, it’s essential examine your Wi-Fi configuration and just remember to use a 2,4Ghz community and that the password is appropriate. In some uncommon circumstances, the gadget could also be incompatible along with your native router. It shouldn’t occur however properly… It occurred to me as soon as, and I had to make use of one other router to proceed. If the cloud icon is pink, possibly your native community isn’t linked to the Web, or UiFlow servers are quickly 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 gadget over-the-air. If it’s essential rotate your API KEY as a result of e.g., you have got compromised the important thing by posting a photograph of your gadget with it within the weblog article, and now everybody can replace your gadget OTA, you need to use the “Erase” button within the burning device 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. In the course of the first begin, the appliance ought to ask you to supply the API KEY and choose your M5Stack mannequin.

UIFlow

You may alter these settings later utilizing a small button within the backside left nook.

The net device is kind 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 when you choose to put in writing the Micropython code by yourself. Within the block mode, utilizing the left bar, you’ll be able to handle extra modules you probably have some. Within the code Python mode, you may as well add some small information to the gadget. 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 gadget’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 appliance will work in each methods – it’s going to present on the gadget’s display screen texts despatched from the cloud through MQTT and ship again the gadget 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 aspect. You should utilize a free tier account for this utility when you don’t have a correct account but.

Open the AWS console after which the IoT Core service. From the left menu, choose Handle -> All units -> Issues.

the AWS console

Create a brand new, single Factor (it’s sufficient for our use case to date). My Factor identify is “UiFlow_test”. You should utilize no matter you need – we’re going to make use of the identify at a later level. You may depart the remainder of the Factor configuration with the default settings (no shadow, generate certificates, and so on.). 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 slender your permissions if wanted. You want to obtain certificates throughout the Factor creation wizard, as a result of it gained’t be attainable later.

certificates and keys

After downloading, rename the certificates and personal key information to one thing shorter, as a result of UiFlow doesn’t like lengthy file names. Chances are 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 in addition:

  • “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 part from the very left menu to the controller preview display screen.

drag and drop menu

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

AWS IoT console

Please be aware it’s essential use the small “+” button within the AWS initialization block to add the personal key and certificates information earlier than you’ll be able to choose them for the “keyFile” and “certFile” fields, and you may add message fragments by clicking an even bigger “+” button within the “create map” part. Your block editor ought to appear 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, guardian=None)

def fun_AWS_to_IoT_(topic_data):
  # world params
  label0.set_text(str(topic_data))
  move

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/personal.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 appliance and see what occurs. If the controller doesn’t reset after your browser’s “resetting” message, it’s essential restart it manually and take a look at 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 examine the communication.

Go to the MQTT check 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 gadget, and you may publish one thing to the AWS_to_IoT matter to vary the label on the gadget’s display screen.

It’s all about your creativeness; what do you wish to do with the IoT gadget linked to the AWS cloud?

Micropython

We’ve discovered the primary steps with our M5Stack controller within the earlier handbook. This time, let’s depart the UI and use the nice outdated IDE and console solely.

To make use of this strategy it’s essential burn the UIFlow_Core2 firmware in the identical manner 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 device however outdated Visible Studio Code.

Let’s begin with the controller. All it’s essential 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 obtainable 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 most recent IDE will likely be downloaded (in case your model is out of date) and run.

To this point, 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 challenge is USB forwarding. By default, USB ports aren’t obtainable within the WSL surroundings. To alter it, it’s essential use usbip. This device will redirect all USB visitors through IP protocol to your Linux surroundings.

In WSL, it’s essential 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 be aware, that the most recent 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 essential set up linux-tools-xxx-generic. You’ll see latest packages for not-generic utilization obtainable, but it surely’s not you’re searching for.

Now, utilizing PowerShell console it’s essential set up usbip and fasten the right port. It’s type of unusual, however you’ll be able to set up the device and redirect the port utilizing commonplace person privileges, however to detach the already connected port, it’s essential open PowerShell as Administrator. Due to this fact, it’s simpler to start out the PowerShell console as Administrator and name the next instructions.

winget set up usbipd
usbipd listing

The output ought to appear like that:

PS C:WINDOWSsystem32> usbipd listing
Related:
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 System, USB Enter System                   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 may be nothing to be fearful about. What we want is the busid of the CP210x gadget. In my case, the id is 1-1.

To connect and detach the controller to and from WSL surroundings, use following instructions within the PowerShell console.

usbipd wsl connect –busid 1-1
usbipd unbind -b 1-1

If the gadget is connected, open your WSL console and run ‘lsusb’ for verification. The output ought to incorporates one thing just like:

Bus 001 System 002: ID 10c4:ea60 Silicon Labs CP210x UART Bridge

The gadget is linked, however to date, it’s obtainable for root solely.

You 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 examine the right identify 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. Be certain, you have got additionally the ‘WSL’ extension put in (it must be there by default).

M5Stack controller

When your IDE is about up, it is best to see a brand new button “Add M5Stack” within the backside menu.

M5Stack controller

Hit it and choose the right /dev/ttyUSB* gadget from the listing. Now, within the Explorer view within the IDE, there’s a new part known as “M5Stack gadget”. Right here you’ll be able to straight handle the gadget reminiscence utilizing the Micropython language.

You should utilize the code from the UiFlow description for testing functions. Simply paste it to the principle.py file on the controller and put the certificates and the personal key information within the correct listing (in keeping with the trail in your code).

To run the code, click on “Run in M5Stack” button within the prime proper nook.

run in M5Stack controller

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. It’s also possible to 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 accumulate some additional knowledge from exterior modules (you probably have some). You want to remember that Micropython will not be the total Python however nonetheless preserves a developer-friendly syntax and a few widespread libraries. Nevertheless, alternatively, there isn’t a method to set up any additional library to your code, so we will’t use TensorFlow with Micropython this fashion. We’ll attempt to handle this challenge within the subsequent chapter of this text.