Send a Modbus TCP reading to MQTT
Suppose a monitoring service needs a frequency reading from a Modbus TCP energy meter. SPX polls the meter, decodes the register value and publishes it on one MQTT topic. This is a 1:1 route from one measurement to one configured destination. The broker can deliver that topic to multiple subscribers. The exercise uses a simulated meter so you can check the full path before connecting field hardware.
Modbus TCP meter
Frequency in input registers 3110–3111.
SPX gateway
Polls the meter and updates frequency_hz.
MQTT broker
Delivers the reading to subscribers.
Install SPX with Modbus TCP and MQTT support. You also need a reachable MQTT broker and a subscriber. The simulated meter and gateway can share one SPX runtime.
On Linux, run the Modbus TCP to MQTT gateway inside SPX Server on Docker Engine with Compose v2. SPX Setup generates the Compose configuration. Use the broker service name for containers on the same Docker network and the published broker port for subscribers on the host. SPX installation on Linux
Set up the gateway
Import the ready-made YAML files, or use the AI agent prompt to create them yourself.
- Import the frequency source model in Models and start an instance. It simulates a 50 Hz measurement on Modbus TCP port
5023, Unit ID1. - Import the Modbus TCP to MQTT gateway model and create an instance. Use
meter_host=127.0.0.1when the source shares the gateway runtime. Setmqtt_broker_hostto an address reachable from SPX, such asmosquitto-serveron the same Docker network. - Subscribe to
spx/examples/energy-gateway/telemetry/frequency_hz. A desktop subscriber typically connects tolocalhost:1883when the broker port is published there.
mosquitto_sub -h localhost -p 1883 -q 1 -v -t spx/examples/energy-gateway/telemetry/frequency_hz
Use the host port published by your broker. The -v flag prints the topic next to each payload.
Build the gateway with an AI agent
Paste this prompt into an agent connected to your SPX workspace.
Prompt for your AI agent
Build a Modbus TCP to MQTT gateway in SPX. 1. Write meter.yaml. Under attributes, declare k__frequency_hz: 50.0 as a scalar. Use modbus_slave on port 5023 (or another free port), Unit ID 1. In mapping, key the entry by k__frequency_hz and set address: [3110, 3111], group: i_r and type: float. 2. Write modbus_to_mqtt.yaml. Initialize frequency_hz to -1.0. In the modbus_master inbound binding, use area: i_r, address: 3110, length: 2, codec: float and bit_order: Big Endian. Set attribute to "$in(frequency_hz)" in both the Modbus binding and the MQTT publish binding. Publish a JSON number at QoS 1 to spx/examples/energy-gateway/telemetry/frequency_hz. 3. Start the meter, subscribe with mosquitto_sub -v, then start the gateway. Confirm 50.0 before setting k__frequency_hz to 49.25. Confirm 49.25 in the gateway attribute and on the same MQTT topic. Use an isolated SPX test workspace. Save the new YAML under custom_models/ and validate it before starting an instance. Use existing reachable services and free ports. Preserve existing models, instances and network settings. Return the YAML paths and the actual client output. If a prerequisite is missing, report which check you could not run.
Tested with GPT-6 Luna · Medium · Est. total 0.0035 USD
In a local Docker lab, Luna generated the YAML, ran the gateway in SPX, and checked the result with protocol clients. SPX and the MQTT subscriber both reported 50.0 Hz, then 49.25 Hz after the simulated meter changed.
The estimate uses token usage for the successful build and test and Standard API rates.
Connect and observe
- With the subscriber running, start the gateway and wait for the simulated meter's initial 50.0 Hz reading.
- In Instances, open the running source instance. Under Attributes, set the Internal value of
k__frequency_hzto 49.25 Hz. - Check
frequency_hzon the gateway instance. It should show 49.25 Hz, andmosquitto_subshould print a line ending in49.25on the same topic.
Verified from source to subscriber
The two matched readings below came from a local wire test with the downloadable models, SPX's Modbus TCP and MQTT adapters, and a separate Mosquitto broker.
SPX runtime
- Source
k__frequency_hz - 50.0 Hz → 49.25 Hz
- Gateway
frequency_hz - 50.0 Hz → 49.25 Hz
MQTT subscriber
spx/examples/energy-gateway/telemetry/frequency_hz
50.0
49.25If the gateway value stays unchanged, check its Modbus binding first. If the gateway updates and the subscriber sees no messages, check the broker address and topic, then change the source again. This model publishes on change with retain disabled.
Stop the gateway and the source instances when you finish. Follow the Modbus TCP to OPC UA and MQTT gateway guide to expose the same reading through OPC UA as well.
Protocol communication
- Modbus source
- TCP 5023, Unit ID 1
- Modbus read
- Function 04, zero-based addresses 3110–3111, big-endian Float32
- Shared attribute
- frequency_hz, in Hz
- MQTT output
- spx/examples/energy-gateway/telemetry/frequency_hz
- Payload
- JSON number, QoS 1, retain disabled
The gateway starts at -1.0 until its first successful Modbus read. With retain disabled, subscribers receive a new sample when the value changes.
To route a different signal, match the device's register area, address, data type and byte order. Bind the decoded value to an attribute and configure the output adapter to read it. Use model logic for unit conversion when required.
Can the MQTT–Modbus gateway run in the reverse direction?
For MQTT to Modbus TCP, bind an MQTT subscription to a command attribute and use modbus_master with an outbound binding to write to a remote device. If a PLC should poll SPX for the value, use modbus_slave to expose a register in SPX. For command flows, verify the destination through a separate Modbus readback. A broker acknowledgment covers delivery on MQTT.
No value or an unexpected value?
- If the gateway stays at
-1.0, check the Modbus host, port, Unit ID and read diagnostics. - If the SPX attribute updates and MQTT is quiet, check broker reachability and the exact topic, then change the source value again.
- If the value is implausible, confirm the register offset and Float32 word order against the device map.
