Protocol gateways

Modbus TCP to OPC UA and MQTT Gateway

Expose one Modbus TCP reading to SCADA over OPC UA and publish it to MQTT. Run the gateway with SPX on Docker and test recovery after an outage.

Share one reading over MQTT and OPC UA

Use one Modbus TCP read to feed a telemetry service over MQTT and expose the same measurement to a SCADA client over OPC UA. In this 1:N gateway, both adapters read the same SPX attribute. When the meter stops responding, the OPC UA client can still read the last value while the MQTT subscriber sees no new sample. Follow the test below to see what happens when the meter comes back.

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.

OPC UA client

Reads a node exposed by SPX.

One Modbus reading feeds two independently updated outputs.

Install SPX with Modbus TCP, MQTT and OPC UA support. Use a reachable MQTT broker and clients for both outputs. Publish TCP port 4846 from the SPX container if you connect from the host.

On a Linux host, use Docker Engine and Compose v2 to run SPX Server. The gateway polls the Modbus device from the container, so verify that the device and broker are reachable from there. Use the published OPC UA endpoint when your SCADA client runs outside the container. SPX installation on Linux

Set up the gateway

  1. Start an instance of the Modbus frequency source. If you completed the Modbus TCP to MQTT gateway guide, reuse its source and stop its gateway if it publishes to the same topic.
  2. Import the Modbus TCP to OPC UA and MQTT gateway model and create an instance. Keep meter_host=127.0.0.1 when the source and gateway run in the same SPX runtime. Set mqtt_broker_host to an address reachable from SPX, such as mosquitto-server on the same Docker network. Set a free opcua_endpoint if the default port is occupied.
  3. Subscribe to MQTT with the command below, then start the gateway. Connect an OPC UA client to opc.tcp://localhost:4846/spx/energy-gateway and browse to Objects → EnergyGateway → FrequencyHz. Use the published ports and hostnames for your deployment.
mosquitto_sub -h localhost -p 1883 -q 1 -v -t spx/examples/energy-gateway/telemetry/frequency_hz

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 gateway with MQTT and OPC UA outputs in SPX. 1. Write meter.yaml. Simulate a meter on port 5023 (or another free port), Unit ID 1, with k__frequency_hz at zero-based input registers 3110–3111 as big-endian Float32. Start at 50.0 Hz. 2. Write modbus_to_opcua_and_mqtt.yaml. Poll the meter with modbus_master into frequency_hz. Publish a JSON number at QoS 1 to spx/examples/energy-gateway/telemetry/frequency_hz. Expose the same attribute as the read-only OPC UA Double Objects/EnergyGateway/FrequencyHz, using namespace urn:spx:examples:energy-gateway and endpoint opc.tcp://0.0.0.0:4846/spx/energy-gateway or a free reachable port. 3. Subscribe to MQTT before starting the gateway. Change the meter to 49.25 Hz and verify both clients. Call detach() on the meter's Modbus adapter, set 51.125 Hz and observe both clients. Call attach() and verify both clients see 51.125 Hz. Check that an OPC UA write is rejected. 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.0045 USD

In a local Docker lab, Luna generated the YAML, ran the gateway in SPX, and checked the result with protocol clients. Both clients saw 49.25 Hz. During the Modbus outage, OPC UA held that reading and MQTT delivered no new sample. Both clients saw 51.125 Hz after reconnection, and OPC UA rejected a write.

The estimate uses token usage for the successful build and test and Standard API rates.

Connect and observe

  1. In Instances → Attributes, set the source's Internal k__frequency_hz from 50.0 to 49.25 Hz. Confirm 49.25 in mosquitto_sub and read 49.25 from the OPC UA node. Try writing a different value through the OPC UA client and confirm that SPX rejects it.
  2. Call detach() on the source instance's communication.modbus_slave adapter and set the source to 51.125 Hz. Keep the gateway and both clients running. Watch for MQTT messages and read the OPC UA node again. Check the gateway's Modbus diagnostics for a failed read.
  3. Call attach() on the same adapter. Confirm that mosquitto_sub receives 51.125 and the OPC UA node returns 51.125. Modbus adapter controls

What the clients observed

Results from the local test with a simulated meter and separate MQTT and OPC UA clients
Modbus sourceMQTT subscriberOPC UA client
Before the outagePayload 49.25Read 49.25 Hz
While detached0 new messages in 1.422 sStill read 49.25 Hz
After reconnectingPayload 51.125Read 51.125 Hz

The OPC UA write attempt returned BadUserAccessDenied. During the outage, the successful OPC UA read returned a held value. The Modbus diagnostics showed that the source read had failed.

Stop the gateway and the source instances when you finish.

Protocol communication

Modbus input
TCP 5023, Unit ID 1, function 04, registers 3110–3111, big-endian Float32
MQTT output
spx/examples/energy-gateway/telemetry/frequency_hz, JSON number, QoS 1, retain disabled
OPC UA endpoint
opc.tcp://localhost:4846/spx/energy-gateway
OPC UA namespace
urn:spx:examples:energy-gateway
OPC UA node
Objects → EnergyGateway → FrequencyHz, read-only Double

The sample uses anonymous OPC UA access with NoSecurity for a local test environment.

Check the Modbus diagnostics for read failures, since the outputs carry only the reading. The example disables the Modbus binding after 100 read failures. Restore the source and restart the gateway if it reaches that threshold.

Use SPX as a Modbus TCP to OPC UA gateway

For a SCADA client that only needs OPC UA, omit the MQTT adapter block from the gateway YAML. Keep modbus_master as the input and opcua_server as the output. Verify the same register value at Objects → EnergyGateway → FrequencyHz. If your source already publishes MQTT, use the MQTT to OPC UA gateway guide.

Add destinations

Add an outbound binding for each destination in the model. For example, additional MQTT bindings can publish the same attribute to other topics, or multiple Modbus client bindings can send a command to different hosts, Unit IDs or registers. Validate each target individually, since one write can succeed while another fails.

If the adapters run in separate SPX instances, create one Connection per destination. In the Connections view, select the same source attribute and value slot for each From, then pick a different To. Start the instances and their connections and check each destination on its own protocol. Use separate command and feedback attributes for bidirectional routes. Check the supported adapter roles and backends before adding KNX/IP, BACnet/IP or other protocols. SPX Connections guide

Only one destination is updating?
  • If MQTT is quiet, subscribe to the exact topic and change the value again. With retain disabled, subscribing alone does not replay the previous sample.
  • If OPC UA is unreachable, check the mapped port and confirm that the namespace and browse path match the running gateway.
  • If neither output changes, check frequency_hz and the Modbus binding's read diagnostics before troubleshooting either client.