Azure IoT and M5Stack with M5Flow Blocky Python - D2C

Device to Cloud (D2C) with Azure IoT Hub

I've been playing with the M5Stack Core2 devices and wanted to see how hard it would be to create a program that could send data to Azure without having to actually write any code.  I used the M5Flow (blocky) graphical program builder. It includes common cloud blocks for Azure and AWS. 

The program sends sensor data to Azure IoT where it can be processed. I only verified the messages were received in Azure.  Processing will be left for another time.

There weren't a lot of samples out there. I hope this will help others can use this as a starting point. You can find a link to a video walkthrough down below.

Azure IoT Hub Device-to-Cloud

From the Microsoft guide https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-d2c-guidance

When sending information from the device app to the solution back end, IoT Hub exposes three options:
  • Device-to-cloud (D2C) messages for time series telemetry and alerts.
  • Device twin's reported properties for reporting device state information such as available capabilities, conditions, or the state of long-running workflows. For example, configuration and software updates.
  • File uploads for media files and large telemetry batches uploaded by intermittently connected devices or compressed to save bandwidth.

Python Program - Fun with Blocky

This program was created with M5Flow/UIFlow desktop 1.7.5.  The web version gets updated more often.  The current web version 1.8.7 has a different connection setup.

Startup
  1. Synchronizes the device time
  2. Pushes the time to the screen and lights up LED 1
  3. Connects with azure, turns on LED 3, and updates the twin metadata with the current time
  4. Retrieves the twin meta and displays it on the screen. and turns on LED 5
Loop
  1. Creates a dictionary with the x rotation, y rotation, temperature, and the current time.
  2. It then updates the screen with those values.
  3. The dictionary is converted to JSON and sent to the Azure IoT Hub and logs the JSON to the console.
  4. Sets the light bar status based on the iteration modulo
  5. Waits 10 seconds and then goes back to the top of the loop

Device Screen Layout

All the text elements on this screen are label elements.  Some will act as labels. Others are labels that will be replaced by values in the programs.
  1. The Twin fields display the Azure IoT twin metadata properties.  The program updates one of the metadata elements. That value may appear on this screen.
  2. The X and Y angle elements contain the current rotation based on data from the IMU.
  3. The temperature labels represent the internal thermocouple temperature.
  4. The Current time is just that
The screen is updated every 10 seconds


Video

Azure IoT Hub 

Our device was configured with the connection string for a device registered in our hub.

Monitoring Message Traffic

We view the messages using the Azure CLI available in the Azure portal cloud shell environment.  Each event represents a single message received from the device.

$ az iot hub monitor-events --output table --hub-name <your hub name>
Starting event monitor, use ctrl-c to stop...                        


Console Logging

The M5Burner (firmware) application has a USB console.  We can use it to see the serial port output from the program.  This only works if the device is connected over USB.

The program logs every message sent to Azure.

References

Created 2021/11/28

Comments

Popular posts from this blog

Understanding your WSL2 RAM and swap - Changing the default 50%-25%

Accelerate Storage Spaces with SSDs in Windows 10 Storage Pool tiers

Java 8 development on Linux/WSL with Visual Studio Code on Windows 10