Azure IoT Hub Node-RED Connection Node Follow
Overview
The Azure IoT Connection node provides a connection to a configured instance of the Azure IoT Platform for device data collection in the Azure Cloud. The node allows for connection with both Azure IoT Hub and Azure IoT Central. This tutorial specifically covers how to configure the node to connect to Azure IoT Hub. For information about using the Azure IoT Connection node to connect to Azure IoT Central, see Azure IoT Central Node-RED Connection Node. When configured correctly, the node will upload any incoming messages to the Azure IoT Hub platform.
This article assumes set up on the Azure IoT Hub platform has been completed. For information regarding the set up of devices within the Azure IoT Hub, please see the example at the end of this article. Knowledge of the following articles is recommended before proceeding.
- MobiusFlow Architecture
- Node-RED Flows
- Mobius Core node-RED Nodes
- Mobius Cloud Format & Store and Forward Nodes
Node Properties
The Azure node has 5 properties that require configuration. These are shown in the properties window below.
Connect To
The 'Connect To' drop-down menu is used to control if the node is connecting to Azure IoT Hub or Azure IoT Central. Connecting to Azure IoT Central is outside the scope of this article. The Azure IoT Hub option should be selected to connect to Azure IoT Hub.
Hub Name
This property should match the name of the IoT Hub resource already set up within Azure. This is optional and is only required when building a connection string for connecting to an Azure IoT Hub device. See the Connection String section for more details.
Device ID
The Device ID property should match the device ID of the device set up within Azure. This can be found on the Microsoft Azure page as outlined by the red box in the window below.
Primary Key
The primary key can be obtained by clicking on the instance of the device set up within Azure. Clicking on the device reveals the following window. The primary key is outlined by the red box. Please note, the key cannot be copied normally and instead should be copied using the copy button outlined in orange.
Connection String
There are two methods to obtain the connection string. Method 1 uses Azure to generate the connection string while Method 2 uses Mobius to generate it. Both methods are equally valid and are both explained in the following sections.
Method 1 - Azure Generated
The Connection String can be found within Azure when clicking on the device. This is in the same place as where the primary key was found in the previous section. This is outlined in the red box in the window below. Please note, the string cannot be copied normally and instead should be copied using the copy button outlined in orange.
Method 2 - Mobius Generated
The connection string can be generated by pressing the generate button within the properties window of the Azure IoT Connection node. This is outlined in red below.
Please note, for this to work correctly, the 'Hub Name' property must be the same as the name of the IoT Hub set up within Azure. This can be found in the resource list in the working resource group within Azure as outlined in red below.
Connection Status
Connected
If configured correctly, the Azure IoT Connection node connects to the device within Azure. The node will show connected status as seen below.
When connection is made, the node will output a message containing the storeFwdControl (msg.storeFwdControl) property. The property will be set to true causing any connected Store and Forward nodes to enter forwarding mode. A typical setup with a Store and Forward node is shown in the diagram below. For more information on the Store and Forward node Mobius Cloud Format & Store and Forward Nodes.
Disconnected
If the node loses connection, a message will be outputted with the storeFwdControl parameter set to false. This will send the Mobius Store and Forward node into store mode until such a time connection is reestablished.
Example
This example demonstrates how to use the Azure IoT Connection node to send data from a device in Mobius to the Azure IoT Hub. This example uses a single Temperature/Humidity sensor which is set up under an EnOcean micro-service. A node-RED connector service has also been set up to connect Mobius Node-RED nodes to Mobius. The configuration for the setup is shown within Mobius below. The Temperature/Humidity sensor used is outlined in red.
Setting up the device on Azure IoT Hub
Setting up a device within the Microsoft Azure Hub platform can be split into three stages. Creating a resource group, creating a resource within the resource group and creating a device within the resource. These three stages are explained below.
Create Resource Group
Within the Azure Hub platform navigate to Resource Groups, outlined in red in the window below, and then click the add button outlined in orange.
The resource group creation wizard prompts the user to add a resource group name. For the purposes of this example, the resource group has been named 'ExampleBuildingA' as outlined in red below. The 'Review + Create' button, outlined in orange, should then be clicked to confirm the creation of the resource group.
Create Resource
Once the new resource group has been created, it should be selected within the list of resource groups. When selected the add button, outlined in red below, should be clicked to create a new resource.
The resource type 'IoT Hub' should be searched for within the search box. This is shown outlined in red below.
The create button adjacent to the IoT Hub resource type should be selected. This is shown in red below.
The resource creation wizard prompts the user to add a Hub Name. The name 'ExampleMobiusHubA' has been used for this example. Please note, this must match the Hub Name used to configure the Azure IoT Connection node within Mobius if the Mobius generated Connection String is to work correctly. Please see the Connection String - method 2 section within this article for more details. The Hub Name entry is shown below in red. The 'Review + create' button should then be pressed to confirm the resource creation. This is shown in orange below.
Create Device
Once the Mobius Hub has been created as a resource within Azure, it should be selected and the IoT devices menu should be navigated to. This is shown in red below. Once on this menu, the new button can be clicked to create a new IoT device. This is shown in orange below.
The device creation wizard prompts the user for a Device ID. In the example, the name 'A5-04-03' has been used to match the type of EnOcean sensor being used. This is shown in red below. The primary and secondary key fields have been left blank as these will be auto-generated by Azure. The save button can then be clicked to confirm the creation of the device. This is shown in orange below.
The device is now fully set up and is ready to be used with Mobius. Selecting the device within the list of devices reveals the credentials required to connect to the device with the Azure IoT Connection node in Mobius. These are shown in the red boxes below.
Setting up Mobius
The following flow is generated. The flow uses several nodes without explanation. The articles containing more information on the function and configuration of these nodes are listed below.
- Set Resource - See Mobius Core
- Mobius Flow Inject - See Mobius Core
- Mobius Cloud Format - See Mobius Cloud Format & Store and Forward Nodes
- Store and Forward - See Mobius Cloud Format & Store and Forward Nodes
The Azure IoT node is configured for use with the properties found in the previous section of this example. Note, the node shows the connection status to show it has successfully connected to the device within Azure IoT Hub.
The two inject nodes, labeled 15 and 16, inject messages to the Set Resource node which changes the temperature resource (URI - 000001/020/0034/0001/40) of the temperature sensor to 15 or 16 degrees respectively. The inject nodes are set to inject data every few seconds, simulating a sensor with a constantly changing reading.
The Mobius Flow Inject node is subscribed to changes to the Temperature/Humidity sensor so it sends a message whenever the temperature reading changes. The message then passes through the Mobius Cloud Format node. These nodes prepare the data to be uploaded into the Azure IoT Hub.
Finally, the data is relayed by the Store and Forward node to the Azure IoT connection node ready for upload. The output is fed back into the Store and Forward to automatically stop the Store and Forward node forwarding if connection to Azure is lost (see Connection status section of this article for more information).
Operation
The flow is deployed and the inject nodes begin sending continuous data to simulate a constantly changing temperature reading. This uploads a continuous temperature data stream to Azure IoT Hub.
The data can be viewed by creating a Stream Analytics resource within the resource group previously created. The Stream Analytics resource responds to input messages from the IoT Hub within Azure and outputs these to a location of the users choice via an SQL query. An example Stream Analytics set up is shown below.
In this example, Stream Analytics has been set up to stream the data from the Hub to Microsoft's Power BI platform. Within the Power BI platform, a line graph has been set up to plot the temperature against time. This is shown below.
Comments
0 comments
Please sign in to leave a comment.