Gateway with Node-RED (Raspberry Pi)
Step-by-step guide to configure a Raspberry Pi with Node-RED as an OPC UA to MQTT gateway for Rela-ai.
What you need
- Raspberry Pi 4 (2GB+ RAM) or any Linux machine with Node.js
- Ethernet cable to connect to the PLC network
- Internet access (WiFi or a second ethernet cable)
- PLC details: IP address, OPC UA port, Node IDs to monitor
Step 1: Install Node-RED
If you are using Raspberry Pi OS, Node-RED installs with a single command:
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)Answer "y" to all prompts. When done:
# Start Node-RED as a service (starts automatically on boot)
sudo systemctl enable nodered
sudo systemctl start noderedOpen Node-RED in your browser: http://<raspberry-IP>:1880
Step 2: Install the OPC UA plugin
In the Node-RED interface, go to Menu (hamburger) → Manage Palette → Install and search for:
node-red-contrib-opcuaClick "Install". This adds the OPC UA nodes to the left panel.
Step 3: Create the OPC UA → MQTT flow
Drag these nodes onto the canvas and connect them in order:
3.1 "OPC-UA Client" node
- Endpoint:
opc.tcp://<PLC-IP>:4840(the address of the PLC's OPC UA server) - Action: Subscribe
- Node IDs: the nodes you want to monitor (e.g.
ns=2;s=Temperature)
3.2 "Function" node (transform to OPC UA Pub/Sub JSON)
Function node code:
// Transforma data change de OPC UA a formato Pub/Sub JSON
var nodeName = msg.topic.split(";").pop() || "Unknown";
var value = msg.payload;
msg.payload = JSON.stringify({
"MessageType": "ua-data",
"PublisherId": "Gateway-" + env.get("HOSTNAME"),
"Messages": [{
"DataSetWriterId": 1,
"Payload": {}
}]
});
// Parsear para agregar el campo dinamico
var obj = JSON.parse(msg.payload);
obj.Messages[0].Payload[nodeName] = {
"Value": value,
"SourceTimestamp": new Date().toISOString(),
"StatusCode": 0
};
msg.payload = JSON.stringify(obj);
return msg;3.3 "MQTT Out" node
- Server: MQTT broker URL (e.g.
mqtt://broker.hivemq.com:1883) - Topic: the topic configured in Rela-ai (e.g.
plant-north/gateway-line1) - QoS: 0
3.4 Connect
[OPC-UA Client] → [Function] → [MQTT Out]Click Deploy (red button at the top right).
Step 4: Verify
Open a terminal and subscribe to the topic to check if messages are arriving:
mosquitto_sub -h broker.hivemq.com -t "plant-north/gateway-line1" -vYou should see JSON messages every time a value changes in the PLC.
Step 5: Configure Rela-ai
- In the dashboard, create a source with protocol OPC UA → Via Gateway MQTT
- Set the broker URL and topic you used in the MQTT Out node
- Rela-ai starts receiving data automatically
Monitoring multiple nodes
To monitor several PLC nodes at once, add multiple "OPC-UA Client" nodes (one per variable) and connect them all to the same Function node. The Function node will accumulate the fields in the Payload.
Alternative: use a single OPC-UA Client node with Action "Subscribe" and configure multiple Node IDs separated by commas.
Tips
- Automatic reconnection: Node-RED reconnects automatically if the PLC or broker disconnects.
- Persistence: If the Raspberry Pi restarts, Node-RED starts automatically (if you enabled the service).
- Node-RED Dashboard: You can add a local dashboard in Node-RED to view data in real time from the plant floor.
- Security: If the PLC requires OPC UA authentication, configure the username/password in the OPC-UA Client node.