To send data from a Raspberry PLC to an M-Duino Arduino PLC with MQTT, run the MQTT broker and a Node-RED publisher on the Raspberry PLC, and subscribe from the M-Duino Arduino PLC with the PubSubClient library. This example publishes the CPU temperature of the Raspberry PLC once per second and prints it on the M-Duino Arduino PLC.
This is part 2 of the series. Part 1 explains MQTT with Node-RED on a Raspberry PLC: Node-RED and MQTT on a Raspberry PLC.
How the Raspberry PLC and the M-Duino Arduino PLC share data over MQTT
MQTT has no master and no slave. Every device is a client of one broker. A client publishes messages to a topic, and the broker delivers them to every client subscribed to that topic.
- Raspberry PLC: runs the broker and publishes to the topic
test/Tempfrom Node-RED. - M-Duino Arduino PLC: subscribes to
test/Tempand prints each value on the Serial Monitor. - PC: can subscribe to the same topic to check the data.

What you need
- Raspberry PLC with Node-RED installed
- M-Duino Arduino PLC and the Arduino IDE with the PubSubClient library
- 12 to 24 Vdc power supply for both PLCs
- Ethernet switch and 3 Ethernet cables
Connect the PC and both PLCs to one Ethernet switch
All three devices must be in the same subnet, with netmask 255.255.255.0. In this example the Raspberry PLC uses 10.10.10.20, its default Ethernet address, and the M-Duino Arduino PLC uses 10.10.10.22. Give the PC another free address in 10.10.10.x.
Run an MQTT broker on the Raspberry PLC
Node-RED publishes to a broker, but it is not a broker. Install Mosquitto on the Raspberry PLC. You can connect over SSH:
sudo apt install mosquitto mosquitto-clients
Since Mosquitto 2.0, the broker only accepts local connections by default. To let the M-Duino Arduino PLC connect, create the file /etc/mosquitto/conf.d/lan.conf with these two lines and restart the service with sudo systemctl restart mosquitto:
listener 1883 allow_anonymous true
Anonymous access is only for a closed test network. In production, add users and passwords.
Publish the CPU temperature from Node-RED
Open Node-RED at http://10.10.10.20:1880 and build a flow with three nodes:
- inject node, set to repeat every 1 second.
- exec node with the command below. It returns the CPU temperature of the Raspberry PLC. Any other command or sensor value works the same way.
- mqtt out node with the topic
test/Tempand the server localhost, port 1883, MQTT V3.1.1.
vcgencmd measure_temp | grep -Eo '[0-9]{1,3}.[0-9]'

You can import the flow from this JSON (Menu, Import):
[{"id":"3886266038c74eba","type":"inject","z":"3b697b58be0a0e02","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":180,"wires":[["2ba008db26e34c47"]]},{"id":"2ba008db26e34c47","type":"exec","z":"3b697b58be0a0e02","command":"vcgencmd measure_temp | grep -Eo '[0-9]{1,3}.[0-9]'","addpay":"","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":420,"y":180,"wires":[["f38a12a1549e4c10"],[],[]]},{"id":"f38a12a1549e4c10","type":"mqtt out","z":"3b697b58be0a0e02","name":"","topic":"test/Temp","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"d46b95fdc1020a0c","x":700,"y":180,"wires":[]},{"id":"d46b95fdc1020a0c","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":"","credentials":{}}]
Subscribe from the M-Duino Arduino PLC
Open the Arduino IDE, select your M-Duino Arduino PLC and upload this sketch:
#include <Ethernet.h>
#include <PubSubClient.h>
IPAddress ip(10, 10, 10, 22); // M-Duino Arduino PLC
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x22}; // 6 bytes, unique in your network
EthernetClient ethClient;
PubSubClient mqtt(ethClient);
const char *broker = "10.10.10.20"; // Raspberry PLC
const uint16_t port = 1883;
const char *id = "mduino-test";
void callbackMQTT(char *topic, byte *payload, unsigned int length) {
Serial.print("Received on ");
Serial.print(topic);
Serial.print(": ");
String data = "";
for (unsigned int i = 0; i < length; i++) {
data.concat((char)payload[i]);
}
Serial.println(data);
}
void connectMQTT() {
if (mqtt.connect(id)) {
mqtt.subscribe("test/Temp");
} else {
delay(500);
}
}
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
delay(1500);
mqtt.setServer(broker, port);
mqtt.setCallback(callbackMQTT);
}
void loop() {
if (!mqtt.connected()) {
connectMQTT();
}
mqtt.loop();
}
- The broker address is the Ethernet IP of the Raspberry PLC.
- In
loop(), if the M-Duino Arduino PLC is not connected to the broker,connectMQTT()connects again and subscribes totest/Temp.mqtt.loop()keeps the connection alive and processes incoming messages. - The broker calls
callbackMQTT()for every message on a subscribed topic. - MQTT topics are case sensitive:
test/Tempandtest/tempare two different topics.
Check the result
Deploy the flow in Node-RED and open the Serial Monitor of the Arduino IDE at 9600 baud. Every second you should see a line like this:
Received on test/Temp: 56.2
From the PC, you can check the same data with the Mosquitto client:
mosquitto_sub -h 10.10.10.20 -t test/Temp
Where this pattern fits
The same structure works for any data that one controller collects and other devices need: a Raspberry PLC publishes sensor values, and M-Duino Arduino PLCs, a SCADA or a database subscribe only to the topics they use. To add a device, subscribe it to the topic. The publisher does not change.
Next in the series: MQTT on an M-Duino Arduino PLC with OpenPLC.