In this post, we will configure an M-Duino as an MQTT slave and a computer as the master. First, we will configure the M-Duino to send the value of I0.0 to the master; then we will see how to configure it to receive information.
Tutoriales previos de OpenPLC para programar MQTT en M-Duino
Este post forma parte de una serie de posts sobre OpenPLC en Arduino y M-Duino. Si aún no te has familiarizado con el software, te recomendamos que repases los posts anteriores primero:
Modbus on Arduino with OpenPLC
Mapping any M-Duino on OpenPLC
Configurar el maestro M-Duino para recibir mensajes MQTT en OpenPLC
For the master, we will create a simple flow in Node-RED to read the messages sent by the M-Duino. Add an "mqtt in" node and connect it to a text node to see the received message in the dashboard. You can also add a debug node to see the message in the debug terminal.
Si aún no te has familiarizado con Node-RED y MQTT, te recomendamos que hagas este curso gratuito:
Develop your SCADA Application based on Node-RED
Configurar el esclavo M-Duino para publicar mensajes MQTT en OpenPLC
We will start from the MQTT_Send example included in OpenPLC. Go to File > Tutorials and Examples and open the MQTT_Send example.
First we will configure the variables. Create three new variables called "broker", "topic" and "msg". Assign the string type to "broker" and "topic", and the bool type to "msg". The locations will be %QX0.0 for "blink_led", %IX0.0 for "msg" and %QX0.1 for "mqtt_connected". The initial value of "broker" will be the IP of the master; in our case, "10.10.10.60". For "topic", assign the initial value "test". The variable table should look like this:
Now you need to modify the ladder diagram. Connect "broker" to the "BROKER" pin of "MQTT_CONNECT0" and "topic" to the "TOPIC" pin of "MQTT_SEND0". To connect "msg" to the "MESSAGE" pin you will need a function block that converts it from bool to string. Add a trigger function block between the "blink_led" contact and the "SEND" pin to prevent the message from being sent continuously when "blink_led" is active.
The resulting ladder diagram will connect to the broker and turn on the Q0.1 LED of the M-Duino if the connection is correct. It will then send the value of the signal received at I0.0 to the master via MQTT. This process will repeat every second.
Para compilar y cargar el código correctamente necesitas hacer dos cosas:
- The first is to correctly map your M-Duino in the OpenPLC Editor. This is explained in the post Mapping any M-Duino on OpenPLC linked above.
- The second is to enable Modbus TCP Ethernet. We will not be using Modbus TCP, but enabling it also activates the functionalities needed to use MQTT over Ethernet. To do this, go to the communications tab in the "Transfer Program to PLC" menu and configure it. In our case we use 10.10.10.70 as the IP, but you can use any IP.
After that, you should be able to upload the code to the M-Duino and see the changes in Node-RED.
Configurar el maestro M-Duino para publicar mensajes MQTT en OpenPLC
We will do a similar configuration to the previous one, but instead of using "mqtt in" and a text node, we will use "mqtt out" and a switch node.
Configura el nodo switch para trabajar con cadenas de texto:
Configurar el esclavo M-Duino para recibir mensajes MQTT en OpenPLC
Para recibir mensajes debemos hacer algo similar a lo anterior, pero con bloques de función y variables distintos. Abre el ejemplo MQTT_Send y configura las variables así:
Esta vez, "msg" estará vinculado a la salida Q0.4 del M-Duino. El maestro controlará esta salida enviando mensajes por MQTT. Ahora configura el diagrama ladder:
We use the "MQTT_SUBSCRIBE" function block to subscribe to the topic. Once successfully subscribed, "MQTT_RECIEVE" will continuously listen for incoming messages. Since "MQTT_RECIEVE" only returns strings, we need a conversion function to transform the message to boolean. It is important to add the SR flip-flop to maintain the positive value of "msg" when no message is received.
After uploading the code to the M-Duino, you can use the switch in the Node-RED dashboard to activate the Q0.4 output of the M-Duino.
Next post: