This document has only the specific information about the device connection settings and its address syntax.
Refer to the User Guide for more information on the Device Module.
On this page:
Communication Driver Information | |
---|---|
Driver name | MQTTAzure |
Assembly Name | T.ProtocolDriver.MQTTAzure |
Assembly Version | 1.0.0.3 |
Available for Linux | True |
Devices supported | Any device compatible with the MQTT protocol |
Manufacturer | Microsoft |
Protocol | MQTT (Message Queuing Telemetry Transport) is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. |
Interface | TCP/IP |
PC Requirements | Ethernet port |
Implemented Methods | Connect, Disconnect, Subscribe, UnSubscribe and Publish. |
Azure IoT Hub is a managed cloud service that acts as a central message hub for bi-directional communication between IoT applications and their connected devices. It facilitates secure, reliable connectivity for millions of devices, supporting messaging patterns such as device-to-cloud, cloud-to-device, and file upload from devices.
The software platform integrates with Azure IoT Hub and other Azure services for enhanced IoT solutions, enabling real-time analytics, machine learning, business process automation, and event-driven responses.
For more information on Azure IoT topics, see IoT concepts and Azure IoT Hub.
The requirements for a successful configuration of MQTT and Azure are listed below:
Visual Studio Code is an open-source, streamlined code editor with support for development operations like debugging, task running, and version control. Visual Studio Code can be downloaded here.
1. To download the extension that allows interactions to Visual Studio, Azure IoT Hub, and IoT Device Management, click here.
This guide assumes that an IoT Hub already exists within the Azure Portal account. |
2. After installing the extension, open the VSCode application. In the explorer pane of the VS Code, click the ”Azure IoT Hub” tab in the bottom left corner (1), and click ”Select IoT Hub” (2) in the context menu.
If you are not signed into Azure, a pop-up will appear in the bottom right corner to prompt you sign in to Azure (3).
3. Your Azure Subscription list will appear after you sign in. Select the Azure Subscription and IoT Hub. After a few seconds, devices and endpoints will appear in the Azure IoT Hub tab.
4. New IoT Devices can be created through the Azure IoT Hub extension. Select the context menu (1), click Create Device (2), and enter a Device ID for the new IoT device.
5. Now that your device is created, you need to setup your Connection String and SAS Token.
Right-click on your device and select Get Device Info. You should see information displayed in the output window.
6. Review the Connection String that was created for the device and record the following pieces of information from this string:
HostName = TatsoftTestHub.azure-devices.net DeviceId = NewDevice |
7. Right-click on the device, select Generate SAS Token for Iot Hub, and enter the expiration time. You should see the information displayed in the output window.
8. Record the following pieces of information from this string:
Password = SharedAccessSignature sr=TatsoftTestHub.azure-devices.net&sig=pzzXCZK%2 BQ6dJUe2Hf1SptTR1oPMbfyip0K9M4TQPZc4%3D&skn=iothubowner&se=1593008673 |
9. To verify that data is flowing from the MQTT Driver to the cloud-based Azure IoT Hub, right-click on the device and select Start Monitoring Built-In Event Endpoint.
Not used in this driver.
Station syntax: <URL> ; <Port> ; <ClientID> ; <Username> ; <Password> ; <SslProtocol> ; [X509Certificate] ; <PayloadFormat>
Where:
URL = <Host Name> Port = <Port Number> Client ID = <Device Id> Username = <Host Name>/<Device Id> Password = <SAS Token> TLS Version = <TLSv1.0> X509 Certificate = <Certificate thumbprint> (optional) QoS = <Quality of Service> Keep Alive = <Message sent to Broker to prevent the link from being broken> (in seconds) |
For the IoT Hub and the device configured in VSCode, the parameters follows:
URL = TatsoftTestHub.azure-devices.net Port = 8883 Client ID = NewDevice Username = TatsoftTestHub.azure-devices.net/NewDevice Password = SharedAccessSignature sr=TatsoftTestHub.azure-devices.net&sig=pzzXCZK%2 BQ6dJUe2Hf1SptTR1oPMbfyip0K9M4TQPZc4%3D&skn=iothubowner&se=1593008673 TLS Version = <TLSv1.0>X509 Certificate = QoS = AtMostOnce Keep Alive = 1 |
One of the easiest ways to install the certificate on client computer is to use the wizard to import the certificates is through the "Internet Options" to import the certificate in the "Trusted Root Certification Authorities”. You need to import the certificate in DER format. |
URL = CustomHub-MQTT.azure-devices.net
Port = 8883
Client ID = MQTT_Device
Username = CustomHub-MQTT.azure-devices.net/ MQTT_Device
Password = SharedAccessSignature sr=CustomHub-MQTT.azure devices.net&sig=ygIt%2FG8fIFAXhUwGXBlm6WKDK5IhbmVHkUs4atsbXFU%3D&skn=iothubowner&se=1612 031715
Ssl Protocol = <TLS>
X059 Certificate =
Payload Format = JSON/SparkplugB
The MQTT for Azure protocol supports Write Commands only.
The Address syntax is: [Payload] : <Group> ; <Node> ; <Device>
Where:
E.g.: Payload:GroupID;NodeID;DeviceID
Payload:AzureGroup;Factory1;Motor
For this example device, the correct topic is:
devices/NewDevice/messages/events/
Go to Runtime → Startup and execute the solution. Enable the Debug and Devices options by clicking on settings and selecting the corresponding boxes. Open the TraceWindow and monitor the communication exchange.
Back on VSCode, you should see the received messages in the output window.
This section details some errors you might see in your connection to Azure IoT Hub via MQTT protocol and its possible causes.
If you see this message on the Trace Window Logs (with modules Debug and Devices enabled), check the port number parameter on Devices / Nodes / PrimaryStation.
If you get this error code on @Devices.Node.<NodeName>.Status
, check if the URL defined on Devices / Nodes / PrimaryStation is correct.
If you see this message on the Trace Window Logs (with modules Debug and Devices enabled) check the following parameters on Devices / Nodes / PrimaryStation:
If you see this message on the Trace Window Logs (with modules Debug and Devices enabled), check the QoS setting on Devices / Nodes / PrimaryStation. As stated in a previous section, IoT Hub will automatically disconnect your client if the QoS value is 2 (Exactly Once).
Another reason is that someone else tried to connect to your IoT Device using the same DeviceID. IoT Hub only supports one active MQTT connection per device. Any new MQTT connection on behalf of the same DeviceID causes IoT Hub to drop the existing connection.
MQTTAzure Revision History | |
---|---|
Version | Notes |
1.0.0.3 | Initial release on new documentation standards. |