Connect to an MQTT Broker using Twin Activate

Publish and subscribe from a client to an IoT platform (external broker) using MQTT (Message Queuing Telemetry Transport) protocol.

Attention: Available only with Twin Activate commercial edition.

Prerequesites for This Tutorial

The following software and accounts are required for this tutorial:

Files for This Tutorial

MQTT_Publishing.scm, MQTT_Subscribing.scm

A finished version of the models you build in the tutorials along with any files required to complete the tutorials are available at this location: <installation_directory>/Tutorial Models/Integration and Collaboration/SmartWorks.

Overview

One of Twin Activate's communication protocol options is MQTT, a well-known communication protocol used in IoT (Internet of Things). It is possible to both publish and subscribe to any MQTT broker, local or external, very quickly and lightly.

When publishing messages to a topic, Twin Activate sends data from the software to the broker. When subscribed to a topic, Twin Activate receives data from the broker to the software.

In this tutorial, the external MQTT broker is on Altair SmartWorks. That is the end-to-end Altair IoT platform that covers all necessary technology for Smart and connected products development. In SmartWorks, it is possible to create Things, which represent the entities in your world. They store information about real-world objects like assets, rooms, or buildings, as well as information about concepts like Tasks or processes. These Things receive data if Twin Activate is publishing, and sends data if Twin Activate is subscribed to the MQTT topic. With the data on the IoT platform, you can create dashboards using that data: https://help.altair.com/smartworks/topics/mqtt_dashboard.htm?hl=mqtt.

General Setup on SmartWorks-IoT Platform

After you create an account on SmartWorks, create one Space to build the IoT project. The Thing is created there to send and receive data from Twin Activate. All the names regarding the Spaces, Things, and MQTT credentials should be different for each user.
  1. In SmartWorks, Create New Space > AnythingDB > Things > New Thing > Create.

    After the Thing is created, a property is created to receive data from Twin Activate and be stored on SmartWorks.

  2. Select Properties (+) > Create.
  3. Create Properties to be attributed to this object:
  4. To set the MQTT credentials on SmartWorks, select Interfaces > MQTT Create Credentials > Set Username and Password > Save.

Publishing to an MQTT Topic on SmartWorks

In this tutorial, a standard Twin Activate demo is used, but the process should be the same across any other diagrams. In this case, data is sent from Twin Activate to the IoT platform using MQTT PUB block, which publishes data to a broker using MQTT.
  1. In Twin Activate environment, select Palettes Browser > Communicate > MQTT PUB.
  2. In the block Connection tab set the Hostname, Credentials, and the Topic end point with the Space name, Thing ID, and, in this case, properties (not mandatory), following the structure:
    'spaces/<space-name>/things/<thing-id>/properties'
    Note: You can find the main topic structure in the URL href.
  3. Ensure the block Parameters tab has the Number of Inputs, in this case two (amplitude and time), and the Message. It is required to use JavaScript Object Notation (JSON) format to send data to SmartWorks:
    {
    	"obj1":{
    		"amplitude": "%%1",
    		"time": "%%2"
    	}
    }
  4. After doing the setup for the MQTT PUB block, connect some of the Signal Generators blocks to the inputs and a SampleClock to activate the block:
  5. Select Simulation > Setup > Real time scaling equal to 1. The simulation sends data in real time to SmartWorks.
  6. Click Run Simulation.
    It is possible to see that the data sent from Twin Activate is being received on SmartWorks using the MQTT protocol.

Subscribing to an MQTT Topic on SmartWorks

In this tutorial, a standard Twin Activate demo is used, but the process should be the same across any other diagrams. In this case, data will be sent from the IoT platform to Twin Activate. It will receive the data using MQTT SUB block, that subscribes to a broker using MQTT.
  1. In Twin Activate environment, select Palettes Browser > Communicate > MQTT PUB.
  2. In the block Connection tab set the Hostname, Credentials, and the Topic end point with the Space name, Thing ID, and, in this case, properties (not mandatory), following the structure:
    'spaces/<space-name>/things/<thing-id>/properties/<property-name>'
  3. Ensure the block Parameters tab has the Number of Outputs, in this case two (amplitude and time), and the Message. As it is being received from SmartWorks, select JSON format, and as the message will be received from one object, the format below must be respected to receive each of the attributes.
  4. After doing the setup for the MQTT SUB block, connect some of the Signal Viewers blocks to the outputs and a SampleClock to activate the block:
  5. Select Simulation > Setup > Real time scaling equal to 1. The simulation sends data in real time to SmartWorks.
  6. Click Run Simulation.
    After being subscribed to an MQTT topic, any message sent to the topic is received in Twin Activate. Say that some device is sending message to SmartWorks on the created object, on its attributes. Considering that Twin Activate is connected to SmartWorks, those messages appear in the software:

    It is possible to see that the data sent from SmartWorks is being received on Twin Activate using the MQTT protocol.