Siemens S7 to MQTT
PLCGateway reads SIMATIC S7 controllers over their Ethernet port and publishes data blocks, markers and I/O to an MQTT broker – no MQTT blocks in the PLC program, no extra hardware.
Publishing MQTT from the PLC itself means adding library blocks to the program, and on older S7-300 and S7-400 CPUs that is rarely practical. PLCGateway leaves the program untouched: it reads the CPU with the S7 protocol on port 102 and publishes the values from outside.
Nearby addresses are fetched in optimised multi-item requests and values are published when they change, so even large tag lists put little load on the CPU and the network. Values can be written back through MQTT when you allow it.
- Source
- Siemens S7
s7 - Connection
- ISO-on-TCP (RFC 1006) · TCP 102
- Target
- MQTT
- Writing
- yes
Set it up in five steps
In TIA Portal allow PUT/GET communication and switch off optimised block access for the data blocks to read (S7-1200 / S7-1500).
Add an S7 device with the IP address; rack 0, slot 1 for S7-1200/1500, rack 0, slot 2 for S7-300/400, or the TSAP for LOGO!.
Enter the tags with their addresses or import them from CSV and test the connection.
Open Gateway → MQTT, enter the broker address, port and login, switch on TLS if the broker requires it, and choose the base topic.
Choose publishing on change or at an interval and the JSON or plain-value format, then check the topics with mosquitto_sub or in your platform.
Example: topics and payloads
A device with the id press1 and these tags is published as shown below; the lines are what mosquitto_sub -v prints. Names and values are examples.
| Tag | Address | Example value |
|---|---|---|
Speed | DB1.DBD4 | 1450.5 rpm |
PartCount | DB1.DBW2 | 1284 |
Running | DB1.DBX0.3 | true |
Recipe | DB10.DBS20[32] | A-104 |
MQTT topics and payloads
plcgateway/status online
plcgateway/press1/Speed {"q":"GOOD","t":1794465246839,"u":"rpm","v":1450.5}
plcgateway/press1/PartCount {"q":"GOOD","t":1794465246839,"v":1284}
plcgateway/press1/Running {"q":"GOOD","t":1794465246839,"v":true}
plcgateway/press1/Recipe {"q":"GOOD","t":1794465246839,"v":"A-104"}
Topic: base topic / device id / tag name. The JSON payload carries the value (v), the quality (q), the timestamp in milliseconds (t) and the unit (u).
The MQTT publisher in PLCGateway
PLCGateway publishes every tag to your MQTT broker – Mosquitto, EMQX, HiveMQ, AWS IoT Core or a cloud platform behind a bridge – so IT systems subscribe to topics instead of polling the controller. It speaks MQTT 3.1.1 over TCP or TLS, with user name and password.
Values are sent when they change or at a fixed interval, as JSON with quality and timestamp or as the bare value. A retained status topic with a last will tells subscribers whether the gateway is online.
- One topic per tag: base topic / device / tag, with a base topic of your choice
- QoS 0 or 1, retained messages, birth message and last will
- Optional writing: a value published to the tag's /set topic is written to the controller
- Messages are queued while the broker is unreachable and sent after the reconnect
Controllers and devices
Equipment that is typically connected with this driver.
Questions and answers
Do I have to change the PLC program?
No. For S7-1200 and S7-1500 only two settings change in TIA Portal: PUT/GET access is allowed, and optimised block access is switched off for the data blocks that are read.
Does it work with S7-300 and S7-400?
Yes. These CPUs are read over their PROFINET port or an Ethernet CP with rack 0 and slot 2. LOGO! 0BA7 and LOGO! 8 work with the TSAP set in LOGO!Soft Comfort.
Can a subscriber write a value to the PLC?
Yes, when writing is enabled in the MQTT settings: a value published to the tag's /set topic is written to the data block, marker or output.
Connect your machines
Tell us which controllers you run; we show you PLCGateway with your own addresses.
Product and model names are trademarks of their owners. They are named only to show which equipment the drivers communicate with; PLCGateway is not affiliated with these companies.
