April 23, 2024

On this sequence of articles, we take into account the professionals and cons of various methods to handle an M5Stack controller to construct a machine studying software 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 excellent for use as an IoT machine for schooling.

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

Sadly, on account of ESP32 structure, it additionally has a number of vital limitations. The controller can’t be used as an IoT edge machine for AWS Greengrass as a result of there is no such thing as a AWS Greengrass Core library for ESP32 to this point. 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 at the least 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 personal ML logic in Micropython. If you wish to use M5Stack to construct a “study on IoT edge” stream, it’s essential have at the least an ARM-based Raspberry Pi (or comparable) in your IoT community and use M5Stack to gather and pre-process information solely.

work with M5Stack? The machine is totally open, and you may flash the inner reminiscence with all the pieces you need. In fact, it’s essential cross-compile your software program for ESP32, and it’s clever so as to add some M5Stack-related modules to deal with at the least the display or some LED to know if it really works. In response to the documentation, there are just a few really helpful methods to handle your M5Stack machine, so let’s talk about these potentialities and discover the professionals and cons.

UiFlow

The straightforward means 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 machine. There may be additionally a desktop instrument when you want to make use of USB cable greater than OTA updates, however it’s already deprecated.

Preparation

To make use of UiFlow, it’s essential set up correct firmware to M5Stack first. You’ll 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 instrument (small, moveable 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 may 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 machine is related, it is best to see the COM port hooked up 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, test your antivirus guidelines, and ensure the machine is turned on. Generally, while you’ve made some mistake, the machine seems like a bricked one (no LED, no display), and it’s onerous to acknowledge whether or not it’s really enabled or not.

Utilizing the instrument, it is best to set up software program ready for Core2 solely. It’s a great 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. Remember that a lot of the firmwares obtainable within the burning instrument are written for a fundamental M5Stack single-core controller, so that you’ll have the ability to burn it to the machine, however it gained’t begin in any respect. Don’t fear; in such a case, you may at all times 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 observe the debugging messages throughout burning. The instrument makes use of esptool.py to burn firmware to the machine, so basically, you are able to do the identical out of your console if you need.

Utilizing UiFlow

Now, let’s burn UIFlow_Core2 firmware.

All it’s essential do is to select the proper firmware, obtain it and burn. The burning instrument will ask you to your Wi-Fi credentials. Keep in mind, the machine 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. Underneath the “configuration” button within the burning instrument, you may choose considered one of three modes: App, USB, or Web. Within the App mode, you can begin some software from the inner reminiscence or configure USB or Web mode immediately utilizing the controller’s display. Within the USB mode, you may add and begin an software out of your laptop (test 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 knowledge.

On the prime bar, you may see the Wi-Fi icon and cloud synchronization icon. Each must be inexperienced. If the primary one is purple, it’s essential test your Wi-Fi configuration and just remember to use a 2,4Ghz community and that the password is right. In some uncommon circumstances, the machine 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 purple, perhaps your native community isn’t related to the Web, or UiFlow servers are quickly down.

Within the middle a part of the display, you may see the API KEY. You need to use this identifier in your browser to replace the machine over-the-air. If it’s essential rotate your API KEY as a result of e.g., you will have compromised the important thing by posting a photograph of your machine with it within the weblog article, and now everybody can replace your machine OTA, you should use the “Erase” button within the burning instrument and burn the UiFlow_Core2 picture once more.

Now, you may open https://flow.m5stack.com in a browser and begin coding. Throughout the first begin, the applying ought to ask you to supply the API KEY and choose your M5Stack mannequin.

UIFlow

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

The net instrument is sort of {powerful}.

On the middle of the highest bar, you may select whether or not you wish to use the block editor or when you want to jot down the Micropython code by yourself. Within the block mode, utilizing the left bar, you may handle further modules if in case you have some. Within the code Python mode, you may also add some small information to the machine. You possibly can 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 may drag&drop UI elements to your machine’s display.

Instance

Let’s use UiFlow to implement some software. The goal is to attach the controller to the AWS IoT Core. The appliance will work in each methods – it should present on the machine’s display texts despatched from the cloud through MQTT and ship again the machine place (from the built-in gyroscope). It’s simply an instance of both-way communication you should use for extra complicated duties.

Let’s begin from the AWS aspect. You need to use a free tier account for this software 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 gadgets -> Issues.

the AWS console

Create a brand new, single Factor (it’s sufficient for our use case to this point). My Factor identify is “UiFlow_test”. You need to use no matter you need – we’re going to make use of the identify at a later level. You possibly can go away the remainder of the Factor configuration with the default settings (no shadow, generate certificates, and many others.). On the insurance policies display, 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 may attempt to slim your permissions if wanted. You’ll want to obtain certificates in the course of 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 shall 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.

drag and drop menu

Now, let’s do the puzzle and fill within the needed information: Factor identify, 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.

AWS IoT console

Please observe it’s essential use the small “+” button within the AWS initialization block to add the personal key and certificates information earlier than you may 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 to be 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 = M5Screen()
display.clean_screen()
display.set_screen_bg_color(0xFFFFFF)

imu0 = imu.IMU()

label0 = M5Label('label0', x=134, y=109, shade=0x000, font=FONT_MONT_14, mother or father=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 applying and see what occurs. If the controller doesn’t reset after your browser’s “resetting” message, it’s essential restart it manually and check out once more.

In the event you see the “label0” message on the controller’s display, it’s time to open the AWS console and test the communication.

Go to the MQTT take a look at shopper within the left menu of the AWS IoT service. Now you may subscribe to the IoT_to_AWS matter to gather the gyroscope information from the machine, and you may publish one thing to the AWS_to_IoT matter to alter the label on the machine’s display.

It’s all about your creativeness; what do you wish to do with the IoT machine 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 essential burn the UIFlow_Core2 firmware in the identical means 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 flowery, block, web-based OTA instrument however a great 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 possibly can set up the IDE immediately from the official web page https://code.visualstudio.com/. In the event you’re engaged on a Home windows machine with WSL, the IDE shall 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 shall be downloaded (in case your model is out of date) and run.

Thus far, so good, however there is a vital 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 situation is USB forwarding. By default, USB ports aren’t obtainable within the WSL setting. To vary it, it’s essential use usbip. This instrument will redirect all USB visitors through IP protocol to your Linux setting.

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 observe, that the latest kernel model for me is 5.15.0-47 however you may 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, however it’s not you’re on the lookout for.

Now, utilizing PowerShell console it’s essential set up usbip and connect the proper port. It’s sort of unusual, however you may set up the instrument and redirect the port utilizing normal consumer privileges, however to detach the already hooked up 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 to be 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

Continued:
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 nervous about. What we’d like is the busid of the CP210x machine. 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 machine is hooked up, open your WSL console and run ‘lsusb’ for verification. The output ought to accommodates one thing just like:

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

The machine is related, however to this point, it’s obtainable 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 test the right identify of your USB connector.

Now you may open VSCode (with ‘code’ command in WSL) and set up the official vscode-m5stack-mpy plugin. Ensure, you will have additionally the ‘WSL’ extension put in (it must be there by default).

M5Stack controller

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

M5Stack controller

Hit it and choose the proper /dev/ttyUSB* machine from the listing. Now, within the Explorer view within the IDE, there’s a new part referred to as “M5Stack machine”. Right here you may immediately handle the machine reminiscence utilizing the Micropython language.

You need to use 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 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 designer, and easy code administration. It’s {powerful}; nonetheless, it’s additionally restricted to superior utilization. You can even use VS Code as a correct, well-liked IDE to create your software. You possibly can mess around with it, blink some LED in your controller or gather some additional information from exterior modules (if in case you have some). You’ll want to remember that Micropython just isn’t the complete Python however nonetheless preserves a developer-friendly syntax and a few widespread libraries. Nevertheless, alternatively, there is no such thing as a strategy to set up any additional library to your code, so we will’t use TensorFlow with Micropython this fashion. We’ll attempt to tackle this situation within the subsequent chapter of this text.