IMD SolutionComputing LibraryShop main store

Troubleshooting

Why an MQTT subscribe function block connects but receives nothing

Edge computing and IIoT Published 7 min read

Short answer

A connected, subscribed status proves only that the broker accepted the subscription. A message reaches your logic only when the subscribe function block is called every cycle, the filter matches the publisher's real topic character for character, and the code consumes the documented receive output. Prove each boundary with one controlled publish from an independent client.

On this page
  1. What does a connected and subscribed status actually prove?
  2. Is the subscribe function block really called every cycle?
  3. Does the topic filter match what the publisher sends?
  4. Which receive output should you monitor?
  5. Why does waiting for the cloud message prove nothing?
  6. How do you run a controlled end-to-end receive test?
  7. Questions asked

An MQTT subscribe function block that connects, subscribes and then stays silent is failing at one of three boundaries: the call, the topic filter or the receive interface. The broker's acknowledgment covers only the subscription request. A message reaches your application logic only when the function block executes on every cycle of its cyclic task, the subscription filter matches the publisher's real topic character for character, and your code consumes the receive output the installed library documents. This article proves those three in order, each with the same controlled test publish from an independent client. A client that never reaches the broker at all is a transport fault, traced in a PLC MQTT client that never reaches the broker, not a receive fault.

What does a connected and subscribed status actually prove? #

It proves only that the broker accepted the connection and the subscription request; it says nothing about whether a matching message was published, whether it was routed to your client, or whether your application decoded it into a usable variable. The status outputs sit at the start of the receive path, not the end of it, which is why a green connection and a green subscription coexist with a permanently empty receive buffer.

The receive path behind those status outputs has three boundaries, and the message must cross all three:

  • The call. The subscribe function block must execute on every cycle of its cyclic task, because the library hands received messages over only during that call.
  • The filter. The subscription filter must match the publisher's actual topic under MQTT matching rules, character for character, including every wildcard.
  • The receive interface. Your code must consume the receive data or callback the installed library documents, not a variable you assume carries the message.

Two neighbouring symptoms are different problems: a client that never reaches the broker is a transport fault, and an edge device with no subscribe function at all is a capability limit, covered in when an edge gateway has no MQTT subscribe function.

Is the subscribe function block really called every cycle? #

The library requirement is that the function block executes cyclically, because received messages are handed to your callback during that cyclic call; a cyclic task alone does not prove the call happens, so verify the call online before touching topics or brokers. Beckhoff's documentation for its MQTT communication sample states that messages are received via a callback method during the cyclic call of FB_IotMqttClient.Execute(), and the LMQTT library integrates the MQTT client in the function block LMQTT_Client for SIMATIC S7-1500 and S7-1200 controllers. In both designs the receive work happens inside the call, so a call that stops ends reception while the broker still shows a healthy session.

Work through the call path in this order:

  • Open the task configuration and confirm the task carrying the MQTT logic is enabled and cyclic.
  • Trace the program path from that task to the function block call. Look for conditional branches, state-machine steps or an early return that can skip the call once the subscribe step has succeeded.
  • Monitor the instance online and confirm, using the status and error output names from the installed library's documentation, that the call executes repeatedly with no active error.
  • Confirm the call continues after connection and subscription are established. A call inside a one-shot initialization branch subscribes once and then never receives again.

Proceed only when online monitoring shows the call executing every cycle with no active error; if it is skipped, restructure the logic so the cyclic task calls the function block for as long as the session must live.

Does the topic filter match what the publisher sends? #

MQTT matches the filter against the published topic character by character, so one wrong level, one capital letter or one trailing space keeps the session healthy and the receive output silent; compare the broker-observed topic with the configured filter before any other change. The rules that decide it:

  • Topic levels are separated by /, and matching is case-sensitive.
  • + stands for exactly one level; # stands for any number of levels and must occupy the whole last level of the filter, so plant1/#/config is not a valid filter.
  • A filter must match the whole topic, not a prefix of it.
  • A filter beginning with # or + never matches a topic beginning with $, which is how brokers keep their internal topics out of wildcard subscriptions.

The common cases, with example strings:

Topic the publisher sends (example) Filter the PLC holds (example) Match
plant1/line2/config plant1/line2/config Yes, the baseline test
plant1/line2/config plant1/line2/# Yes
plant1/line2/config plant1/+/config Yes
plant1/line2/oven4/config plant1/+/config No, + covers one level and this topic has four
Plant1/line2/config plant1/line2/config No, the first level differs in case

Run the comparison as a baseline test first: subscribe the PLC to one exact topic with no wildcards and publish to that same string. If the exact-topic test receives and the production filter does not, the wildcard placement or the publisher's real hierarchy is at fault. To see the real string, subscribe an independent client with mosquitto_sub on a wide filter and copy the topic it prints, character for character, including any tenant or device prefix a cloud service adds in front of the level you configured.

Which receive output should you monitor? #

Monitor the receive data or callback the installed library documents, never the variable you assume carries the message, because the documented interface differs between libraries and between versions; the documentation for your installed library is the only source for those names. One widely used client API documents MQTTClient.subscribe() with a callback that is called asynchronously if the subscribe completes. That callback reports subscription success, not message arrival, so watching it repeats what the status output already told you.

  • From the installed library's documentation, take the receive-related names: the data or buffer output, its data type, the indication that new data is present, and the status codes.
  • Monitor that documented output or buffer directly while a controlled test message is published, and record whether it changes.
  • Compare the test payload with what the parser expects: delimiters, encoding, field names and numeric representation.
  • Trace the received value into the application logic and look for conditions that reject, overwrite or reset it before it is used.

The observation decides the branch: if the documented receive indication changes but the application variable does not, the fault is parsing or downstream logic; if it never changes, go back to the call and the filter. One safety note belongs here: when received data feeds setpoints or commands, range-check it in logic before it reaches anything that moves the machine.

Why does waiting for the cloud message prove nothing? #

If the cloud republishes the configuration every 6 hours, a subscription started just after one publication can wait 21,600 s for the next one, so a 5-minute watch observes about 1.4 percent of the publish period and proves nothing either way. The arithmetic with those example inputs:

  • Publish period: 6 h × 3,600 s/h = 21,600 s between messages.
  • Average wait when subscribing at a random moment: half a period, 10,800 s.
  • A 5-minute watch: 5 × 60 = 300 s, which is 300 / 21,600 × 100 ≈ 1.4 % of the period.

Two broker rules close the trap: unless the publisher set the retained flag, the broker keeps nothing for a subscriber that arrives after the publish, and a retained message is delivered only when a new subscription matches. Commit to the controlled publish instead of waiting; at multi-hour publish periods, patience is not a test.

How do you run a controlled end-to-end receive test? #

Publish one message with a known topic and a known payload from an independent client such as mosquitto_pub while the PLC stays subscribed, then confirm it first at the documented receive output and then in the application variable, in that order. The sequence, one step per boundary:

  1. Confirm the cyclic task runs and the function block is called on every cycle, with no active error on the documented status outputs.
  2. Confirm the documented status shows a connected client and an accepted subscription for the intended filter.
  3. From an independent client on the same broker, publish one short, known payload to the exact subscribed topic, for example {"test": 123} at QoS 1.
  4. Watch the documented receive output; it must change on a cyclic call following the publish.
  5. Trace the value into the application variable and confirm it equals the test payload.
  6. Repeat with the production publisher and compare its actual topic and payload against the test that passed.

The result picks the fix:

  • Independent test passes, production traffic fails: the fault is the publisher's topic, payload or timing. Correct the filter or the publisher, not the PLC task.
  • Independent test never reaches the receive output: the fault is the call, the filter or the session; return to those checks.
  • Receive output changes but the application variable does not: the fault is parsing or downstream logic.

The receive path is proven only when a production publication arrives on the documented receive output and produces the intended value in application memory; a connection indicator alone has never proven it.

Questions asked

Why does the subscription drop when my test client connects?

MQTT allows one connection per client identifier. When a second client connects with the same identifier, the broker drops the first session, so a leftover test client or a second instance of your program silently disconnects the subscribed function block. Give the test client its own identifier and check where the installed library configures the PLC's client ID; two clients sharing one identifier look exactly like a subscription that receives nothing.

Does a QoS 1 publish prove the PLC received the message?

No. A QoS 1 acknowledgment travels from the broker back to the publisher and confirms the broker accepted the message, not that any subscriber received it. Delivery to the PLC is proven only when the documented receive output changes during the cyclic call. QoS 1 is still a reasonable choice for the test message because it removes broker-side doubt, but the pass criterion stays the function block output.

Should the test message be published as retained?

Publishing the test message with the retained flag set makes the broker redeliver it on every new matching subscription, which saves republishing each time you restart the PLC during debugging. Clear it afterwards by publishing an empty payload on the same topic with the retained flag; otherwise every later resubscribe replays the stale test value into your application logic.

Why would a short test payload arrive while the real cloud payload never does?

Payload length. The installed library documents a receive buffer, and a payload longer than that buffer is truncated or rejected at the function block before any parsing code runs. Repeat the test with a payload the same length as the production message; if the documented receive output stops changing at that length, the documented buffer size is the limit to design around.

Can a slow cyclic task make messages disappear?

A slow task delays delivery rather than discarding it. For a connected session the messages wait in the TCP connection and in the broker's in-flight window until the cyclic call processes them, so a long task interval shows up as data arriving late, which matters for values that must be fresh. Messages are lost only when a burst exceeds the receive buffer the installed library documents.

Keep reading

  • Troubleshooting

    Telegraf MQTT consumer connects but writes no data to InfluxDB

    An empty 0 / 10000 output buffer with both the MQTT and InfluxDB connections up means no metrics are reaching Telegraf's parser. Prove live delivery with a second subscriber on a different client ID before touching parser or buffer settings: the two startup connections are independent events, and with qos = 0 and a non-persistent session, traffic published while Telegraf is offline is unrecoverable.

    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

  • 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

Why historian tags don't prove a Galaxy object still exists