The IoT Hub is the gateway for data to the Microsoft Azure Portal. Data can be exchanged with the IoT Hub via MQTT. We show the MQTT connection of the IoT Hub in a step by step tutorial with the OPC Router as MQTT Client.

The Microsoft Azure Cloud is one of the leading cloud environments on the market. The Azure Portal provides many ready-made services that can be easily connected. Cloud applications are created by interconnecting the finished components. One of these components is the IoT Hub, which serves as the most important bidirectional connector for external data sources and IoT devices. With the OPC Router, the IoT Hub and thereby the Azure Cloud can be connected to write data to the Azure Cloud, but also to obtain data from the Cloud for the field level. The connection of the IoT Hub is realized with the OPC Router MQTT Client Plug-in. The transfer of data at the field level or the data procurement for the cloud is implemented with the various plug-ins of the OPC router.

Azure IoT Hub MQTT Connection

MQTT Connection of the Azure IoT Hub

For the connection the following steps are necessary, which we will explain in detail below. If you do not yet have access to the Azure portal, you can create a free Azure account here.

  1. Create an Azure IoT Hub in the Azure Portal
  2. Determining the Azure IoT Hub connections data
  3. Configuration of the MQTT Client for connecting the IoT Hub

(Click to enlarge images)

Creating an Azure IoT Hub

  1. Login to the Azure Portal with your account.
  2. Create a new resource of the type “IoT Hub”.
    Azure IoT Hub - Create Ressource

    Azure IoT Hub – Create Ressource

  3. Create the IoT Hub:
    Azure IoT Hub - Create

    Azure IoT Hub – Create

  4. Choose the resource group (1)(maybe create it) and name the IoT Hub (2) and check the selection (3):
    Azure IoT Hub - Ressource Group

    Azure IoT Hub – Ressource Group

  5. Finalize after review:
    Azure IoT Hub - Finalize

    Azure IoT Hub – Finalize

Azure IoT Hub Determine connection data

  1. Select the newly created IoT Hub under “All Resources”(1) and invoke the “Shared Access Policies”(2). Under “Policy”, select “iothubowner”(3):
    Azure IoT Hub Connection String

    Azure IoT Hub Connection String

  2. Copy the “Connection string – Primary key” from the “iothubowner” policy:
    Azure IoT Hub - Copy Connection String

    Azure IoT Hub – Copy Connection String

  3. The connection string must be converted with the “DeviceExplorer”. The DeviceExplorer can be downloaded here (or directly download the DeviceExplorer Installer here).
  4. In DeviceExplorer, enter the connection string(1) and the hostname(2) and click “Update”(3).:
    IoT Hub Device Explorer

    IoT Hub Device Explorer

  5. Then create(1) and confirm a Device with any name(2) in DeviceExplorer via “Create”(3):
    Create IoT Hub Device

    Create IoT Hub Device

  6. Finally create a SAS token(2) in the tab “Management”(1):
    IoT Hub SAS Token Create

    IoT Hub SAS Token erzeugen

  7. For the token, select the Device you just created(1) and set a TimeToLive(2). After generation(3) finally copy out the required part for the client(4):
    IoT Hub SAS Token

    IoT Hub SAS Token

Configuring the MQTT Client to Access the IoT Hub (here OPC Router MQTT Client Plug-in)

  1. Transfer the connection data from the DeviceExplorer to the MQTT Client configuration. To do this, give the MQTT connection a free name(1), store the host from the DeviceExplorer as broker connection(2), use the selected DeviceID from the DeviceExplorer as client ID(3), connect the host name and the DeviceID with a “/” as user name(4), use the copied part of the SAS token from the DeviceExplorer as password(5) and finally set the Quality of Service to the value “At least once”(6). Then test the connection(7) and save if successful(8):
    OPC Router Azure IoT Hub Configuration

    OPC Router Azure IoT Hub Configuration

  2. Use the appropriate topic to send the data. For data sent to the cloud you can use “devices/{device-id}/messages/events/”:
    MQTT Transfer_Object

    MQTT Transfer_Object

  3. Define a trigger for receiving events with the Topic for Events. Devices/{device-id}/messages/devicebound/#” can be used for data from the cloud to the device:
    MQTT Client Trigger

    MQTT Client Trigger

Example: Temperature transmission to the Azure IoT Hub with the OPC Router

In this example, temperature data is imported from an SQL database and sent to the Azure IoT Hub as a character string. The received datas are displayed via the integrated monitoring in the DeviceExplorer.

Azure IoT Hub Example Connection OPC Router
Azure IoT Hub Monitoring via DeviceExplorer

Azure IoT Hub Connection via Drag & Drop!

You can use the OPC Router to connect your production systems with the Azure Cloud via MQTT in a graphical way.

Request your download and test the connection to Azure IoT Hub!