Last Code Revision Date: 03-Sep-2025
This project connects to an OPC UA server, reads node values, and publishes each value as a raw payload to its own MQTT topic. Each tag gets its own topic, and the payload is the raw value (not JSON). The application is designed for reliability, automatic reconnection, and easy configuration.
Edit config.json
to set up OPC UA and MQTT connection details:
{
"opcua": {
"endpointUrl": "opc.tcp://Parrot:4840/OPCUA/SimulationServer",
"nodesToRead": [
{ "name": "Constant", "nodeId": "ns=3;i=1001" },
{ "name": "Counter", "nodeId": "ns=3;i=1002" }
// ... more nodes ...
],
"username": "", // Optional: for OPC UA authentication
"password": "" // Optional: for OPC UA authentication
},
"mqtt": {
"hostname": "mqtt://broker.hivemq.com",
"port": 1883,
"masterTopic": "opcua_data", // Used as a prefix for each tag's topic
"username": "", // Optional: for MQTT authentication
"password": "", // Optional: for MQTT authentication
"qos": 1, // Optional: 0, 1, or 2
"tls": false // Optional: true for secure connection
// "ca": "path/to/ca.crt", // Optional for TLS
// "cert": "path/to/client.crt", // Optional for TLS
// "key": "path/to/client.key" // Optional for TLS
}
}
npm install
node index.js
username
and password
in the mqtt
section of config.json
.username
and password
in the opcua
section and update the code to use these when creating the session.Each tag’s value is published as a raw payload to its own topic. The topic format is:
<masterTopic>/<tagName>
or, if no masterTopic is set:
<tagName>
The payload is the raw value of the tag, e.g.:
Topic: opcua_data/Constant
Payload: 42
Topic: opcua_data/Counter
Payload: 123
Each tag is published individually to its own topic, not as a JSON array.
34.03$