Expose MQTT telemetry to an OPC UA client
If a device already publishes measurements to an MQTT broker, use SPX to make those values readable by an OPC UA client such as SCADA. The MQTT adapter subscribes to one topic, decodes a JSON number and updates frequency_hz. The OPC UA server exposes that value as a read-only Double.
This example needs an MQTT broker, mosquitto_pub and an OPC UA client. On Linux, run SPX Server with Docker Engine and Compose v2. The broker must be reachable from SPX and the OPC UA port must be reachable from your client. SPX installation on Linux
Set up the gateway
- Import the MQTT to OPC UA gateway model in Models and create an instance. Use
mosquitto-serverasmqtt_broker_hostif both services share the same Docker network. Set the broker port from the SPX runtime's point of view. - Start the instance. Publish TCP port
4846from the SPX container for this example. If another instance already uses that port, setopcua_endpointto a free port and expose that port instead. - Connect your OPC UA client to
opc.tcp://localhost:4846/spx/mqtt-opcua-gatewayand browse toObjects → MqttGateway → FrequencyHz.
Build the gateway with an AI agent
Paste this prompt into an agent connected to your SPX workspace.
Prompt for your AI agent
Build an MQTT to OPC UA gateway in SPX. 1. Write mqtt_to_opcua.yaml. Under attributes, initialize frequency_hz to -1.0. Subscribe with mqtt to spx/examples/mqtt-opcua-gateway/telemetry/frequency_hz at QoS 1. Set payload_codec: json and attribute: "$in(frequency_hz)" in the subscribe binding. 2. Use opcua_server with namespace urn:spx:examples:mqtt-opcua-gateway and endpoint opc.tcp://0.0.0.0:4846/spx/mqtt-opcua-gateway, or use a free reachable port. SPX starts browsing under Objects automatically. Set the section's browse_path to [MqttGateway] and the node's browse_path to [FrequencyHz]. In the binding, use direction: outbound and read_attribute: "#attr(frequency_hz)" with node.data_type: Double. 3. Start the gateway. Publish 50.0 and then 49.25 with mosquitto_pub. After each publish, read the node in an OPC UA client. Try writing 47.5 and verify the server rejects the write. 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.0089 USD
In a local Docker lab, Luna generated the YAML, ran the gateway in SPX, and checked the result with protocol clients. The OPC UA client read the MQTT values 50.0 and 49.25 as Double. The server rejected a write to the node.
The estimate uses token usage for the successful build and test and Standard API rates.
Connect and observe
Publish two numeric JSON payloads. Use the broker address and port visible to your terminal.
mosquitto_pub -h localhost -p 1883 -q 1 -t spx/examples/mqtt-opcua-gateway/telemetry/frequency_hz -m 50.0
mosquitto_pub -h localhost -p 1883 -q 1 -t spx/examples/mqtt-opcua-gateway/telemetry/frequency_hz -m 49.25
Read FrequencyHz after each publish. In the local wire test, mosquitto_pub sent 50.0 and then 49.25, and an OPC UA client read both values as Double. A write of 47.5 through the OPC UA client returned BadUserAccessDenied. Stop the gateway instance when you finish.
Protocol communication
- MQTT input
- spx/examples/mqtt-opcua-gateway/telemetry/frequency_hz, JSON number, QoS 1
- OPC UA endpoint
- opc.tcp://localhost:4846/spx/mqtt-opcua-gateway
- Namespace
- urn:spx:examples:mqtt-opcua-gateway
- Node
- Objects → MqttGateway → FrequencyHz, read-only Double
The node starts at -1.0 until the first valid MQTT message. This local example uses anonymous OPC UA access with NoSecurity.
If you need OPC UA to MQTT, the source is an OPC UA node and the destination is an MQTT topic. To make a register readable from SCADA, follow the Modbus TCP to OPC UA gateway setup.
Why does the OPC UA node still show −1?
- Check the MQTT broker address and topic in the gateway instance.
- Publish after the gateway subscribes. Send a JSON number such as
49.25. - If the SPX attribute has changed, verify the OPC UA endpoint, namespace and browse path used by your client.
