Getting Started with MQTT

This page takes an MQTT broker connection from nothing to a working subscriber and a working publisher. It uses the public HiveMQ broker so that there is no broker to install; a broker of your own differs only in its IP Address, Port and credentials.

Info

MQTT is a licensed option. Open Configure OAS and check that MQTT appears in the licensed drivers list. OAS 8.26 or later is required.

Configure MQTT Data Source

In the following steps you will create and configure a MQTT Connector to connect to a third party broker so that tags will be able to subscribe to a topic. For the purposes of this guide, you will connect to the public HiveMQ broker.

  1. Select Drivers in the sidebar.

  2. Click + Add, enter a meaningful Driver Interface Name to give this driver interface instance a unique name (for example MQTT Data Source), and click Create. Once created, the driver interface name should appear in the list of drivers.

  3. Ensure the following parameters are configured:

    • Driver: MQTT
    • IP Address: broker.hivemq.com
    • Port: 1883
    • Client ID: OAS_Client_123

    MQTT connector configuration

    The app flags the empty User Name and Password fields. The public HiveMQ broker accepts anonymous connections, so leave them blank here. Fill them in if the broker you are connecting to requires authentication.

    Tips

    If you are having trouble reading a value in later steps, try to change the Client ID to something else as each client ID connecting to the broker must be unique.

  4. Turn on Enable and click on the Apply changes button. The driver interface is not active until it is enabled and the changes are applied.

    Apply changes button

Assign MQTT as Tag Data Source

You will now set the Tag's data source to the MQTT driver interface that you created previously.

At the end of this section you will need to use a tool like MQTT Explorer to publish a value to your Tag.

  1. Select the Tag that will source data from the MQTT data source.

    Tag

  2. Set the following properties:

    • Data Type: JSON
    • Data Source: MQTT
    • Select Driver Interface: MQTT Data Source
    • Topic: oas/temperature

    MQTT tag configuration

  3. Click on the Apply changes button to apply the changes.

  4. Using an MQTT testing tool to publish messages, such as MQTT Explorer, publish a JSON message to the oas/temperature topic.

    {
        "temperature": 24.9
    }
    

    MQTT explorer connection

    MQTT explorer connection advanced

    MQTT explorer connection publish

  5. Check that the quality status is Good and you can see the JSON value.

    MQTT tag quality

Configure MQTT Publishing

In the following steps you will create and configure a second MQTT Connector to connect and publish tags to a third party broker. For the purposes of this guide, you will connect to the same public HiveMQ broker.

Warning

Do not publish sensitive information to a public broker.

  1. Select Drivers in the sidebar.

  2. Click + Add, enter a meaningful Driver Interface Name to give this driver interface instance a unique name (for example MQTT Publisher), and click Create. Once created, the driver interface name should appear in the list of drivers.

  3. Ensure the following parameters are configured:

    • Driver: MQTT
    • IP Address: broker.hivemq.com
    • Port: 1883
    • Client ID: OAS_Publisher

    MQTT connector configuration

    The app flags the empty User Name and Password fields. The public HiveMQ broker accepts anonymous connections, so leave them blank here. Fill them in if the broker you are connecting to requires authentication.

  4. Turn on Enable and click on the Apply changes button. The driver interface is not active until it is enabled and the changes are applied.

    Apply changes button

Publish Selected Tags in MQTT

In this step you will select the Tags that you want to publish to the MQTT broker.

  1. In the Drivers screen, select the MQTT driver instance that you created in the previous section (for example MQTT Publisher).

  2. Make sure the Publish Selected Tags toggle is turned on.

    Publish selected tags

  3. In the Tags To Publish table click on the Add button.

    Publish selected tags Add button

  4. The Publish tag window will appear. Click on the … button to browse for a Tag, select the Tag you want to add in the left hand panel and then ensure the Value property is selected, and click on the Use this tag button.

    Tag browser

  5. Back in the Publish tag window the Id defaults to the full Tag path (e.g. TemperatureSensor.Value). If you want to set your own property name, you can change the Id field to your own custom value. Click on the OK button.

    Publish tag window

  6. The Tag has now been added to the list. You can add other Tags by repeating steps 3 to 5.

    Tag added

  7. Click on the Apply changes button.

Verify Messages are Published to MQTT Broker

In this step you will confirm that OAS is successfully publishing your selected Tags to the external MQTT broker. By default, the publishing type is set to Continuous and the interval is 10 seconds.

In this section you will need to use a tool like MQTT Explorer to subscribe to the OAS tags in the HiveMQ broker.

  1. Open MQTT Explorer and configure the connection to broker.hivemq.com.

    MQTT Explorer HiveMQ connection

  2. Click on the ADVANCED button. Add a subscription to oas_tags. Click on the BACK button.

    MQTT Explorer subscribe to oas_tags

  3. Click on the CONNECT button to open your connection.

  4. Any changes to your TemperatureSensor tag value should be published within 10 seconds. You can inspect the message in the side panel.

    MQTT Explorer oas_tags message received

  5. Click on the DISCONNECT button when you are done.

Save the configuration

Apply changes updates the running configuration; it does not write it to disk. Click Save to write the tag configuration file, and set it as the default under Options so that it loads again when the service restarts.

Where next