OpenPLC can publish and receive MQTT messages on an M-Duino Arduino PLC over Ethernet. In this example the M-Duino Arduino PLC sends the state of input I0.0 to a Node-RED dashboard, and a switch in the dashboard turns output Q0.4 on and off.
How the M-Duino Arduino PLC, the broker and Node-RED exchange messages
MQTT has no master and no slave. The M-Duino Arduino PLC and Node-RED are both clients of the same broker. One publishes to a topic, and the broker delivers the message to every client subscribed to that topic.
- Broker: runs on the PC, at 10.10.10.60 in this example. Mosquitto is a common choice. Since Mosquitto 2.0 it only accepts local connections by default, so add
listener 1883andallow_anonymous trueto its configuration for a closed test network. - M-Duino Arduino PLC: runs the OpenPLC program, with the IP 10.10.10.70.
- Node-RED: runs on the PC and shows and sends the messages.
Before you start
This post is part of a series about OpenPLC on Arduino and on the M-Duino Arduino PLC. If OpenPLC is new to you, start with these posts:
- First steps with OpenPLC
- Modbus on Arduino with OpenPLC
- OpenPLC on M-Duino 42+
- Mapping any M-Duino Arduino PLC on OpenPLC
For Node-RED and MQTT, see the free course Develop your SCADA application based on Node-RED.
Node-RED: show the messages the M-Duino Arduino PLC sends
Create a flow with an mqtt in node subscribed to the topic test and connected to the broker. Connect it to a text node to see the message in the dashboard. A debug node also shows it in the debug panel.
OpenPLC: publish input I0.0 from the M-Duino Arduino PLC
Start from the MQTT_Send example in the OpenPLC Editor: File, Tutorials and Examples, MQTT_Send.
Create three variables:
- broker, type STRING, initial value the broker IP: "10.10.10.60".
- topic, type STRING, initial value "test".
- msg, type BOOL, location %IX0.0 (input I0.0).
The example variables keep their locations: blink_led at %QX0.0 and mqtt_connected at %QX0.1.
Then change the ladder diagram:
- Connect broker to the BROKER pin of MQTT_CONNECT0.
- Connect topic to the TOPIC pin of MQTT_SEND0.
- Connect msg to the MESSAGE pin through a BOOL to STRING conversion block.
- Put a trigger block between the blink_led contact and the SEND pin, so that each pulse of blink_led sends one message instead of sending continuously.
When the connection to the broker works, output Q0.1 turns on. Each pulse of blink_led then publishes the state of I0.0 to the topic test.
Compile and upload to the M-Duino Arduino PLC
- Map your M-Duino Arduino PLC in the OpenPLC Editor, as explained in Mapping any M-Duino Arduino PLC on OpenPLC.
- In the Transfer Program to PLC window, open the communications tab and enable Modbus TCP with the IP of the M-Duino Arduino PLC, 10.10.10.70 in our test. The program does not use Modbus TCP: in our test this option is what enabled the Ethernet interface that MQTT needs.
Upload the program and change input I0.0. The new value appears in the Node-RED dashboard.
Node-RED: send a command to the M-Duino Arduino PLC
Now the direction changes. Replace the mqtt in node with an mqtt out node, and the text node with a switch node from the dashboard.
Set the switch to send strings:
OpenPLC: receive the command and switch output Q0.4
Open the MQTT_Send example again and set up the variables. This time msg is bound to output Q0.4, so Node-RED controls that output.
Then build the ladder diagram:
- The MQTT subscribe block subscribes to the topic.
- Once subscribed, the MQTT receive block listens for new messages.
- The receive block returns a string, so a conversion block turns it into a BOOL.
- An SR flip-flop keeps the last value of msg between messages.
Upload the program. The switch in the Node-RED dashboard now turns output Q0.4 on and off.
Next in the series: HTTP on an M-Duino Arduino PLC with OpenPLC.