IMD SolutionComputing LibraryShop main store

Troubleshooting

Why an MQTT edge router sends incomplete JSON payloads

Edge computing and IIoT Published 8 min read By IMD Solution engineering team

Short answer

A subscriber that receives a grouped JSON object as two halves on consecutive publishes is seeing two separate MQTT messages, not one message torn by TCP or QoS: the transport reassembles whole messages, and QoS 0, 1 and 2 never change a message's content. Compare whole payloads, prove the split with the receive timestamps, then read the router's collection store at /var/modbus_db to see whether the missing keys arrived late or were absent before the send - that result decides whether to fix the grouping or the collection.

On this page
  1. Why QoS and the transport cannot split a JSON payload
  2. The period changes already ruled out
  3. Timing the 11:34:48 split against the send period
  4. Trace the payload through five boundaries
  5. Read the collection store before the send
  6. Map the segment count to the keys before changing it
  7. The decision for each case
  8. Questions asked

A subscriber that shows a grouped JSON object as two halves on two consecutive publishes is not looking at one message torn in transit. MQTT frames every message independently of the byte stream beneath it, so TCP segmentation and QoS 0, 1 or 2 can delay, duplicate or lose a message but never cut its content in two. When an edge router collects Modbus TCP data from a PLC line and publishes one grouped object every 10 s, two partial payloads arriving one send period apart mean the router built and sent two messages; the split was decided at the grouping boundary between collection and publish. Work the checks in order: prove the two-send pattern with arithmetic, trace five boundaries, then read the router's collection store to see whether the missing keys arrived late or were absent before the send.

Why QoS and the transport cannot split a JSON payload #

MQTT runs over TCP, but the two protocols do not share message boundaries. Every MQTT message carries its own fixed header and a remaining-length field, so the client library reassembles however many TCP packets the message needed and hands the application exactly one message. A subscriber therefore never receives half a message from the network: it receives whole messages, or it receives none.

QoS 0, 1 and 2 change only the delivery handshake - acknowledgment, retry and duplicate removal between publisher, broker and subscriber. They never touch the payload, which is why the QoS sweep in the affected installation could not have changed anything, and why any level is correct once the transport is cleared. QoS does shape timing: QoS 2 messages can stall under load and then arrive late, but no level turns two published objects back into one.

Packet size rules the split out as well. Published captures of MQTT traffic show single packets carrying payloads up to about 456 bytes - an observation from captured traffic, not a protocol limit - and a grouped object of a few dozen short numeric values sits in that same range. The whole object fits comfortably in one message; size is not what divides it.

The period changes already ruled out #

Changing when the router asks for data and when it sends cannot fix this, because the periods decide when a payload is built, not how many records go into it. The affected installation tested five arrangements, and the split survived each one:

Test Settings Result at the subscriber
QoS sweep QoS 0, then 1, then 2 Split unchanged
Faster collection Request period 5 s, send period unchanged A double message in each interval
Intermediate collection Request period 8 s, send period unchanged Split unchanged
Matched periods Request period matched to the send period Split unchanged
Slow test Both periods 60 s, Modbus timeout 10 s, number-of-timeouts setting 0 Split unchanged

The double message in the 5 s row is not automatically duplicated data: two collection cycles can return equal values on their own, so compare the collection timestamps inside the two payloads before calling anything a retransmission. The slow test carries the other lesson: with a 60 s cycle, a 10 s timeout and no tolerated timeouts, time was abundant and the payloads still split, so this is not a rush condition.

Other MQTT clients publishing Modbus data through the same broker delivered whole payloads throughout, which clears the broker and the subscriber tool and points the evidence at the publishing router's own grouping.

Timing the 11:34:48 split against the send period #

Inputs, each from the affected installation:

  • Subscriber log times: first half received at 11:34:48.076, second half at 11:34:58.083, first complete object at 11:35:08.127.
  • Send period: 10 s, from the router's Data to Server schedule.

The rule, in words: TCP reassembly happens below the MQTT client and takes milliseconds, so a second payload arriving a full send period after the first is a new send cycle, not a late fragment of one message. Subtract the receive times and compare each gap with the send period; a gap within tens of milliseconds of it means two publishes.

The arithmetic:

  • 11:34:58.083 - 11:34:48.076 = 10.007 s between the two halves.
  • 11:35:08.127 - 11:34:58.083 = 10.044 s from the second half to the complete object.

Result: both gaps equal one send period, to within 7 ms and 44 ms. The subscriber received three complete messages at three consecutive send times - half, half, whole.

Decision: a gap within tens of milliseconds of the send period means the router published twice because its grouping produced two objects; treat the case as a grouping fault and go to the collection store. A truncated object with no follow-up at the next send time would instead mean the formatter emitted one malformed message - still a sender-side fault. Nothing on the wire and no QoS level changes either outcome.

Trace the payload through five boundaries #

A connection or a transmission at one boundary proves nothing about the content at the next - the same rule as in why a gateway shows a TCP connection but sends no payload, where a live session carries no data at all. Treat the path as five boundaries and compare a different artifact at each:

  1. PLC response - per request: successful replies, exception codes, timeouts.
  2. Router collection store - records and their timestamps as stored, before any send.
  3. Data to Server grouping - which records the formatter selects for one payload: segment count, grouping and JSON formatting settings.
  4. MQTT publish - whole payloads captured per message, with topic and receive time.
  5. Subscriber display - the raw payload bytes as the subscribing client shows them.

Read the endpoint configuration - PLC address and port, broker address and port, topic, QoS - from the running configuration rather than from protocol defaults, and capture one normal interval alongside an affected one so the difference is reproducible. At the last two boundaries compare whole payloads and never packet captures alone: one payload may cross several TCP packets and still be one message.

Read the collection store before the send #

The router writes what it collected before it formats anything, and that store is the checkpoint that decides where the fault sits. Open an SSH session with an authorized account and go to /var/modbus_db. Copy the file first and inspect only the copy: the router writes to the store continuously, and a snapshot taken mid-write can show torn rows that never belonged to any send. Open the copy in a database browser that reads the file's format, and take the table and column names from the copy's own schema.

For one send whose payload arrived split:

  1. Note the send's arrival time at the subscriber.
  2. In the copy, list the stored records whose timestamps fall inside the collection window before that send.
  3. Check every key missing from the split payload against those records.

The result decides the case. Keys present in the store before the send: collection succeeded and the grouping selected only part of the records, so the fault is at the Data to Server boundary and the correction lives in the grouping settings. Keys absent, or stamped after the send: collection ran late or failed for those requests, so the fault is upstream - in the request period, the timeout and the PLC's response behavior, and what you must configure on the PLC side for Modbus TCP lists what the controller itself must accept before any router-side setting can help.

Map the segment count to the keys before changing it #

The affected installation set the segment count to 33 because the line has 33 Modbus devices and wants one grouped object, while the object must carry 34 keys, V1 through V34. The two counts describe different things, and the difference is exactly where a split hides: one device can return several values, several requests can serve one device, a collected record is not necessarily one JSON key, and the formatter can add keys of its own.

Build the map before touching the segment count:

  1. List every configured device and every enabled Modbus request.
  2. Map each key from V1 to V34 to the device, request and register that produce it.
  3. Mark any key the formatter adds independently of collected records.
  4. Compare the map against one complete captured payload and the stored records behind it.

Read what the segment count means for the installed firmware in the router's own documentation, because the behavior is defined by the vendor's implementation, not by Modbus or MQTT. Then keep two acceptance tests separate: the payload must parse as JSON, and the parsed object must contain every key in the map. A payload that parses but misses keys is a grouping failure; a payload that fails to parse is a formatting failure. The corrections differ, so the tests must stay separate.

The decision for each case #

Run the sequence once and the case lands in one of three buckets:

  1. Two partial payloads, one send period apart. The router built two messages, so the split is at the Data to Server grouping boundary. If the store held the missing keys before the send, correct the grouping so one send carries the whole mapped key set, then verify with a captured payload that parses and holds every key.
  2. Keys missing from the store. Collection failed or ran late. Measure the real sweep time from the first request to the last stored response before touching the request period, because a per-request timeout does not bound a sweep across dozens of devices; check the PLC side, then re-test with captured payloads.
  3. One truncated payload with no follow-up. The formatter emitted invalid JSON in a single message; correct the JSON formatting settings, not the periods.

Leave QoS wherever it is: every level delivers whole messages, and the sweep already proved it changes nothing here. And if the evidence ends at the hardware - a collection host that splits groups no matter what its configuration says - the durable fallback is to move the collection role to an industrial mini PC that runs the same Modbus TCP and MQTT stack on an operating system you control.

Questions asked

Does QoS 2 guarantee the subscriber receives complete JSON?

No. QoS levels govern acknowledgment, retry and duplicate removal between publisher, broker and subscriber; they never change what a message contains. Two partial payloads that arrive one send period apart are two separate messages the publisher built that way, so no QoS setting can reunite them. The correction sits in how the router groups its collected records, verified against the collection store - not in the broker, the topic or the QoS level.

Where does the router store the Modbus data it collected?

In the database file at /var/modbus_db on the router's own filesystem. Reach it over SSH with an authorized account, copy the file, and open the copy in a database browser that reads its format. Copy before opening: the router writes to that file continuously, and a snapshot taken mid-write can show torn rows that never belonged to any send. Read the table and column names from the copy's schema rather than assuming them.

Should the segment count equal the number of devices?

Only when every device contributes exactly one record. Count keys first: map each expected key, here V1 through V34, to the device, request and register that produce it, and include any keys the formatter inserts on its own. Set the grouping so a single send carries the whole map, then confirm with one captured payload that parses and contains every key. The device count alone is not the grouping rule.

How do I tell a retransmission from a genuine duplicate sample?

Compare the collection timestamps carried in the two payloads. Equal values with identical timestamps mean one stored record was sent twice; equal values with different timestamps mean two independent collection cycles genuinely returned similar data. Classify before changing any setting, because the causes differ: a publish-side retransmission points at the sending function, while two fresh samples simply reflect a collection rate faster than the send rate.

Keep reading

  • How-to

    MQTT to Modbus TCP with Node-RED on an edge gateway

    Yes: Node-RED with the node-red-contrib-modbus package can serve MQTT data to a SCADA client as Modbus TCP. Read the RTU device in Node-RED first, give the Modbus server node a port nothing else owns, and scale each MQTT payload to one integer per holding register before the first end-to-end test.

    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

  • Explainer

    Edge gateway Modbus TCP polling over the WAN port

    When a data gateway supports MQTT as a client only, drop the broker hop: configure the Modbus TCP poll to target the device's IP address in the same subnet as the gateway's WAN interface. The gateway opens the TCP connection itself, so polled values keep updating with the internet link down. Verify by pulling the uplink and watching one known register.

    Edge computing and IIoT

Redirect TCP 502 to 8502 on a Linux edge controller firewall