#NR4V1: Combine S7 and MQTT

Application-Note: #NR4V1 : Combine S7 and MQTT

Related Documents:

  • Application-Note: #NR0V1 : Get Node-RED ready on my router
  • Application-Note: #NR1V1 : Hello World
  • https://nodered.org/docs/user-guide/
  • https://cookbook.nodered.org/mqtt/
  • https://flows.nodered.org/node/node-red-contrib-s7

This application describes how to communicate with an S7 and display the value in a Node-RED dashboard. The setpoint of an Value is pushed to MQTT Broker and also the current value of an variable is pushed to an MQTT Broker. Both values are then displayed by the Node-RED dashboard and read from the MQTT Broker.

For this application-note we provide an Example Flow for Node-RED which displays widgets and reads values from a S7-1200 PLC. Please consider that this application is running on the router and can be accessed via http or https://router-ip:1880/ui. You can access it also from outside through an Web2Go link from your mbCONNECT24 account.

Description 1 – Dashboard

This Dashboard displays widgets to visualize data and control setpoints. The setpoint "Room 1 Temperature" is send to an public MQTT Broker as the topic "RoomTemperature". This topic is then again read from the broker and displayed at the widget "Set Temperature". The value "temperature" is read from the S7-1200 PLC and send to the public MQTT Broker as the topic "CurrentTemperature". This topic is then again read from the broker and displayed at the widget "Current Temperature".

This example is very easy since it doesn't need any programmed code in a function node.

The following picture shows the flow and gives an impression on how it is organized.

Description 2 - Node-RED Flow

To communicate with an S7 PLC you need to choose an input or/and output node.

Description 3 - S7 input and output

Use input if you want to read values and use output if you want to write values of an S7 PLC.

Let's take a look deeper into parameter:

Description 4 - S7 ocnnection setup

IP Address should be your PLC.

  • S7-1200 and S7-1500 CPU access requires access using "Slot 1" and you must disable optimized block access (in TIA portal) for the blocks you are using. In addition, you must "Enable GET/PUT Access" in the S7 controller in TIA Portal. Doing so opens up the controller for other access by other applications as well, so be aware of the security implications when you doing this.

Define your variable in the tab "Variables".

Description 5 - variables setup

Syntax of S7 address.

Examples:

  • MR30 - MD30 as REAL
  • DB10,INT6 - DB10.DBW6 as INT
  • DB10,I6 -same as above
  • DB10,INT6.2 - DB10.DBW6 and DB10.DBW8 in an array with length 2
  • PIW30 - PIW30 as INT
  • DB10,S20.30 - String at offset 20 with length of 30 (actual array length 32 due to format of String type, length byte will be read/written)
  • DB10,S20.30.3 - Array of 3 strings at offset 20, each with length of 30 (actual array length 32 due to format of String type, length byte will be read/written)
  • DB10,C22.30 - Character array at offset 22 with length of 30 (best to not use this with strings as length byte is ignored)

To communicate with MQTT you need to choose an input or/and output node.

In this example we are using an public MQTT broker : broker.mqttdashboard.com. We are recommending to use this only for testing and tutorials, not for productive usage.

Description 6 - MQTT server setup

If you selected and setup an server address, go ahead with defining your topic name

Description 7 - MQTT topic setup

See below the Flow as a file to Import in your application.