Explainer
Subscribe to MQTT and write the value back to the PLC
Short answer
Yes. A cellular edge gateway can read the PLC, publish telemetry to the broker, subscribe to a command topic and write the decoded value back to a PLC address. Keep the two directions logically separate, run the command topic at QoS 1 without retained messages, range-check and freshness-check every received value, and test the four boundaries before PLC logic acts on a command.

On this page
- Can an edge gateway subscribe to MQTT and write the value back to the PLC?
- Why is the write-back a control path and not just another subscription?
- What must you define before writing any gateway code?
- Which QoS and retained-message settings does the command topic need?
- How do you test the four boundaries one at a time?
- What checks belong between the received payload and the PLC write?
- What happens when the 4G link or the broker drops mid-loop?
- Questions asked
Yes, the scenario works on a cellular edge gateway: it reads the PLC, publishes the values to your broker, subscribes to a return topic, decodes what arrives and writes it back to a PLC address, and the PLC logic then acts on it. What separates that loop from a demo you switch off after a week is structure. Keep the outbound and inbound paths logically separate, treat the inbound topic as a control path with defined ranges and a defined fail-safe, and test the four boundaries — PLC to gateway, gateway to cloud, cloud to gateway, gateway to PLC — one at a time before any PLC logic acts on a received value. The sections below walk the loop in the order you build it: the control-path decision, the definitions, the MQTT settings, the boundary tests, the payload checks and the drop-out behavior.
Can an edge gateway subscribe to MQTT and write the value back to the PLC? #
Yes — read the PLC, publish to your broker over the cellular link, subscribe to a return topic, decode the payload and write it to a PLC address, and close the loop only after each of the four boundaries passes a test on its own.
The broker is a relay and nothing more: it forwards every published message to each current subscriber of the topic and holds nothing extra unless a message is marked retained, which is why one gateway can play both roles — telemetry publisher and command subscriber — on a single connection. If you want that relay behavior spelled out before you design the topic tree, the page on how MQTT clients publish and subscribe through the broker covers it in one place.
Two capabilities decide whether your hardware can close the loop: a subscribe function, not just publish, and a way to write into the PLC's own protocol through a driver or a script. A publish-only box cannot close the loop at any topic setting, which is the situation described in when an edge gateway has no MQTT subscribe function. If that is the box you have, the loop's host is the part to replace: the same subscriber, decoder and write logic runs on an industrial mini PC installed in the cabinet, writing to the PLC through its own protocol drivers.
Why is the write-back a control path and not just another subscription? #
Because the moment a cloud value can change what the PLC does, the inbound topic is a control path: it needs named permitted values, named destination addresses and PLC-side acceptance conditions, exactly as IEC 62443 treats a connection between zones — a conduit with declared data flows.
Telemetry that is wrong costs a bad trend; a command that is wrong moves a setpoint, changes a mode or starts something. That difference is why each direction gets its own declared, bounded set of flows: outbound carries only telemetry from defined tags, inbound carries only commands of a defined shape, from an authenticated publisher, to defined write addresses. The Node-RED MQTT setup that keeps publish and control separate applies the same split one layer down, at the topic level, and the broker's access rules should enforce it too: the telemetry credential must not be able to publish to the command topic.
One rule belongs in every design review of this loop: it never carries a safety function. The emergency stop and the guard interlocks stay wired to the safety relay, whatever arrives over MQTT.
What must you define before writing any gateway code? #
Seven definitions cover it: the PLC interface with its read and write addresses, the broker address with TLS on port 8883, the two topic names — one per direction — the payload schema, the field-to-tag mapping, the acceptance range per value, and the fail-safe behavior on a missing or invalid command.
- PLC interface and addresses. Read tags for telemetry and a separate, deliberate set of write addresses for commands. Never write to a tag the PLC treats as internal state.
- Broker and transport. TLS on port 8883, the registered port for MQTT over TLS, with the broker's CA certificate loaded on the gateway. Plaintext MQTT on port 1883 carries telemetry at most; it never carries commands into a control network. TLS authenticates the pipe, not the sender — broker credentials and access rules decide who may publish a command. If you are still deciding where the broker itself lives, running an MQTT broker in the PLC or on a separate device compares the placements.
- Topics. Two distinct names, one per direction, for example
site1/line2/telemetryoutbound andsite1/line2/cmdinbound, chosen so no wildcard subscription can pick up the wrong direction. - Payload schema. A command example:
{"id": 1042, "tag": "temp_sp", "value": 4750, "unit": "degC", "ts": 1718352000}— an identifier, the target tag, the raw value, the unit and the send time as a Unix timestamp. - Mapping. One row per command: inbound tag name to gateway variable to PLC write address, and nothing else writable.
- Acceptance range. For example 0 to 600 °C for that setpoint tag; anything outside the window is a rejected message.
- Fail-safe. What the PLC does when the command stream stops or a value fails its checks, described at the end of this article.
Which QoS and retained-message settings does the command topic need? #
Run the command topic at QoS 1 with retained messages off: QoS 1 re-delivers a command the broker or the gateway missed, and keeping the retained flag off stops the broker from handing a weeks-old command to the gateway the instant it reconnects.
MQTT 3.1.1 defines three delivery levels. QoS 0 sends once and forgets — a command lost in a coverage gap never comes back. QoS 1 acknowledges and retries until the gateway confirms, at the cost of possible duplicates. QoS 2 adds a four-way handshake to every message to guarantee exactly-once delivery, which a setpoint write does not need: writing the same validated setpoint twice leaves the tag unchanged, so a QoS 1 duplicate costs nothing. Telemetry can stay at QoS 0 or 1; the command topic gets QoS 1.
The retained flag is the trap to check. A publisher that sets it makes the broker store the message and deliver it to every future subscriber the moment that subscriber connects — documented broker behavior, and on a command topic it means a setpoint from the last shift fires at reconnect. Publish commands with the retained flag off. If a publisher you do not control has already left a retained message on the topic, erase it by publishing a zero-byte payload with the retained flag set, which the specification defines as the clear.
How do you test the four boundaries one at a time? #
Test each boundary with one known value and one place to look, in this order — PLC to gateway, gateway to cloud, cloud to gateway, gateway to PLC — so a failure names its own link.
| Boundary | What you send | Where you look | Pass condition |
|---|---|---|---|
| PLC to gateway | one live telemetry tag | the gateway's publish log | the published value equals the tag value |
| Gateway to cloud | that same published message | the broker's message inspector | the payload arrives unchanged |
| Cloud to gateway | one test command of 250.0 °C | the gateway's subscription log | the payload arrives complete and decodes |
| Gateway to PLC | the decoded 250.0 °C | the command's write tag in the PLC | the tag reads 250.0 °C |
Run the two inbound tests with the process in manual and on a tag that cannot start anything on its own. A subscriber that connects but receives nothing has its own short list of causes — wrong topic filter, wiped session, broker-side authorization — worked through in why an MQTT subscribe function block connects but receives nothing. Let PLC logic act on a received value only after all four rows pass in one continuous session, and then only through the acceptance conditions defined above.
What checks belong between the received payload and the PLC write? #
Decode, scale and range-check before any write: a payload of 4750 raw counts at 0.1 °C per count gives 4750 × 0.1 °C = 475.0 °C, inside the acceptance window, so the gateway writes it; 65535 counts gives 6553.5 °C and is rejected.
The checks run in a fixed order:
- Type. The value field is a number. A string or a missing field is a rejected message, never a zero.
- Scale and unit. Apply the scale factor once, then compare the declared unit with the tag's unit: the same digits declared as 475.0 °F convert to (475.0 - 32) × 5 / 9 = 246.1 °C, a different process condition, so a unit mismatch rejects the message too.
- Range. Outside the window: reject, log the raw payload, leave the tag alone. Clamping an out-of-range command toward the limit converts an error into a deliberate write at the limit, so the gateway never clamps.
- Freshness. Compare the payload timestamp with the gateway clock; older than the freshness window means stale, and stale means rejected.
A rejected message produces no write at all: the PLC keeps its last accepted value and the rejection lands in the gateway log with the payload attached, so you can see exactly what the cloud side sent. The decode-and-map mechanics — reading JSON fields into gateway tags and driving the write from them — are the same ones used when writing tags on an edge gateway from an MQTT JSON payload, pointed at a PLC write address instead of a local tag.
What happens when the 4G link or the broker drops mid-loop? #
The PLC owns the fail-safe: if no fresh, valid command has arrived within its freshness window — 60 s works for a supervisory setpoint — it reverts to its local setpoint and flags the link down instead of holding the last cloud value forever.
Two constraints size that window, and with them the choice stops being arbitrary: it must be longer than the loop's normal round trip, measured with a timestamped test command rather than guessed, and shorter than the time an unattended wrong setpoint can do damage. A slowly drifting supervisory value tolerates a window of minutes; anything driving faster behavior needs a tighter one. The direction of the decision is the point: the PLC never holds a cloud value indefinitely on the strength of a link that died an hour ago.
MQTT's own machinery then takes over in a defined order. QoS 1 re-delivers whatever went unacknowledged, so the newest command may arrive again seconds late; the freshness check accepts it while it is still inside the window and rejects it once it is not. On reconnect the gateway must re-subscribe, because a subscription lives in a session that a broker restart can wipe: a gateway that reconnects cleanly and then receives nothing is the symptom walked through in why an MQTT subscriber stops receiving after a broker restart. And a dead cellular link never takes the line down with it: the loop is supervisory, the PLC keeps its local mode, and telemetry simply pauses until the link returns.
Questions asked
Does the PLC itself need an MQTT client for this to work?
No. The gateway terminates MQTT in both directions and talks to the controller through the controller's own protocol, so it sees ordinary reads and writes on its normal interface. The controller needs a client only if you want it to speak MQTT directly, which is a different architecture with the broker living at the controller and its own trade-offs in scan load and wiring.
How fast does the loop run over a cellular link?
There is no fixed cycle time. Each round trip crosses the cellular network twice, passes through the broker twice and then waits for the controller's scan time, so the total varies with signal conditions. Measure your own round trip with a timestamped test command, and treat the loop as supervisory: it adjusts setpoints and modes, it does not replace millisecond interlocks.
What stops a duplicate command from being written twice?
QoS 1 can deliver a message more than once after a reconnect, so give each command a monotonically increasing identifier and have the gateway reject any identifier it has already processed. For a setpoint the write is idempotent anyway, but the identifier check also exposes a publisher stuck in a retry loop, sending the same old command over and over.
Can the gateway publish telemetry and receive commands on the same connection?
Yes, MQTT handles both directions on one connection with one client identifier. Keep separate topic namespaces for the two directions, and on a broker that supports access rules give each direction its own credentials, so the telemetry identity has no right to publish to the command topic. That way a compromised telemetry client cannot write to the controller.


