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.
Prerequisite OpenPLC Tutorials for M-Duino MQTT Programming
This post is part of a series of posts about OpenPLC on Arduino and M-Duino. If you still have not familiarized yourself with the software, we recommend you go through the previous posts first:
Modbus on Arduino with OpenPLC
Mapping any M-Duino on OpenPLC
Configuring the M-Duino Master to Receive MQTT Messages in 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.
If you haven't familiarized yourself with Node-RED and MQTT, we recommend you go through this free course:
Develop your SCADA Application based on Node-RED
Configuring the M-Duino Slave to Publish MQTT Messages in 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.
To successfully compile and upload the code there are two things you'll need to do:
- 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.
Configuring the M-Duino Master to Publish MQTT Messages in 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.
Set up the switch node to work with strings:
Configuring the M-Duino Slave to Receive MQTT Messages in OpenPLC
To receive messages we have to do something similar to what we previously did, but with different function blocks and variables. Open the MQTT_Send example and set up the variables like this:
This time, "msg" will be bound to the Q0.4 output of the M-Duino. The master will control this output by sending messages through MQTT. Now set up the ladder diagram:
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:

MQTT on M-Duino with OpenPLC