Azure IoT and M5Stack with M5Flow Blocky Python - C2D
The M5Flow (blocky) graphical program builder makes it easy to drag and drop a program that receives C2D messages from 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 sends data Azure to an IoT device without having to actually write any code. They support several development environments
including a graphical Python builder. The UIFlow IDE includes common cloud integration blocks for Azure and AWS.
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 Cloud-to-Device
From the Microsoft guide
https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-c2d-guidance
IoT Hub provides three options for device apps to expose functionality to
a back-end app:
- Direct methods for communications that require immediate confirmation of the result. Direct methods are often used for interactive control of devices such as turning on a fan.
- Twin's desired properties for long-running commands intended to put the device into a certain desired state. For example, set the telemetry send interval to 30 minutes.
- Cloud-to-device (C2D) messages for one-way notifications to the device app.
Python Program
The program sends data from Azure to the IoT device for display on the
device. I use the Azure Portal to send data and did not write a cloud data
source. That will be a future exercise.
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.
Fun with Blocky
This program listens for Azure IoT Hub generated Cloud-to-Device messages and displays them in a text field.
Setup
- My UI Flow editor was connected via USB so this program has to initialize its own Wi-Fi connection in order to connect to the internet.
- Get the current time and display it in a field on the screen and enable RGB 1.
- Connect to Azure and enable RGB 2
- Update the reported twins with the connect time and enable RGB 3
Loop / Body
A minimal main program loop because the real work is done in the event handler
- Flash RGB 5 every 2 seconds to show it is alive.
Subscribe / Callback / Handler
The Subscribe C2D message handler is invoked every time a message is received from Azure
- Push the body of the message into the field on the screen
- Flip the color of RGB 10 whenever a message is received
The IoT device screen
There are three labels on the screen.
- One is purely a label that doesn't change
- The 2nd one down is a placeholder for messages received from Azure. It is updated with the content of every message
- The bottom label is a time field. It is updated with the current time every 2 seconds by the main program loop.
Azure Portal
Our device is bound to a Device in an Azure IoT Hub. We can view and update data and metadata in the Azure Portal.
IoT Hub Device - C2D - Cloud to Device
This is the main screen for Device-1 in Azure IoT Hub M5Stack-Hub. You can see the hidden connection strings we put into our program to connect to the device.This screen has links for sending both one-way Message to Device and forrequest/response Direct Method invocations.
Sending the message
We clicked on Message to Device on the screen above. Just enter your message in the Message Body and press the Send Message button. The IoT device receives the Message Body exactly as presented on this screen.Video
References
Microsoft Docs
- https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-d2c-guidance
- https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-messages-d2c
- https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-c2d-guidance
- https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-device-twins
- https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-create-through-portal
M5Stack Docs
Created 2021/12/03
Comments
Post a Comment