MQTT
This quick start tutorial demonstrates how to set up a PLC project, OPC UA RT device and a publisher that sends a data set containing one variable via MQTT. The variable is linked to a variable from the PLC project via the process image.
Preparing the PLC project
Prepare a PLC project that defines one variable with data type INT, e.g.:
PROGRAM MAIN
VAR
nCounter : INT;
END_VAR
nCounter := nCounter + 1;
Please note that you do not need to set any input/output compiler statement because we will not use the process image to link the variable. Activate this configuration and start the PLC program.
Configuring a publisher
Please perform the following configuration steps to set up an OPC UA Publisher device:
- 1. Make sure that TwinCAT is in config mode.
- 2. In Solution Explorer, expand the I/O node and select the child item Devices.
- 3. Right-click Devices and select Add new element...
- 4. Expand the category OPC UA, select the Real-Time OPC UA Device and click on Ok.
- 5. Double-click the added device and navigate to the tab Adapter.
- 6. Bind the device to one of your Network Interface Cards. Make sure that you have installed the TwinCAT-Intel PCI Ethernet Adapter driver on that NIC.
- 7. Right-click the device and select Add new element...
- 8. Select the entry OPC UA Publisher (Module) and click on Ok.
- 9. To configure the added Publisher node, simply double click it in the tree view. There are many configuration options on the Publisher, e.g. the transport to use (UDP, MQTT), the data format (binary, JSON), optional header fields and so on. We want to leave these settings on their default values for the time being and only change the transport setting to MQTT/JSON.
- 10. In the next step you want to configure a so-called Dataset in order to define variables that should be send out by the Publisher. Right-click the Publisher node and select Add new element....
- 11. Select the entry Published DataSet (Module) and click on Ok.
- 12. Double-click on the data set and open the Data Set fields tab.
- 13. Open the TwinCAT Target Browser, navigate to the MAIN program of your PLC Project and drag&drop the variable nCounter to the list of data set fields.
Activating the project
Activate the TwinCAT project by clicking the Activate button on the TwinCAT XAE toolbar. Make sure that you have selected the correct target device (we assume that you are using your local device) and that you are using the correct Real-Time settings so that you can activate the TwinCAT Runtime on your device. Please consult the regular TwinCAT documentation for more information about how to activate a TwinCAT configuration and switch TwinCAT into Run mode.
In this quick start tutorial, we have used MQTT as the transport protocol. You can now use any MQTT client that has access to the message broker to subscribe to the published data set. The following screenshot shows the command line tool mosquitto_sub.exe that has been subscribed to the data. This tool is part of the Mosquitto software application.
