Microsoft's Azure IoT Hub is a cloud service for the secure management of Internet of Things (IoT) devices. Sending messages or telemetry data to the Azure IoT-Hub is achieved through the use of appropriate software. However, this can also be done with the help of Microsoft Flow.
Microsoft's Azure IoT-Hub is the scalable transport layer for connecting, monitoring and managing Internet of Things devices. There are countless examples and solutions to send (test) data to the IoT-Hub. Nearly all of these approaches have the disadvantage that you have to deal with, corresponding to single-board computers and/or the programming of simulators, which can be very time-consuming.
The task is to find a simple method to send data (messages) to the Azure IoT-Hub without much effort.
The creation of an IoT-Hub and related devices in Microsoft Azure is not discussed in this article. For more information, visit:
https://azure.microsoft.com/en-us/services/iot-hub/
The sending of events and messages with HTTP via REST (Representational State Transfer - programming interface/programming paradigm) seems to be a favorable procedure here. Using the POST method (for transmission), a HTTP request can be sent to the IoT hub in order to transport the data from the body. The structure of the URI to be used is as follows:
https://{IoT-HubID}.azure-devices.net/devices/{DeviceID}/messages/events?api-version=2016-02-02
IoT-HubID is the unique name of the used Azure IoT-Hub and DeviceID is the unique device ID. The body of the call contains the possible value pairs according to the following scheme:
Body-Structure for the value pairs
Body-Structure | Body-Example |
---|---|
|
|
For security reasons, a SAS token must also be sent using the header of the HTTP request. The device is then authenticated and can send data to the IoT-Hub. The generation of the SAS token can be generated with the help of a few lines of code or better, with the Azure Device Explorer.
The IoT-Hub Connection String must simply be entered in the Device Explorer twin. This can be found in the Azure Portal under the desired IoT-Hub in the "Settings / Guidelines for shared access" area:
Fig. 1: IoT-Hub – Shared access policies
The "Connection string - primary key" is entered in the Device Explorer as follows:
Fig. 2: Device Explorer Twin – Connection string - Primary key
In the Device Explorer under the tab "Management" you can find all devices registered in the IoT-Hub after a [Refresh].
Fig. 3: Device Explorer Twin – Device Management
The [SAS Token...] button can then be used to generate the SAS token by specifying the validity period (TTL Days). We only need the marked part of the generated text. This can simply be copied to the clipboard for further use.
Fig. 4: Device Explorer – SAS Token Form
For the HTTP request, we now have the URI with IoT HubID and DeviceID, the header with the generated SAS token, and the body with the corresponding measurement data.
Microsoft Flow can be used to assemble all parts and send data to the IoT hub. With Microsoft Flow it is possible to create and manage automated workflows. Further information about Microsoft Flow can be found at the following link:
https://emea.flow.microsoft.com/en-us/
We only need two connectors here. One flow button connector and one HTTP connector. The advantage of this connector combination is that the flow app can also be used on the mobile phone to record and send measured values.
The overall configuration for our flow example "AzureIoTHubTest" thus looks like this:
Fig. 5: Microsoft Flow – “AzureIoTHubTest”
The Button Connector is configured as follows:
Fig. 6: Button Connector
As an example, two fields ("Status" and "Temperature") are added, where "Status" contains a list of possible options and "Temperature" is a free input field. By default, the flow button also provides the location, user, timestamp, etc.
In the next connector everything is put together:
Fig. 7: HTTP Connector
POST is selected as the method for the HTTP request. The URI, specified as described above, the generated SAS token is entered in the header and the body is assigned the required value pairs.
If the flow is now started via mobile phone or the MS-Flow-Dashboard, the successful workflow run can be tracked in the course of the process.
Fig. 8: Successfully executed flow
The values of the button passed in the body can also be seen here.
On the Azure IoT Hub overview page, the reception of the message is displayed:
Fig. 9: Testing the usage in the IoT Hub Dashboard
Sending messages to the Azure IoT hub is easy with Microsoft Flow. No matter if you want to send test data to the IoT-Hub quickly or if you want to collect real data via mobile phone in the field. The combination of Azure IoT-Hub, Device-Explorer and Microsoft Flow allows a fast and comfortable realization of these applications.
If IoT devices are to send data to the IoT hub, corresponding code snippets must be used for the authentication and transmission of telemetry data. Here, however, a corresponding but manageable development effort is necessary. This would, however, also regulate bidirectional communication between IoT devices and the solution backend. At the end the data has to be analyzed. Also here Microsoft Azure offers corresponding services such as Stream Analytics and Machine Learning.
If you don't want to orchestrate these services yourself, you are well advised to use the pre-configured solutions from the Azure IoT Solution Accelerators. There are solutions for remote monitoring, maintenance solutions, solutions for the connected factory and various device simulations. The advantage of these preconfigured solutions is that the used Azure services can be adapted and extended by the user as if the services were added manually in Azure.
Another IoT service in Azure is IoT-Central. IoT-Central is a fully managed SaaS (Software as a Service) solution that makes it easy to link, manage and monitor IoT resources. Setting up this service is very simple, so the effort is limited.
Overall, Microsoft Azure IoT provides a comprehensive collection of services and solutions for creating end-to-end IoT applications. From fully hosted and managed to customizable services that can be tailored to meet the specific needs of each user's industry.
Titelbildnachweis: https://pixabay.com/de/netzwerk-iot-internet-der-dinge-782707/
No available comments yet
Do you have any questions?