The way to Handle an M5Stack Core2 for AWS

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 related to the AWS IoT cloud. The collection includes 6 manuals with completely different languages, instruments, and methods.

Introduction to the M5Stack controller

M5Stack Core2 ESP32 IoT Improvement Package for AWS IoT EduKit is a small controller primarily based 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, resulting from 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. 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 a minimum of 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” move, you want to have a minimum of an ARM-based Raspberry Pi (or comparable) in your IoT community and use M5Stack to gather and pre-process information solely.

The way to work with M5Stack? The machine is totally open, and you may flash the inner reminiscence with every part you need. In fact, you want to cross-compile your software program for ESP32, and it’s clever so as to add some M5Stack-related modules to deal with a minimum of the display or some LED to know if it really works. In response to the documentation, there are a couple of advisable methods to handle your M5Stack machine, so let’s talk about these potentialities and discover the professionals and cons.

UiFlow

The straightforward method first. UiFlow is a browser-based instrument with a user-friendly blocky editor that makes use of OTA updates to ship Micropython code to the machine. There’s additionally a desktop instrument in the event you desire to make use of USB cable greater than OTA updates, however it’s already deprecated.

Preparation

To make use of UiFlow, you want to set up correct firmware to M5Stack first. You 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). 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 do this, open the burning instrument and open the “COM Monitor” from the menu within the prime proper nook. If the machine is related, you must see the COM port connected to it by your OS. In my case, it’s COM3. The identical port needs to 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 driving force set up, examine your antivirus guidelines, and ensure the machine is turned on. Typically, once you’ve made some mistake, the machine seems like a bricked one (no LED, no display), and it’s arduous 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 superb second to mess around and attempt to obtain and set up, e.g., Core2_Tools (to check sensors) or some good previous sport ready by the group. Bear in mind that a lot of the firmwares obtainable within the burning instrument are written for a primary M5Stack single-core controller, so that you’ll have the ability to burn it to the machine, however it received’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 word the debugging messages throughout burning. The instrument makes use of esptool.py to burn firmware to the machine, so usually, you are able to do the identical out of your console if you would like.

Utilizing UiFlow

Now, let’s burn UIFlow_Core2 firmware.

All you want to do is to choose the right firmware, obtain it and burn. The burning instrument will ask you to your Wi-Fi credentials. Bear in mind, the machine doesn’t assist 5Ghz frequency, so you want to have a 2,4Ghz or 2,4/5Ghz combined community to maneuver on.

Now, it’s time to configure the firmware. Below the “configuration” button within the burning instrument, you may decide considered 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 immediately utilizing the controller’s display. Within the USB mode, you may add and begin an utility out of your pc (examine 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 essential items of knowledge.

On the prime bar, you may see the Wi-Fi icon and cloud synchronization icon. Each needs to be inexperienced. If the primary one is crimson, you want to examine your Wi-Fi configuration and just be sure you use a 2,4Ghz community and that the password is appropriate. In some uncommon instances, the machine could also be incompatible together with your native router. It shouldn’t occur however nicely… 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, 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 you want to rotate your API KEY as a result of e.g., you’ve got 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 need to 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. In the course of the first begin, the applying ought to ask you to offer the API KEY and decide your M5Stack mannequin.

UIFlow

You’ll be able to modify these settings later utilizing a small button within the backside left nook.

The net instrument is sort of {powerful}.

write the micropython code on your own

On the middle of the highest bar, you may select whether or not you need to use the blocky editor or in the event you desire to write down the Micropython code by yourself. Within the blocky mode, utilizing the left bar, you may handle further modules in case you have some. Within the code Python mode, you too can add some small information to the machine. You’ll be able to 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 parts to your machine’s display.

Instance

Let’s use UiFlow to implement some utility. The purpose is to attach the controller to the AWS IoT Core. The appliance will work in each methods – it can 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 need to use for extra complicated duties.

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

the AWS console

Create a brand new, single Factor (it’s sufficient for our use case to this point). My Factor title is “UiFlow_test”. You need to use no matter you need – we’re going to make use of the title at a later level. You’ll be able to 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 want to obtain certificates in the course of the Factor creation wizard, as a result of it received’t be doable 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. You could obtain the Root CA certificates too. These will probably be helpful at a later stage.

OK, let’s create some code.

You’ll want all blocks from “IoTCloud -> AWS” part from the Blocky 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 element from the very left menu to the controller preview display.

drag and drop menu

Now, let’s do the puzzle and fill within the crucial information: Factor title, 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 word you want to use the small “+” button within the AWS initialization block to add the non-public key and certificates information earlier than you may 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 blocky editor ought to seem like that:

AWS initialization block

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, dad or mum=None)

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

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, you want to restart it manually and check out once more.

When you see the “label0” message on the controller’s display, it’s time to open the AWS console and examine 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 vary the label on the machine’s display.

It’s all about your creativeness; what do you need to do with the IoT machine related to the AWS cloud?

Micropython

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

To make use of this method you want 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, blocky, web-based OTA instrument however a superb previous Visible Studio Code.

Let’s begin with the controller. All you want 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’ll be able to set up the IDE immediately from the official web page https://code.visualstudio.com/. When you’re engaged on a Home windows machine with WSL, the IDE will probably be obtainable in each (Home windows and Linux) environments. As we may have some further libraries, simpler to handle in Linux, let’s use VSCode from WSL. Let’s open WSL and run ‘code’. The latest IDE will probably be downloaded (in case your model is out of date) and run.

Up to now, 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 problem is USB forwarding. By default, USB ports should not obtainable within the WSL setting. To alter it, you want to use usbip. This instrument will redirect all USB visitors through IP protocol to your Linux setting.

In WSL, you want 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 word, 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, you want to set up linux-tools-xxx-generic. You’ll see latest packages for not-generic utilization obtainable, however it’s not you’re searching for.

Now, utilizing PowerShell console you want to set up usbip and connect the right port. It’s type of unusual, however you may set up the instrument and redirect the port utilizing normal person privileges, however to detach the already connected port, you want to 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 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 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’s nothing to be nervous about. What we want 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 in PowerShell console.

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

If the machine is connected, open your WSL console and run ‘lsusb’ for verification. The output ought to comprises 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 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 correct title of your USB connector.

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

M5Stack controller

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

M5Stack controller

Hit it and decide the right /dev/ttyUSB* machine from the record. 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 principle.py file on the controller and put the certificates and the non-public key information within the correct listing (in accordance 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 Blocky editor, WYSIWYG display designer, and easy code administration. It’s {powerful}; nonetheless, it’s additionally restricted to superior utilization. You too can use VS Code as a correct, fashionable IDE to create your utility. You’ll be able to mess around with it, blink some LED in your controller or gather some further information from exterior modules (in case you have some). You want to remember that Micropython shouldn’t be the total Python however nonetheless preserves a developer-friendly syntax and a few frequent libraries. Nonetheless, then again, there is no such thing as a solution to set up any further library to your code, so we will’t use TensorFlow with Micropython this manner. We’ll attempt to handle this problem within the subsequent chapter of this text.