Last Code Revision Date: 03-Sep-2025
This project connects to an OPC UA server, reads values from specified nodes, and publishes their values as a JSON payload to a single MQTT topic. It is useful for integrating industrial data into IoT platforms via MQTT.
config.json
(server, broker, topic, nodes, interval).Install dependencies:
python -m pip install -r requirements.txt
Package | Version | Notes |
---|---|---|
asyncua | 1.1.6 | Async OPC UA client |
gmqtt | 0.7.0 | Async MQTT client |
aiofiles | 24.1.0 | Async file I/O |
aiosqlite | 0.21.0 | Async SQLite access |
cryptography | 45.0.6 | >42.0.0 — TLS & security |
pyopenssl | 25.1.0 | >23.2.0 — SSL/TLS support |
python-dateutil | 2.9.0.post0 | Date/time utilities |
pytz | 2025.2 | Timezone support |
sortedcontainers | 2.4.0 | Fast sorted collections |
typing-extensions | 4.15.0 | Typing enhancements |
cffi | 1.17.1 | ≥1.14 — C interface for Python |
pycparser | 2.22 | C parser for cffi |
six | 1.17.0 | ≥1.5 — Python 2/3 compatibility |
opcua | 0.98.13 | Synchronous OPC UA client |
paho-mqtt | 2.1.0 | MQTT client (sync) |
lxml | 6.0.1 | XML parsing (used by opcua) |
Edit config.json
to set your OPC UA server, MQTT broker, topic, nodes, and interval:
{
"opcua_server_url": "opc.tcp://DESKTOP-FAFB2HS:53532/OPCUA/SimulationServer",
"mqtt": {
"broker": "127.0.0.1",
"port": 1883,
"master_topic": "OPC_UA_Nodes"
},
"nodes": {
"Counter": "ns=3;i=1002",
"Random": "ns=3;i=1003",
"Sawtooth": "ns=3;i=1004"
},
"interval": 5,
"reconnect_delay": 5
}
Run the main script:
python main.py
config.json
.main.py
: Main script for OPC UA to MQTT bridge.config.json
: Configuration file for server, broker, nodes, and interval.requirements.txt
: Python dependencies.The main.py
script loads configuration from config.json
, connects to the OPC UA server and MQTT broker, reads values from configured OPC UA nodes, and publishes their values as a JSON object to a single MQTT topic. It supports both asynchronous and synchronous OPC UA/MQTT clients (as listed in requirements), and only publishes updates when node values change, reducing unnecessary traffic.
₹ 3,000.00