MQTT Datasource

This section will show you how to create and configure a MQTT Datasource. The MQTT Datasource enables you to connect to an MQTT server and subscribe to messages communicated using the MQTT messaging protocol.

For this example we will connect to an Azure IoT Hub that is configured to support MQTT communication. For instructions on how to set this up, please visit the Cloud Sink with Azure IoT Hub.

Both SAS Token Authentication and X.509 CA Certificate Authentication are supported but in this example we will show how to configure for SAS Token Authentication.

Configuration of MQTT Datasource

  1. In DataStudio, first create a local Connector. Then, select the Connector, right-click Admin  New  External Interface  Datasource from the Context menu add a new datasource. In the Create Object wizard, go to the Server Type page, select "MQTT" from the drop-down menu and enter "MQTT" as the Subscriber Name. Click Create to create the Datasource.

    Create MQTT Datasource - Server Type
    Figure 1. Create MQTT Datasource - Server Type
  2. Select the Datasource in the I/O Model and go to the Connection Parameters property compound. These should be entered as described in the Cloud Sink with Azure IoT Hub instructions. The Broker address is of the form:

    [Hub name] + .azure-devices.net

    For this specific example, the Broker Port is set to "8883" and the Client ID to "indevice".

    MQTT Datasource - Connection Parameters
    Figure 2. MQTT Datasource - Connection Parameters
  3. In the Credentials property compound enter the Username and the generated SAS Token. The Username should be of the form:

    [Hub name] + .azure-devices.net + [Device ID] + /?api-version=2018-06-30

    MQTT Datasource - Connection Parameters
    Figure 3. MQTT Datasource - Connection Parameters
  4. Open the SSL/TLS Support property compound and enter the path to a DigiCert Baltimore Root certificate. For details on how to obtain this follow the instructions here.

MQTT Datasource - SSl/TLS Support
Figure 4. MQTT Datasource - SSl/TLS Support
  1. To subscribe to a topic click on the "+" in the Subscription Data property compound and set Topic as "#" (this means subscribe for the complete tree of topics available on the broker), and Quality Of Service as "0".

MQTT Datasource - Subscription Data
Figure 5. MQTT Datasource - Subscription Data
For subscriptions, two wildcard characters are supported

For this example imagine that broker has 4 topics: SENSOR/1/TEMP, SENSOR/1/HUMIDITY, SENSOR/2/TEMP, SENSOR/2/HUMIDITY. - """ character represents a complete sub-tree of the hierarchy and thus must be the last character in a subscription topic string. Example: SENSOR/1/ This will match any topic starting with SENSOR/1/, such as SENSOR/1/TEMP and SENSOR/1/HUMIDITY. - "+ character represents a single level of the hierarchy and is used between delimiters. Example, SENSOR/+/TEMP This will match SENSOR/1/TEMP and SENSOR/2/TEMP.

A successful connection is indicated by both state and communication indicators being green. This also means that Datasource has successfully subscribed to all specified topics and now is waiting for notifications.

Receiving Messages

To check that you are receiving messages, you can firstly check in the Azure IoT Portal by going to IoT Hub  IoT devices  %Device%  Message to Device  Send Message in the portal.

Send Message in IoT Hub
Figure 6. Send Message in IoT Hub

Alternatively, you can use the Visual Studio Code extension to send messages to the device. Select the device in the list and use Send C2D Message to Device from the context menu.

Send Message in VS Code
Figure 7. Send Message in VS Code
Send Message in VS Code - Output
Figure 8. Send Message in VS Code -

The message can now be seen in Data Studio.

Message Received - I/O Model
Figure 9. Message Received in I/O Model

And in the Faceplate of the item.

Message Received - Faceplate
Figure 10. Message Received in I/O Model