IMD SolutionComputing LibraryShop main store

Comparison

PLC MQTT direct publishing versus an IoT gateway

Edge computing and IIoT Published 9 min read

Short answer

For publishing process data to an MQTT broker for web monitoring, put the MQTT client in a gateway and leave the S7-1500 out of broker session logic. Keep the LMQTT_Client block in the PLC only when publication must be coordinated with control logic or the gateway cannot meet your topic, payload and delivery requirements. A green MQTT status proves the broker connection only, never the S7 data behind it.

On this page
  1. Two ways to hold the MQTT session
  2. Side by side
  3. What the green MQTT status proves, and what it does not
  4. Worked example: 20 tags at 1 Hz versus publishing every 10 ms scan
  5. Commissioning the gateway path
  6. Commissioning the direct LMQTT_Client path
  7. If the requirements outgrow the gateway
  8. The decision, case by case
  9. Questions asked

Publish from the gateway. For process data that a web dashboard reads, the S7-1500 should expose its values and nothing more: a gateway reads them over S7 communication, holds the MQTT session and publishes on a schedule, and your control program never has to survive a broker outage. Keep the LMQTT_Client block in the PLC only when publication must be timed by control logic, such as a batch step or an alarm transition, or when the gateway cannot produce the topics, payload format and delivery behavior the application requires. Whichever device holds the session, one rule governs diagnostics: a green MQTT status proves the connection to the broker and nothing about the data behind it. The sections below compare the two architectures, work the publish-load arithmetic, and end with the decision for each case.

Two ways to hold the MQTT session #

Direct publishing means the controller program is the MQTT client. Siemens supplies the LMQTT library for SIMATIC S7-1500 and S7-1200 CPUs: one function block, LMQTT_Client, documented in support entry 109748872 with a manual and project examples. Your program calls that block, sequences the connection, assembles the payload and requests each publish. Community libraries built for unsecured brokers also exist; they move process data in cleartext and hand you code the vendor does not maintain, so build on the documented block.

A gateway means an external device is the MQTT client. It reads the controller over S7 communication, the read service an S7-1500 serves on its integrated Ethernet interfaces, and publishes through its own client on its own schedule. The path is therefore two independent sessions: controller to gateway, gateway to broker. Your PLC project contains data, not protocol, and that split drives everything in the comparison below. One boundary matters for planning: a gateway that speaks only S7 and MQTT serves a web dashboard, but it will not grow into OPC UA later. That takes a different device, covered near the end.

Side by side #

Decision area LMQTT_Client in the S7-1500 Gateway reading S7, publishing MQTT
Who runs the MQTT session Your program: connect, publish and retry states around the block The gateway's firmware; the PLC never sees the broker
PLC project work The block, a state machine and a payload buffer, maintained for the life of the machine Expose data over S7 communication; no protocol code in the project
When a value publishes Exactly when your logic decides: on change, on event, in a defined order When the gateway's poll and publish schedule decides
Topic or payload changes A PLC project change and download An edit in gateway configuration, not the controller project
Failure behavior Broker outage becomes an error state inside the control program S7 read and MQTT publish fail and recover independently
Skills and lifecycle Automation staff own protocol code in TIA Portal One more device: configuration backup, firmware, power, spare

Read the table against your goal. A dashboard wants scheduled, periodic data, so rows two and four decide it: the gateway removes protocol code from the controller and keeps every future topic or field rename out of the PLC project. Direct publishing wins one row outright: no external device can time a publication more precisely than the logic that produced the value. If nothing in the application needs that timing, the gateway is the simpler machine to keep alive.

What the green MQTT status proves, and what it does not #

Because a gateway holds two sessions, its indicators describe two different things. The MQTT status proves one thing only: a session to the broker is up. It says nothing about the S7 side, whether the read succeeds, whether items map to the addresses you intended, or whether a controller that stopped answering is feeding the publisher stale data. Commission and diagnose the two sides separately:

  1. Configure the gateway's S7 station with the controller's address per the gateway manual, and make sure the CPU's protection settings permit that read access, restricted to the gateway rather than open to the network.
  2. Create the data items and read them in the gateway's own diagnostics before any publishing is enabled.
  3. Force or safely simulate a change at the source, where machinery cannot move, and expect the new value, unit and field name to appear at the publisher input.
  4. Only then configure the broker endpoint, credentials and transport security, enable publishing, and check that a published message carries the identical value the diagnostics showed.

If a dashboard freezes months later, start at step two, not at the broker: the green light stays green while the read has already failed.

Worked example: 20 tags at 1 Hz versus publishing every 10 ms scan #

The inputs, each with its source: 20 process values, the scope of a first-cut web dashboard; a 300-byte JSON payload, an illustrative assumption of roughly 15 bytes per field name plus value, not a measured size; a 10 ms scan cycle, a typical machine-control loop, which you should replace with the cycle time your controller's diagnostics report; and a 1 s design interval, what a human-facing dashboard actually reads. These are illustrative inputs, so substitute your own and the arithmetic rescales.

The rule in words: publishes per second equal one divided by the publish interval. Daily messages equal publishes per second times 86,400 seconds per day. Link traffic equals publishes per second times payload bytes, multiplied by eight to get bits.

Every scan. 1 ÷ 0.010 s = 100 publishes per second. Daily count: 100 × 86,400 = 8,640,000 messages per day. Traffic: 100 × 300 B = 30,000 B/s, which is 30,000 × 8 = 240,000 bit/s ≈ 240 kbit/s, and 30,000 B/s × 86,400 s ≈ 2.6 GB per day.

At 1 s. 1 ÷ 1 s = 1 publish per second. Daily count: 1 × 86,400 = 86,400 messages per day. Traffic: 300 B/s × 8 = 2,400 bit/s ≈ 2.4 kbit/s, and 300 B/s × 86,400 s = 25,920,000 B ≈ 26 MB per day.

The result: publishing on the scan costs 100 times the messages and the bandwidth for data the dashboard reads at most once per second. On the direct path it also cannot work as written, because your state machine must let one publish finish before the next starts, and each publish waits on a network round trip and a broker acknowledgement that a 10 ms scan will not wait for. Requests pile up or get skipped.

The decision, derived from these inputs: if the consumer refreshes at 1 s, any publish rate above 1 Hz is overhead you pay per message, per kilobit and per scan. Publish on a timer or on change of value, never on the scan, whichever architecture you picked.

Commissioning the gateway path #

  1. Write the data contract first: for each value, its controller address, engineering unit, valid range, topic, payload field and publish trigger. Without it, a successful broker connection still delivers ambiguous data.
  2. Configure the S7 station and verify each item in the gateway diagnostics, following the four steps in the status section above.
  3. Bind each confirmed item to its topic and payload field. Gateways that follow the tag-publishing model do this per tag on a schedule, the same pattern as configuring tag publishing to a broker in SCADA-side software.
  4. Point the publisher at a broker that authenticates and encrypts. If none exists yet, stand one up on a lab machine and prove TLS and per-client access rules first, as walked through in Run a secure Mosquitto MQTT lab broker with Node-RED.
  5. Set the publish schedule from the arithmetic above: a fixed interval of 1 s or longer, or change-of-value if the gateway supports it.
  6. Enable publishing and watch both statuses. Acceptance: a live S7 value appears in diagnostics first, then the same value leaves in a published message.

A gateway simplifies the PLC while adding an independent configuration database, a firmware lifecycle and a power dependency. Back up its configuration under the same change control as the PLC project, because a dead gateway with an undocumented mapping is a longer outage than the PLC code ever was.

Commissioning the direct LMQTT_Client path #

Direct publishing is more PLC code, and the code has a shape:

  • Build a small state machine around LMQTT_Client: idle, connecting, ready, publishing, retrying. Stay in connecting until the block reports completion or error, and enter ready only when the session is usable.
  • Request one operation at a time and wait for the block's completion before the next. Call the block every scan so its inputs never go stale: a one-scan command pulse can be missed, and a command held forever re-triggers a finished operation. Follow the semantics in the library manual rather than assuming generic busy and done behavior.
  • Snapshot the published values into a buffer, then serialize the buffer. Payload assembly must never read and rewrite the same variables the machine control uses.
  • On error, capture the block's diagnostic output and retry with a delay. Process control stays independent of broker availability: a broker outage must never stop the machine.

Respect the controller's own ceilings too. Direct publishing consumes controller connection and memory resources, and a controller family's MQTT broker and topic limits are numbers to compare against your tag list before you write the project, not surprises to meet at commissioning. The acceptance test is then identical to the gateway's: a simulated source change reaches the broker as the intended value, unit and field name.

If the requirements outgrow the gateway #

Your gateway speaks S7 and MQTT, and for web monitoring that is enough. It is not a configuration switch that adds OPC UA later. When a consumer needs a browsable OPC UA tag model with quality and semantics, that job moves to a small industrial computer of its own: an edge PC running a bridge, following the pattern in Bridging OPC UA PLC data to MQTT with an edge PC, on an industrial mini PC mounted in the cabinet beside the controller. Before you blame any architecture for a gap, write the requirements down: topic naming, payload format, per-value triggers, delivery guarantees (QoS 0, 1 or 2), retained behavior, and what the consumer shows when the chain is down. Whichever device cannot meet the written requirements loses.

The decision, case by case #

Web monitoring of process data: gateway. It takes MQTT session logic out of the controller, keeps topic and payload changes out of the PLC project, and separates two failure domains. Accept that you own one more device's configuration and firmware, and never read its green MQTT light as proof of good data.

Publication tied to control logic: direct publishing. If the instant a value becomes publishable is decided by machine logic, a batch step change or an alarm transition with time logic behind it, the PLC must be the publisher, because only the program knows that instant. Write the state machine, buffer the payload, and keep the retry loop out of process control.

The gateway cannot meet a written requirement: direct publishing or a different device. Topic scheme, payload format, per-value triggers, delivery guarantees: if the box cannot produce them, replace the requirement or replace the device. Do not ship a compromise nobody specified.

Alerts only: decide this before anything else. If the real need is threshold alarms with time limits rather than trend data, the pipeline shrinks to a few published states at a slow rate, and the tag-list machinery is unnecessary.

Whichever path you take, the acceptance test is identical: change a value at the source, safely, and watch the same value arrive at the broker with the right unit and field name. Green lights do not prove data; data proves data.

Questions asked

Which CPUs can run the LMQTT_Client block?

The LMQTT library provides a single MQTT client function block, LMQTT_Client, for SIMATIC S7-1500 and S7-1200 CPUs; Siemens documents it in support entry 109748872 with a manual and project examples. Firmware requirements apply, so confirm your exact CPU and firmware in that entry before you plan the project. Whichever CPU you use, plan a state machine around the block that finishes one connection or publish request before it issues the next.

Can a gateway publish only when a value changes?

It depends on the gateway, and the manual answers it before the purchase order does. Many gateways offer change-of-value, deadband or per-tag triggers; others only poll a fixed list on a fixed period and republish the whole set. If event-driven publication is a hard requirement and the gateway cannot meet it, that is exactly the case where direct publishing in the PLC earns its complexity.

Do retained messages make the broker a small historian?

No. A retained message stores exactly one value per topic, the last one published, and nothing before it, so a restarting dashboard gets the last value, not a trend. The broker forwards and forgets; it is a message intermediary, not storage. If reports or post-mortems need history, put an edge store or historian on the subscriber side instead of stretching the retained feature into a database.

The gateway shows MQTT connected but dashboard values stopped moving. Where is the fault?

Start on the S7 side, because the green MQTT status proves only the broker session. Check the gateway's acquisition status: a controller that stopped answering, a changed address, or a protection setting that now blocks the read all leave the publisher retransmitting stale values. Then give the web application a staleness timeout: when no message arrives within the publish interval plus a small margin, display no data rather than the last value.

Is MQTT still the right protocol if a SCADA host becomes the consumer?

Compare before you commit. OPC UA gives a SCADA host a browsable tag model with quality and semantics; MQTT with a payload convention such as Sparkplug adds birth, death and rebirth messages over a broker. A JSON feed written for a web dashboard rarely carries the state and quality a SCADA host expects. Decide the consumer first, then the protocol, not the reverse.

Keep reading

  • How-to

    Sending PLC data to an IoT hub without custom software

    You do not need custom software to move PLC data to an IoT hub: a configurable edge package with an S7, Modbus TCP or EtherNet/IP driver, a channel mapping and an MQTT publisher replaces hand-written acquisition code. Verify two things before you commit: the driver covers the protocol your controller actually speaks, and the publisher covers the destination's authentication and message format. Software still does the work — you configure it instead of writing it.

    Edge computing and IIoT

  • How-to

    Bridging OPC UA PLC data to MQTT with an edge PC

    Keep the OPC UA server on the PLC side and give MQTT publishing its own role on a separate edge PC that acts as the OPC UA client and MQTT publisher. Commission the two links independently — first a verified tag read, then a verified subscriber message — and only then widen the tag set.

    Edge computing and IIoT

  • Troubleshooting

    Fix MQTT sensor data missing in Node-RED

    A green MQTT connection in Node-RED proves only a broker session, not that the GSM module published a value or that the flow subscribes to its topic. An http in node cannot receive MQTT publications at all: use an mqtt in node. Trace RS-485 acquisition, publish result, broker identity and topic match in order, and fix the first stage with no positive result.

    Edge computing and IIoT

Selecting MQTT I/O hardware for a single sensor signal