Turn Modbus addresses into explicit point definitions
Modbus manuals may describe data as 40001, 30001, or hexadecimal offsets, while client libraries often expect a zero-based offset. The dangerous failure is not always a timeout: a connection can succeed while the software reads the neighboring register. Avoid embedding numbers such as 40001 throughout application code. Define every point with a device identifier, Unit ID, function code, starting offset, register count, data type, byte order, scaling rule, unit, and expected range.
Keep the address printed in the vendor manual separate from the offset passed to the library. Converting between them in one configuration loader is safer than asking every engineer to remember when to subtract one. Coils, discrete inputs, input registers, and holding registers should be distinguished explicitly through the function code. If firmware revisions use different maps, select a versioned map for each model or firmware family instead of accumulating conditional exceptions inside the decoder.
- Identity: site, gateway, device, Unit ID, and a stable point name.
- Read definition: function code, documented address, library offset, length, and polling group.
- Decode definition: int16, uint32, float32, bit field, string, byte order, and word order.
- Engineering meaning: scale, offset, unit, valid range, invalid sentinels, and expected update interval.
Decode types without hiding byte-order assumptions
Two 16-bit registers do not automatically form the intended 32-bit value. A device may use big-endian bytes with swapped register words, and some vendors apply different conventions to different points. Preserve the raw registers and pass them through a declarative decoder. Before commissioning, compare the result with the device display, the vendor's diagnostic tool, or a controlled input. Test negative values, zero, upper bounds, and values that change across the register boundary.
Scaling deserves more than an inline multiplication by 0.1. The point definition should retain the raw type, scale, offset, and engineering unit. If a PLC stores 253 to represent 25.3 °C, the normal MQTT value can be 25.3, while a controlled diagnostic topic or log retains the raw 253. For counters, define rollover behavior. For bit fields, name individual flags. For floating-point values, detect NaN and Infinity and mark them invalid rather than attempting to place unsupported values in JSON.
Expected ranges should influence quality, not silently rewrite measurements. Clamping an unexpectedly high temperature to the configured maximum hides sensor failures and incorrect decoding. Preserve the decoded value and mark it out_of_range so an application or alarm policy can decide what to do. Clamp only when a control specification explicitly requires it, and retain the original value and reason for traceability.
Publish value, time, and quality together
A payload containing only 25.3 is compact, but it cannot tell a consumer when the measurement was taken, whether it came from a cache, or whether the Modbus connection failed afterward. For enterprise data pipelines, use a stable JSON envelope containing at least a schema version, device ID, point name, value, unit, source timestamp, gateway timestamp, and quality. Use timezone-aware ISO 8601 timestamps or one consistent epoch convention, and distinguish device time from gateway receipt time.
The quality model does not need to reproduce every industrial automation standard, but it must support operational decisions. A practical design uses good, uncertain, and bad as primary states, with reasons such as timeout, crc_error, decode_error, stale, out_of_range, or device_exception. Never republish the previous value as good after a failed poll. If an application needs the last known value, label it stale and include its original measurement time and age. Likewise, do not overload null to mean a valid empty value, a failed read, and an uninitialized point; quality and reason should make those states distinct.
- good: the current poll, decoding, and plausibility checks all succeeded.
- uncertain: a usable value exists, but it is old, outside the expected range, or based on an unreliable clock.
- bad: the request timed out, returned a Modbus exception, failed decoding, or came from an offline device; value is usually null.
- sequence: a monotonic sequence number helps consumers detect duplicates, gaps, and gateway restarts.
Choose MQTT delivery behavior deliberately
Topics should represent a stable asset hierarchy, such as site/gateway/device/telemetry, rather than mutable display names or engineering units. One topic per point is easy to subscribe to and retain but creates more messages. Publishing a polling group as one message reduces overhead, although partial updates and per-point errors become more complicated. High-frequency telemetry, event data, and slow-changing status usually deserve separate publishing policies. Whichever structure is chosen, version the schema so fields can evolve safely.
QoS 1 is often appropriate for telemetry, but it provides at-least-once delivery, so consumers must tolerate duplicates by using a message ID, sequence number, or measurement timestamp. Retained messages are useful for current state but may be misleading for historical samples. A Last Will message can expose gateway connectivity. Offline buffering also needs explicit limits, replay order, and a policy for a full queue, such as dropping the oldest low-priority telemetry rather than exhausting gateway storage.
Verification should go beyond confirming that the broker received a message. Maintain replayable register fixtures and test address bases, byte and word order, negative values, disconnects, exception responses, stale data, and schema compatibility. Monitor poll latency, decoding failures, stale points, and the MQTT publish queue. When point maps, decoders, quality rules, and message schemas are versioned and tested together, a Modbus-to-MQTT bridge becomes a maintainable data pipeline instead of a fragile one-time connection.