En este blog configuraremos un M-Duino Arduino PLC como esclavo MQTT y un Raspberry PLC como maestro. Si seguiste nuestro blog anterior en el que usamos Node-RED como maestro, puedes reutilizar los mismos diagramas ladder.
Prerequisite OpenPLC and MQTT Tutorials for M-Duino Arduino PLC and Raspberry PLC
This blog is part of a series of blogs about OpenPLC on Arduino and M-Duino Arduino PLC. If you still have not familiarized yourself with the software, we recommend you go through the previous blogs first:
Modbus on Arduino with OpenPLC
Mapping any M-Duino Arduino PLC on OpenPLC
MQTT on M-Duino Arduino PLC with OpenPLC
HTTP on M-Duino Arduino PLC with OpenPLC
Hardware Requirements for M-Duino Arduino PLC and Raspberry PLC MQTT Integration
Para el maestro usamos un Raspberry PLC con Mosquitto. El cliente es un M-Duino 42+, but you can use any M-Duino Arduino PLC or any OpenPLC-compatible board. The client connects to the server through Ethernet, it may be necessary to use an Ethernet switch.
Configuring the Raspberry PLC as MQTT Broker with Node-RED
If you need a reminder on how to connect to your Raspberry PLC, we recommend you take a look at this free course:
After connecting to your Raspberry PLC, run these two commands to install Mosquitto:
Now you need to do some edits to the Mosquitto configuration. Execute this command to go to the Mosquitto folder and open the configuration file:
Then add these two lines at the end:
Save the file, and restart Mosquitto. To restart Mosquitto, use this command:
Now you can create a topic. Slave devices will use this topic to send or receive information to the Raspberry PLC:
We use the 10.10.11.20 ip because we are using the Ethernet port on the top side of the Raspberry PLC. The one at the side would be 10.10.10.20. The topic in this case is "openplc".
After executing that last command, Mosquitto should be listening for any incoming messages.
Configuring M-Duino Arduino PLC as MQTT Slave to Publish Messages in OpenPLC
Abre el ejemplo MQTT_Send en OpenPLC (File > Tutorials and Examples).
Crea una nueva variable llamada "msg", asigna el tipo bool y la ubicación %IX0.0. Luego, en el diagrama ladder, cambia el broker de "MQTT_CONNECT0" de 'test.mosquitto.org' a '10.10.11.20', cambia el topic de "MQTT_SEND0" de 'openplc-test-topic' a 'openplc' y cambia el mensaje de 'Hello from OpenPLC!' a la variable "msg". Añade un bloque de función "R_TRIG" justo antes de "SEND" y un "BOOL_TO_STRING" antes de "MESSAGE".
La tabla de variables debe quedar así:
And the ladder diagram like this (ignoring the top part of the blink):
Esto enviará "TRUE" o "FALSE" por MQTT según el valor de %IX0.0, que corresponde a la entrada I0.0 del M-Duino Arduino PLC.
To successfully compile and upload the code there are two things you'll need to do:
- La primera es mapear correctamente tu M-Duino Arduino PLC en el OpenPLC Editor. Esto se explica en este blog:
Mapping any M-Duino Arduino PLC for OpenPLC
- La segunda es habilitar Modbus TCP Ethernet. No vamos a usar Modbus TCP, pero al habilitarlo también se activan las funcionalidades necesarias para usar MQTT por Ethernet. Para ello, ve a la pestaña de comunicaciones en el menú "Transfer Program to PLC" y confíguralo. Lo hemos configurado así:
Now you can upload it to the M-Duino Arduino PLC. If your Raspberry PLC is still listening for MQTT messages, you should start receiving "FALSE" or "TRUE" messages every second.
Next blog: