Interaction between devices using node-red

I have a raspberry pi 3 A+ and pi zero w both with node red. I am trying to setup a button on the Pi Zero Z W and an LED on the Pi 3 A+ so that when the button on the zero is pressed, it will trigger the LED to turn on.

I have looked into the MQTT to facilitate this but have not been able to work it out. Am I looking in the right place? Or is there a better way?

There are a number of ways to achieve this, MQTT is certainly a tried and tested method. One observation though, you would not need NR server installed on both Pis to achieve this.

Going down the MQTT route, have you installed an MQTT broker yet? One that works very well on the Pi is Mosquitto https://mosquitto.org/.

Mosquitto and NR running on the one Pi is a good combination.

1 Like

As @Bobo says, to use MQTT one of the two pi needs to be set up as a MQTT broker ao you need to install MQTT on one of them. Lets say you install MQTT on the Pi 3 A+ you would have

You will need to use the IP of the Pi 3A+ in the MQTT-out node on the Pi Zero W to tell it where the broker is.

2 Likes

For simplicity you could also use this contributed node installed in the Pi3:

But watch out for the fact that the node doesn't support Retained topics out of the box. In fact mosquitto is so easy to setup that I would go for that.
sudo apt install mosquitto mosquitto-clients
is all you have to do if I remember correctly. I think the default configuration file (/etc/mosquitto/mosquitto.conf) is ok for normal use.

1 Like

+1 on mosquitto - it just works straight away with no configuring

1 Like

I think @zenofmud should get 10 out of 10 marks for excellent artwork.

If all you want to do is press a button, then maybe a Pi-Zero-W would be a bit of an overkill.
I would use a Wemos D1 Mini as it's much cheaper (and safer in case you destroy an input pin).

3 Likes

But it can't run Node-RED

Pi/MQTT/Node-RED are best combination for beginners (especially if they already have the Pi)

Switching to WEMOS type devices comes later on in IOT education

Hmm, I do agree on the simple installation, in fact I use mosquitto all over the places, I was thinking what could be easiest for beginners. For the sample simple button solution drafted, retained topics seems not really needed

Anyway, besides installation of the mosquitto broker, I think I remember it is needed to arrange so that it starts automatically at boot. Simple for sure, but some additional beginner steps

sudo systemctl enable mosquitto.service
sudo systemctl start mosquitto.service

AFAIR (I run my mosquitto broker on Win10 but have run one or two in the past) mosquitto automatically installs itself as a service on the Pi

[edit] just tested it and it auto runs mosquitto straight away after doing sudo apt-get install mosquitto mosquitto-clients

OK thank you, I was wrong then

To summarise for @realsammcphail

install Node-RED on both Pi and set them running

install mosquitto on the Pi3A+ (as its the more powerful Pi)

Connect to Node-RED on the Pi3A (preferably from a browser on a 3rd machine)

Add yourself a Pi input node configured to use whatever pin you have your button connected to

And a MQTT out node configured with topic button and address of localhost (or you can use 127.0.0.1 or the actual Ip of the machine - up to you)

image

image

So you'll end up with this

image

Connect to Node-RED on the PiZero using another tab on your browser

Add yourself an MQTT input node and a Pi output node

configure your MQTT to use same topic (button) but make sure its set for the actual ip address of your Pi3A (e.g replace 192.168.0.218 with ip address of your pi3A)

image

Come back if you need further explanation :slight_smile:

This really ought to be properly documented somewhere as a 101 on using MQTT to communicate between two Pi

Or is it and I've just re-invented the wheel :slight_smile:

1 Like

Almost - but not quite as you have it - https://cookbook.nodered.org/#mqtt
so it would be a good new receipe for a PR.

Where's the source for those docs and I'll try and put together one for it

It's here

try to aim for similar task Problem / Solution /Discussion style.
Of course feel free to reference other ones as necessary as this builds on the basics nicely.

1 Like

thank you everybody, I'm right on this and I will notify everybody of my progress. The pi zero w is overkill because we needed a few levels of redundancy for my project.

Thanks again!!!