Install an MQTT Server and Node-RED on Raspberry Pi for Home Automation

Messaging Queuing Telemetry Protocol, or MQTT, is a lightweight messaging protocol designed for low bandwidth devices and environments. It’s a fast, easy-to-deploy, and energy-efficient solution to connect and exchange information between multiple IoT devices. It requires an MQTT broker/server and the topic to which messages are published and subscribed by the devices or users to send or receive the information.

In this guide, we will deploy the MQTT broker and Node-RED tool on Raspberry Pi to connect MQTT-enabled DIY smart IoT devices and use them for home automation.

4

Things You Will Need

To build and deploy an MQTT server with Node-RED, you will need the following:

Step 1: Install Raspberry Pi OS Lite (64-bit)

You will need the 64-bit Lite version of Raspberry Pi OS. In theRaspberry Pi Imagertool on another computer, clickChoose OS>Raspberry Pi OS (Other), then selectRaspberry Pi OS Lite (64-bit). In addition, you’ll want to use Imager’s advanced settings to enable SSH (we usedpiandraspberryas the username and password for this guide) and configure the Wi-Fi connection. For more details, check out our guide onhow to install an operating system on a Raspberry Pi. After writing the OS to the card, eject it and insert it into your Raspberry Pi.

Step 2: Connect Over SSH and Update the Packages

it’s possible to connect to your Raspberry Pi over SSH using PuTTY on Windows or Terminalon macOS and Linux. However, you should know the IP of the Raspberry Pi to connect over SSH. To find out, you can open your wireless router’s DHCP settings or use the Fingapp on your smartphone.

Enter the IP address inPuTTYand clickConnect. Type your username and password to log in.

BBC iPlayer showing on a smart TV.

On Linux or macOS terminal, typeYourPiUsername@IPAddressand then type the password to log in. Once connected via SSH, run the following command to update the package list and upgrade packages.

Step 3: Install MQTT Broker

To install and enable the MQTT broker (Mosquitto), take the following steps.

Step 4: Test MQTT Broker

To test whether the MQTT broker is working or not, installMQTTLensor a similar MQTT app in Chrome or any other web browser and launch it. Enter the IP address of the Raspberry Pi and the MQTT username/password. ClickCreate Connection.

Enter any topic in theSubscribefieldand the same topic in thePublishfield. Type “Hi” or anything in the message field and clickPublish.

Mint Mobile SIM Protection Number Lock with SIM cards on table

If you see the message under the subscriptions, the MQTT broker is working fine.

Step 5: Install Node-RED

With Node-RED, you may publish messages or send and receive payloads to or from your smart devices. This payload can be used to trigger smart devices to switch on/off or receive data (such as temperature) from a sensor. You can use your web browser to create such flows in Node-RED and control your smart devices.

To install Node-RED, execute the following command in the Terminal window.

The streaming app Filmrise opened to crime documentaries and suspense movies and new arrivals

This will download and install the Node-RED on your Raspberry Pi. It may take a while to complete. Once Node-RED is installed, you need to create a Node-RED settings file by running the following command.

Then follow these steps:

A Raspberry Pi in a case with a LEGO mini figure sitting on top

Once this is done, restart Node-RED.

you’re able to now visitYour-Raspberry-Pi-IP:1880in a web browser to open and log in to the Node-RED admin panel.

You can now create flows in Node-RED. Below is an example of a Node-RED flow we use to run our WS2812B 8x32 matrix display that shows various sensor information installed in our home in real-time.

To get started, you can alsobuild a DIY smart clock using MAX7219 dot matrix, and connect it to your MQTT server to display text or messages on it.

Enable Home Automation Using MQTT and Node-RED

You can now create flows to control or monitor your MQTT-enabled smart home devices. You can also build DIY Tasmota smart switches or Arduino-based smart sensors, and other smart devices with MQTT control and use them with this MQTT and Node-RED server. You can create flows for home automation based on events, time, or payload received from your smartphone MQTT client.

On your Android smartphone, you can install MQTT Dash to connect to your MQTT broker and send or receive payloads to or from various topics that your smart devices subscribe or publish to.

Add all your compatible smart home devices, such as smart plugs, lamps, switches, AC, and more, to Home Assistant for open-source automation.

My iPhone does it all, but I still need my dumb phone.

The best features aren’t the ones being advertised.

It’s not super flashy, but it can help to keep your computer up and running.

Your phone’s camera app doesn’t show this, so it’s easy to miss.

You’re not getting the most out of what you pay for iCloud+.

Technology Explained

PC & Mobile