Siemens S7 to MQTT Integration (Step-by-Step Guide)

Content

In the modern factory, the combination of Siemens S7 controllers and MQTT is playing an increasingly decisive role in the seamless integration of OT and IT systems. In the modern factory, the combination of Siemens S7 controllers and MQTT is playing an increasingly decisive role in the seamless integration of OT and IT systems. S7 controllers are known for their reliability and performance in automation. MQTT, on the other hand, offers the event-based and scalable interface between OT and IT. Due to the popularity of Siemens controllers and the increasing spread of MQTT, the need for seamless integration of both technologies is growing. In addition, integration is becoming increasingly important in the context of the Industrial Unified Namespace Architecture. Therefore, the following step-by-step guide will take you through the process of integrating Siemens S7 with MQTT.

 

Options to integrate Siemens S7 and MQTT

A Siemens S7 controller can be connected to an MQTT broker in 3 ways: via a gateway, on the basis of direct communication using the integrated “LMQTT” library in the S7 or via an OPC UA server. The selection of the appropriate variant depends on the specific use-case requirements and the existing infrastructure.

 

1. Option: Integration via Gateway

One of the most common methods for Siemens S7 to MQTT integration is the use of a gateway (software or hardware gateway). The gateway acts as a bridge between the controller and the MQTT broker. The gateway reads the data from the S7 controller, transforms the data into MQTT messages and publishes these messages in the MQTT broker. This method is particularly suitable for the integration of S7 controllers that do not support OPC UA or the “LMQTT” library.

Siemens S7 to MQTT Integration via Gateway

 

2. Option: Integration via S7 „LMQTT“ Library

Another effective method for integrating S7 controllers into MQTT networks is to use the “LMQTT” library. This library enables direct communication between the S7 controller and the MQTT broker without the need for an additional gateway. The controller uses the “LMQTT” library to format and send data directly as MQTT messages.

Siemens S7 to MQTT Integration via LMQTT Library

 

3. Option: Integration via OPC UA Server

If the S7 controller supports an OPC UA server interface, the Siemens S7 to MQTT integration can be done on the basis of this interface. In practice, the integration 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 and step-by-step instructions can be found here.

 

Step-by-Step Guide to Integration via Gateway

The following steps must be completed in order to connect the controller to the MQTT broker using a gateway.

 

Step 1: Check the Prerequisites

Check the existing infrastructure and configure the necessary access rights to the systems to ensure reliable integration.

Select gateway: Ensure that your gateway natively supports S7 and MQTT protocols. Check compatibility with other relevant systems (e.g. controllers, ERP) in your OT/IT landscape. Ensure that the gateway offers the required security features. Depending on the use-case, the gateway should meet additional requirements (e.g. redundancy, scalability).

Check infrastructure: Check whether the controller and 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 controller, the MQTT broker and the gateway. This also includes the necessary user rights and authorizations to be able to carry out configurations. If necessary, install the corresponding configuration software for the systems.

 

Step 2: Identify the relevant S7 Data Blocks

Check your control system to ensure that the control system reliably provides the relevant data points (data blocks). This includes identifying the data blocks in the control system that are relevant for the use-case.

Identify S7 data blocks: Determine which S7 data blocks (data points such as sensor data, status information) are relevant for your use-case.

Check data block types and attributes: Check whether the correct types (e.g. DB, FB, FC) and attributes (e.g. start address, length, access rights) are assigned to the data blocks. Make sure that these are configured correctly in the control unit.

 

Step 3: Definition of the MQTT Namespace

In S7 controllers, the namespace (address space) is defined at the controller level, with the configuration of data blocks (DBs) playing a key role. 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. Clients can publish messages to these topics or subscribe to topics to receive messages. 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 4: Mapping of S7 Data Blocks to MQTT Namespace

In this step, you establish a connection between the gateway, the S7 controller and the MQTT broker. The gateway acts as a bridge between the two protocols and translates S7 data blocks into standardized MQTT messages.

Connection test: Check the connectivity between the S7 controller, gateway and the MQTT broker. Pay particular attention to compliance with the security guidelines. This includes the correct setup of authentication and encryption methods.

Mapping of the MQTT payload: Map the relevant data from the S7 data blocks to the data model defined in step 3. If necessary, complete 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 or, depending on the use-case requirements, 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.

Mapping of Siemens S7 Data Blocks and MQTT Namespace

 

Step 5: Integration of S7 and MQTT in test environment

Define message transmission: Specify when the S7 data should be updated in MQTT. The update can take place when changes are made to specific data blocks (best practice) or according to a predefined cycle (e.g. every second). 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 and that all security mechanisms work as planned.

 

Step 6: Go-live and Monitoring

Go-live: After successful testing, you can push the S7 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 LMQTT Library

As the basic steps are the same, the specific steps for the Siemens S7 to MQTT integration using the “LMQTT” library are simplified below.

1. Check prerequisites: The following prerequisites are required to implement the library on the control unit.

    • Availability of PLC programmers: Appropriate programming resources are required to implement and configure the library on the controller.
    • Hardware and firmware compatibility: Check whether the controller (e.g. S7-1200, S7-1500) provides the necessary resources for the library. For secure communication via TLS, at least firmware version 2.0 or 4.4 must be installed on the CPU (S7-1500 or S7-1200).
    • Programming environment: The use of the TIA Portal is required.
    • Network interface: The control unit must have an Ethernet interface to enable the exchange of messages via TCP/IP.

2. Create a TIA Portal project: Create a new project in the TIA Portal for S7-1200 or S7-1500 CPU. Configure the IP address of the CPU Ethernet interface so that it is in the same subnet as the MQTT broker.

3. Add the “LMQTT” library: Download the “LMQTT” library and add it to the project. Open the library in the “Global libraries” area and drag the “LMQTT_Client” FB into your project.

4. Create a global data block (DB): Create a new global DB for the TCP/MQTT connection parameters and the messages to be sent/received.

5. Integrate FB “LMQTT_Client”: Insert the FB “LMQTT_Client” into the main program (e.g. OB1) and link the required tags from the global DB.

6. Configure MQTT: If a secure connection (TLS) is required, import the certificate of the MQTT broker. Configure the FB to use the certificate.

7. Test S7 to MQTT integration: Set all necessary connection parameters in the DB, load the program onto the CPU and start the connection to the MQTT broker.

Note: Due to compatibility restrictions and the usually limited availability of PLC programmers, integration via the LMQTT library in the brownfield is often only suitable to a limited extent.

 

Conclusion

The combination of S7 controllers and MQTT opens up new possibilities for intelligent and connected factories. Choosing the right integration method is crucial for success. By implementing a sustainable integration strategy, companies can demonstrably increase efficiency, boost productivity and ultimately improve their competitiveness. The standardized mapping of S7 data blocks and the MQTT namespace across systems and factories is a key aspect of successful integration. In existing plants (brownfield), this is usually done via a gateway, which should support the standardization and scaling of the MQTT namespace throughout the entire company.

 

i-flow Industrial Unified Namespace (UNS)

About i-flow: At i-flow, we are dedicated to empowering manufacturers with the world’s most intuitive software to seamlessly connect factories at scale. Over 400 million data operations daily in production-critical environments not only demonstrate the scalability of the software, but also the deep trust our customers place in i-flow. Close cooperation with our customers and partners worldwide, including renowned Fortune 500 companies and industry leaders such as Bosch, Sto and Lenze, is at the heart of our business.

i-flow Industrial Unified Namespace (UNS)About the software: i-flow provides the central tools for the implementation and operation of a scalable Industrial Unified Namespace Architecture.

  1. Connectivity Layer: With more than 200 connectors, you can integrate common OT and IT systems into your UNS with just a few clicks.
  2. Harmonization Layer: The i-flow software harmonizes source data before it is made available in a central message broker.
  3. Message broker: i-flow comes with a fully integrated MQTT broker, but also supports existing brokers (e.g. MQTT, Kafka).
  4. Microservices: Combine, aggregate and transform OT and IT data via i-flow microservices, for example to calculate KPIs or realize complex integrations.

Try it out now - free trial version

Experience the unlimited possibilities that i-flow offers by taking a test for yourself. 30 days free trial, on your systems.

Your question has not been answered? Make a non-binding inquiry now.

Our data policy applies.