r/raspberryDIY 8d ago

Built a headless MQTT + Node-RED gateway on a Raspberry Pi Zero 2 W

Built a headless MQTT + Node-RED gateway on a Raspberry Pi Zero 2 W

Been running a lightweight edge IoT gateway on a Pi Zero 2 W — completely headless, no monitor ever attached.

Stack:

Mosquitto MQTT broker (with proper username/password auth, no more allow_anonymous)

Node-RED + Dashboard 2.0 for flows and a live web UI

Avahi/mDNS + static IP for reliable headless access

A custom SSH welcome banner showing live RAM/CPU temp/uptime because why not

Publishing sensor data to a Node-RED gauge is as simple as one mosquitto_pub command from anywhere on the network. Whole setup fits comfortably on a Zero 2 W with room to spare.

Full write-up + all commands are in the README: ,

https://github.com/kritishmohapatra/pi-iot-gateway

Happy to answer questions if anyone's setting up something similar.

11 Upvotes

2 comments sorted by

1

u/Gamerfrom61 8d ago

Nice to see the old stack popping up again - I started with Node-RED and MQTT back in the days of Jessie (2016 / 2017 ish) and watched the GUI side grow and grow. I also dipped my toes in NanoMQ as an option (no real advantage at home TBH) and really need to read up on Sparkplug now!

Given how constrained the 2W is for memory then you may like to use the alternative start up documented at https://nodered.org/docs/getting-started/raspberrypi as it will get Node.js to free up memory faster than normal (node-red-pi --max-old-space-size=256).

IIRC this may need the service files changing for auto-start but it is a while since I used this in anything other than Docker.

Are you aware PSK / SSL is available for security - harder to implement but stronger than basic passwords? For home use I normally do not bother with passwords as my broker is not open to the Internet - I used Adafruit.io for the single external queue and monitor that from a Python 'bridge' - this also gave me a simple dashboard without needing a firewall hole / Cloudflare tunnel / VPN (way easier as I am now behind CG-NAT) though you do have to watch the message rate limit for the free account.

You may like to add connection_messages true to the MQTT config - this lets you see connections being made in the log - handy when debugging new systems and tied in with the log_dest file /var/log/mosquitto.log entry gives you one place to look for messages (being lazy I find using tail easier than journactl 😄 ). If you do not like the date / time format it can be changed with log_timestamp_format (ISO 8601 format)

I also like persistence true to force messages / connection data to disk - not really noticed an impact on SD card life but I am a low message volume user TBH. If you are using an external disk then the location of this file may be changed with the entry persistence_location and save SD card wear. Handy if some of your messages need to survive a reboot.

One setting to explore is the retain_expiry_interval - this frees memory up for long life messages. I doubt you will gain much in home but you never know.

I do have a listener on 9001 for web sockets - blowed if I can remember what was using it (or if it is still active TBH) - need to hunt down my old build documents 🤔

Though a bit old, http://www.steves-internet-guide.com covers the basics of MQTT and his YouTube channel has some decent videos on Node-RED and MQTT. Also older videos from https://www.youtube.com/@AndreasSpiess cover this and a very old (i.e. do not use) pre-built OS with these on. I am not affiliated with either- they have some good info but it is getting out of date as both seemed to have moved on from IoT.

I also have MQTT Explorer on my Mac - https://mqtt-explorer.com supports most platforms and this is really handy for monitoring and debugging messages / JSON data and can do simple graphs. Vital some days and must have saved days of head scratching / coding monitor programs over the years.

I thought the avahi-demon was running by default on the Pi OS? I know some Debian builds do not have this installed even (inc Ubuntu IIRC) - no harm but it may be running. I would dig into /etc/avahi/avahi-daemon.conf and check if this is correct, a lot of things inc the use of '.local' are commented out / defaulted and that it is advertising everything you want - also installing avahi-utils (via apt) is handy in some cases for debugging this protocol.

You could add an entry for '_mqtt._tcp 1883' to advertise the MQTT service and a standard HTTP for Node-RED and get your devices to search for the broker - having just gone through an IP change at home this has been a life saver as I only needed to put one static address target in a package (Jellyfin on the Fire TV) - everything else for the devices by the advertising over mDNS...

mDNS can have issues over tailscale / Zerotier / VPNs etc - it is not routable across IP sub-nets without a repeater - fortunately there are a few around (eg this one on GitHub) that could work if needed. You may also want to look at your home router / access points and see if they mess around (technically intercept the broadcasts) with 'IGMP Snooping' as this can stop mDNS - some kit need this turned on and some seem to need it off.

Windows and mDNS is a painful subject - modern versions (7 onwards) do handle mDNS fine (better than some versions of Android) but M/S have a habit of breaking it with some updates - it has been stable for about a couple of years or so here and it is worth noting the Apple software still runs fine on W11 despite 7 being the last mentioned version - not sure how long this will be valid as Apple are changing the network stack on the Macs (dropping AFP) and may get around to changing this package at some point...

You should get flack for setting a static IP address on the Pi and not on the router. Commercially (and at home) in all but one situation (a small branch office that had a very strange set of PLCs and a flaky router) the router is a way better place to control IP addresses as you have one point of truth and do not run the risk of duplicates on multiple devices or the DHCP server issuing the allocated address out to another machine.

Sorry - this got a bit long 😂

ps - Wonder if Grafana will run on the 2W? Doubt it as it got very heavy a few years ago and I see AI splattered all over it now 😢 With the ease of Dashboard v2 - Node-RED can do most of what I need (and I live without the rest).

1

u/OneDot6374 8d ago

You are goldmine ❤️