The seamless integration of OT and IT is becoming increasingly important in the modern factory. In this context, the integration of Beckhoff TwinCAT to MQTT networks is also an increasingly frequent requirement. Thanks to their PC-based architecture and openness to various programming languages, TwinCAT systems are particularly suitable for flexible solutions in automation technology. MQTT, on the other hand, offers the event-based and scalable interface between OT and IT. Due to the popularity of Beckhoff TwinCAT and the increasing spread of MQTT, the need for seamless integration of both technologies is growing. Integration is also becoming increasingly important in the context of the Industrial Unified Namespace Architecture. The following step-by-step guide takes you through the process of integrating Beckhoff TwinCAT with MQTT.
Options for the integration of Beckhoff TwinCAT and MQTT
In practice, Beckhoff TwinCAT is connected to an MQTT broker in three ways: via a gateway, on the basis of direct communication using the integrated “Tc3_IotBase” library or via an OPC UA server. The selection of the appropriate option depends on the specific use-case requirements and the existing infrastructure.
1. Option: Integration via Gateway
One of the most common methods for integrating Beckhoff TwinCAT and MQTT is the use of a gateway (software or hardware gateway). The gateway serves as a bridge between the controller and the MQTT broker. Moreover it extracts the data from TwinCAT, converts it into MQTT messages and then publishes them in the MQTT broker. This method is particularly suitable for the integration of Beckhoff TwinCAT controllers that do not offer direct support for OPC UA or special MQTT libraries.
2. Option: Integration via Beckhoff “Tc3_IotBase” Library
Another effective method for integrating Beckhoff TwinCAT into MQTT networks is the use of the “Tc3_IotBase” library. This library enables direct communication between the TwinCAT controller and the MQTT broker without the need for an additional gateway. The controller uses the “Tc3_IotBase” library to format and send data directly as MQTT messages.
3. Option: Integration via OPC UA Server
If the Beckhoff controller supports the TwinCAT OPC UA server, integration with MQTT can take place on the basis of OPC UA. In practice, the connection of OPC UA and MQTT is realized in 2 ways: via a gateway or on the basis of direct communication using “OPC UA over MQTT”. Detailed information on this and step-by-step instructions can be found here.
Step-by-Step Integration of TwinCAT to MQTT via Gateway
The following steps must be completed in order to connect the controller to the MQTT broker using a gateway.
Step 1: Selecting the Gateway
Ensure that the gateway supports MQTT natively and provides your preferred interface for TwinCAT. Depending on the use-case requirements and the capabilities of the controller, various communication protocols can be used. In addition to OPC UA, the most common ones are:
-
- ADS (Automation Device Specification): ADS is Beckhoff’s own protocol for communication between Beckhoff controllers and software. While it enables direct access to TwinCAT system functionalities, including reading and writing PLC variables it also starts or stopps programs and diagnostic functions.
- Modbus: Modbus is an easy-to-implement communication protocol that is available in both TCP/IP and serial versions.
- EtherCAT: EtherCAT is mainly a network protocol for communication at field level. However, it can also be used to access control systems via third-party software, especially when real-time data exchange is required.
- TCP/IP: TCP/IP is a basic internet protocol and can be used for direct data exchange with TwinCAT controllers. TCP can have latency problems with high network loads, for example for user-defined communication solutions or for integration into IT networks.
Also make sure that the gateway offers the required security features. Depending on the use-case, the gateway should meet additional requirements (e.g. redundancy, scalability).
Step 2: Check the Prerequisites
Check the existing infrastructure and configure the necessary access rights to the systems to ensure reliable integration.
Check infrastructure: Check whether the TwinCAT controller and the MQTT broker are fully functional and set up a test environment. This allows you to test the integration and subsequent adjustments in a controlled environment before making changes in the production environment.
Configure the network: Make the necessary adjustments to your network topology. This includes the configuration of ports and firewalls to enable communication and at the same time prevent unauthorized access.
Access and authorizations: Make sure that you have access to the TwinCAT controller, the MQTT broker and the gateway. This also includes the necessary user rights and authorizations to be able to carry out configurations. After making sure, if necessary, install the corresponding configuration software for the systems.
Step 3: Identification of the relevant TwinCAT variables
Check your control system to ensure that the control system reliably provides the relevant variables. This also includes identifying the variables in the control system that are relevant for the use-case.
Identify TwinCAT variables: Determine which TwinCAT variables (data points such as sensor data, status information) are relevant for your use-case.
Check variable types and attributes: Check whether the variables are assigned the correct types (e.g. INT, REAL, BOOL) and attributes (e.g. address, length, access rights). Make sure that these are configured correctly in the control unit.
Step 4: Definition of the MQTT Namespace
In Beckhoff TwinCAT controllers, the namespace (address space) at the controller level is defined by the configuration management of symbolic addresses and variables. In contrast, the MQTT namespace is created in collaboration between publisher and subscriber. Adherence to a standardized structure is crucial for effective communication and data organization in MQTT. This step includes:
Standardization of the MQTT topic hierarchy: A topic is a UTF-8 string that specifies the path under which a message is published. Therefore clients can publish messages to these topics or subscribe to topics to receive messages. Also define guidelines for the naming of these topics, taking best practices into account, to ensure consistency.
Standardization of the MQTT message content (payload): The definition of a standardized payload is essential for interoperability and scalability, especially in the case of a heterogeneous machine park. To do this, create a data model that includes the following components:
- Uniform structure of the payload, including metadata (e.g. machine type and ID) and their representation within the MQTT topics
- Standardized data format, for example JSON or XML, to simplify data processing and integration
- Standardized data types and naming conventions to ensure the uniqueness and comprehensibility of the data.
Step 5: Mapping of TwinCAT Variables and MQTT Namespace
In this step, you establish a connection between the gateway, the TwinCAT controller and the MQTT broker. The gateway acts as a bridge between the two protocols and translates TwinCAT variables into standardized MQTT messages.
Connection test: Check the connectivity between the TwinCAT controller, the gateway and the MQTT broker. Pay particular attention to compliance with the security guidelines, including the correct setup of authentication and encryption methods.
Mapping of the MQTT payload: Map the relevant TwinCAT variables to the data model defined in step 4. If necessary, supplement the data model with static inputs (e.g. for metadata such as machine type and ID).
Mapping of the MQTT topics: Specify how the MQTT payloads are mapped to the MQTT topics. This can include simple 1:1 mappings; however, depending on the requirements of the use-case, require more complex transformations.
Implement transformation logic: Use the functionalities of your gateway to perform the required data conversions. This can include the scaling of measured values, the conversion of data types or the aggregation of data points.
Step 6: Integration of S7 and MQTT in Test Environment
Define message transmission: Specify when the TwinCAT data should be updated in MQTT. The update can take place either when changes are made to specific variables (best practice) or according to a predefined cycle (e.g. every second). Additionally you can also define important MQTT settings such as Quality of Service (QoS), Retain and Report by Exception.
Check data integrity: Ensure that the data in your test environment flows correctly from the controller via the gateway to the MQTT broker. Furtheremore check that all security mechanisms work as planned.
Step 7: Go-live and Monitoring
Go-live: After successful testing, you can push the TwinCAT to MQTT integration into production.
Set up monitoring: Implement monitoring and alerting features to continuously monitor system performance and data transmission integrity.
Logging and diagnostics: Ensure that detailed logs are available for troubleshooting and optimizing system performance.
Step-by-Step Guide to Integration via “Tc3_IotBase” Library
As the basic steps are the same, the specific steps for the Siemens S7 to MQTT integration using the “Tc3_IotBase” library are simplified below.
-
Check prerequisites:
In addition to the corresponding “TF6701 TC3 IoT Communication” license, the following prerequisites are required to implement the library.
- Availability of PLC programmers: Appropriate programming resources are required to implement and configure the library on the controller.
- Operating systems: Windows 7/10, Windows Embedded Standard 7, Windows CE 7, or TwinCAT/BSD.
- TwinCAT Version: At least TwinCAT 3.1 Build 4022.0 for MQTTv3 and TwinCAT 3.1 Build 4026.0 for MQTTv5.
- TwinCAT setup level: TwinCAT 3 XAE or XAR are required.
- Hardware: Compatible PC architecture (x86, x64, ARM).
-
Installation of Tc3_IotBase:
- TwinCAT 3 Setup: Install the TwinCAT 3 XAE or XAR setup, which contains the necessary drivers and the PLC library Tc3_IotBase.
- Installation via TwinCAT Package Manager: Use the TwinCAT Package Manager to install the Tc3_IotBase. This can be done using the command tcpkg install TF6701.IotCommunication.XAE.
-
Configuration of Tc3_IotBase:
- Project settings in TwinCAT: Open your TwinCAT project and add the Tc3_IotBase to your libraries.
- PLC programming: Create a new PLC program block in which you declare an instance of FB_IotMqttClient and the necessary auxiliary variables.
-
Set up the MQTT connection:
- Configure the FB_IotMqttClient: Set the MQTT client parameters, such as sHostName (the host name of the MQTT broker), nHostPort (the port of the broker) and authentication details if necessary.
- Configure the topic and the payload for the messages to be sent and received.
-
Send and receive messages:
-
- Test the connection to the broker: Firstly implement logic in your PLC program to connect to the broker, send and receive messages. Afterwards use the Execute call of the FB_IotMqttClient to initiate the connection.
- Testing the pub/sub functionality: Check that sending and receiving messages works as expected.
-
Note: The availability of PLC programmers and compatibility requirements may limit integration in a brownfield project. Therefore the suitability of the “Tc3_IotBase” library should be carefully considered.
Conclusion
The combination of Beckhoff TwinCAT and MQTT opens new possibilities for intelligent, connected factories. Therefore, choosing the right integration method is crucial for success. By implementing a sustainable strategy, companies can significantly improve efficiency, productivity, and competitiveness. Moreover, the standardized mapping of TwinCAT variables and the MQTT namespace across systems and factories plays a vital role. In existing plants (brownfield), this integration is typically achieved through a gateway, which facilitates the standardization and scaling of the MQTT namespace company-wide.