Gateway con Node-RED (Raspberry Pi)
Guia paso a paso para configurar un Raspberry Pi con Node-RED como gateway OPC UA a MQTT para Rela-ai.
Que necesitas
- Raspberry Pi 4 (2GB+ RAM) o cualquier Linux con Node.js
- Cable ethernet para conectar a la red del PLC
- Acceso a internet (WiFi o segundo cable ethernet)
- Datos del PLC: IP, puerto OPC UA, Node IDs a monitorear
Paso 1: Instalar Node-RED
Si usas Raspberry Pi OS, Node-RED se instala con un solo comando:
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)Responde "y" a todas las preguntas. Al terminar:
# Iniciar Node-RED como servicio (arranca automaticamente al encender)
sudo systemctl enable nodered
sudo systemctl start noderedAccede a Node-RED en el navegador: http://<IP-del-raspberry>:1880
Paso 2: Instalar el plugin OPC UA
En la interfaz de Node-RED, ve a Menu (hamburguesa) → Manage Palette → Install y busca:
node-red-contrib-opcuaHaz click en "Install". Esto agrega los nodos OPC UA al panel izquierdo.
Paso 3: Crear el flujo OPC UA → MQTT
Arrastra estos nodos al canvas y conectalos en orden:
3.1 Nodo "OPC-UA Client"
- Endpoint:
opc.tcp://<IP-del-PLC>:4840(la direccion del servidor OPC UA del PLC) - Action: Subscribe
- Node IDs: los nodos que quieres monitorear (ej:
ns=2;s=Temperature)
3.2 Nodo "Function" (transformar a OPC UA Pub/Sub JSON)
Codigo del nodo Function:
// 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 Nodo "MQTT Out"
- Server: URL del broker MQTT (ej:
mqtt://broker.hivemq.com:1883) - Topic: el topic configurado en Rela-ai (ej:
planta-norte/gateway-linea1) - QoS: 0
3.4 Conectar
[OPC-UA Client] → [Function] → [MQTT Out]Haz click en Deploy (boton rojo arriba a la derecha).
Paso 4: Verificar
Abre una terminal y suscribete al topic para ver si llegan los mensajes:
mosquitto_sub -h broker.hivemq.com -t "planta-norte/gateway-linea1" -vDeberias ver mensajes JSON cada vez que un valor cambia en el PLC.
Paso 5: Configurar Rela-ai
- En el dashboard, crea una fuente con protocolo OPC UA → Via Gateway MQTT
- Configura el broker URL y topic que usaste en el nodo MQTT Out
- Rela-ai empieza a recibir los datos automaticamente
Monitorear multiples nodos
Para monitorear varios nodos del PLC a la vez, agrega multiples nodos "OPC-UA Client" (uno por variable) y conectalos todos al mismo nodo Function. El Function acumulara los campos en el Payload.
Alternativa: usa un solo nodo OPC-UA Client con Action "Subscribe" y configura multiples Node IDs separados por coma.
Tips
- Reconexion automatica: Node-RED reconecta automaticamente si el PLC o el broker se desconectan.
- Persistencia: Si el Raspberry Pi se reinicia, Node-RED arranca automaticamente (si habilitaste el servicio).
- Dashboard Node-RED: Puedes agregar un dashboard local en Node-RED para ver los datos en tiempo real desde la planta.
- Seguridad: Si el PLC requiere autenticacion OPC UA, configura usuario/password en el nodo OPC-UA Client.