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

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

by Staff
0 comment

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.

Things You Will Need

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

  • A Raspberry Pi Zero W or better. We recommend using a Raspberry Pi 3 or 4 (if available).
  • A microSD card (8GB or higher).
  • A microSD card reader.
  • A Linux PC, Mac, or Windows PC.
  • Internet connection (Wi-Fi).

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

You will need the 64-bit Lite version of Raspberry Pi OS. In the Raspberry Pi Imager tool on another computer, click Choose OS > Raspberry Pi OS (Other), then select Raspberry Pi OS Lite (64-bit). In addition, you’ll want to use Imager’s advanced settings to enable SSH (we used pi and raspberry as the username and password for this guide) and configure the Wi-Fi connection. For more details, check out our guide on how 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

You can connect to your Raspberry Pi over SSH using PuTTY on Windows or Terminal on 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 Fing app on your smartphone.

Enter the IP address in PuTTY and click Connect. Type your username and password to log in.

On Linux or macOS terminal, type [email protected] and then type the password to log in. Once connected via SSH, run the following command to update the package list and upgrade packages.

sudo apt update && sudo apt upgrade y

Step 3: Install MQTT Broker

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

  1. Install Mosquitto by running the following command in the Terminal window.
    sudo apt install mosquitto mosquitto-clients -y

    This will start installing the Mosquitto (MQTT broker) and client services.

  2. After the installation, enable the Mosquitto service and check the MQTT version using the following commands.
    sudo systemctl enable mosquitto.service
    mosquitto -v
  3. Create a user account to allow authenticated remote access using the following command.
    sudo mosquitto_passwd -c /etc/mosquitto/passwd ravi

    Here ravi is the username (you can use whatever you like). Enter the password when prompted. Note down this username and password for later use.

  4. Open the mosquitto.conf file in the nano editor using the following command.
    sudo nano /etc/mosquitto.conf

    Paste the following code in the nano editor.

    per_listener_settings true
    pid_file /run/mosquitto/mosquitto.pid
    persistence true
    persistence_location /var/lib/mosquitto/
    log_dest file /var/log/mosquitto/mosquitto.log
    include_dir /etc/mosquitto/conf.d
    allow_anonymous false
    listener 1883
    password_file /etc/mosquitto/passwd

    Press CTRL+X followed by Y and Enter to save the changes.

  5. Restart the MQTT broker by running the following command.
    sudo systemctl restart mosquitto
  6. Check the MQTT broker status to see whether MQTT is running.
    sudo systemctl status mosquitto

    If you see the above output, MQTT is running.


Step 4: Test MQTT Broker

To test whether the MQTT broker is working or not, install MQTTLens or 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. Click Create Connection.

Enter any topic in the Subscribe field and the same topic in the Publish field. Type “Hi” or anything in the message field and click Publish.

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

Step 5: Install Node-RED

With Node-RED, you can 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.

bash <(curl -sL https:

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.

node-red admin init

Then follow these steps:

  1. Press the Enter key.
  2. Do you want to set up user security? Select Yes. Enter a username and a password. Keep it safe as this will be used to log in and manage Node-RED via a web interface.
  3. Choose Full Access.
  4. You can add another user or select No to proceed.
  5. Select No when asked to enable the project features.
  6. Press Enter.
  7. Type a passphrase and keep it safe.
  8. Choose the default theme > Monaco (default).
  9. Select No when asked to load external nodes.

Once this is done, restart Node-RED.

node-red restart

You can now visit Your-Raspberry-Pi-IP:1880 in 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 8×32 matrix display that shows various sensor information installed in our home in real-time.

To get started, you can also build 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.

Read the full article here

SaleBestseller No. 1
Apple AirPods Max Wireless Over-Ear Headphones. Active Noise Cancelling, Transparency Mode, Spatial Audio, Digital Crown for Volume Control. Bluetooth Headphones for iPhone - Green
Apple AirPods Max Wireless Over-Ear Headphones. Active Noise Cancelling, Transparency Mode, Spatial Audio, Digital Crown for Volume Control. Bluetooth Headphones for iPhone - Green
 Apple-designed dynamic driver provides high-fidelity audio; Active Noise Cancellation blocks outside noise, so you can immerse yourself in music
$449.99
SaleBestseller No. 3
Apple iPad Air 2, 64 GB, Space Gray (Renewed)
Apple iPad Air 2, 64 GB, Space Gray (Renewed)
Apple iOS 8; 9.7-Inch Retina Display; 2048x1536 Resolution; A8X Chip with 64-bit Architecture; M8 Motion Coprocessor
$129.99
SaleBestseller No. 4
2021 Apple 10.2-inch iPad (Wi-Fi, 64GB) - Silver
2021 Apple 10.2-inch iPad (Wi-Fi, 64GB) - Silver
Gorgeous 10.2-inch Retina display with True Tone; A13 Bionic chip with Neural Engine; 8MP Wide back camera, 12MP Ultra Wide front camera with Center Stage
$269.99
Bestseller No. 5
2022 Apple TV 4K Wi‑Fi with 64GB Storage (3rd Generation)
2022 Apple TV 4K Wi‑Fi with 64GB Storage (3rd Generation)
4K Dolby Vision and HDR10+ for vivid picture quality; Dolby Atmos for three-dimensional, theater-like sound
$123.49
Bestseller No. 7
Apple AirTag 4 Pack
Apple AirTag 4 Pack
Keep track of and find your items alongside friends and devices in the Find My app; Simple one-tap setup instantly connects AirTag with your iPhone or iPad
Bestseller No. 8
Apple MacBook Air with Intel Core i5, 1.6GHz, (13-inch, 4GB,128GB SSD) - Silver (Renewed)
Apple MacBook Air with Intel Core i5, 1.6GHz, (13-inch, 4GB,128GB SSD) - Silver (Renewed)
1.6 GHz dual-core Intel Core i5 (Turbo Boost up to 2.7 GHz) with 3 MB shared L3 cache; 13.3-Inch (diagonal) LED-backlit Glossy Widescreen Display, 1440 x 900 resolution
$305.00
Bestseller No. 9
Apple Of My Eye
Apple Of My Eye
Amazon Prime Video (Video on Demand); Amy Smart, Burt Reynolds, Liam McIntyre (Actors); Castille Landon (Director) - Castille Landon (Writer) - Dori A. Rath (Producer)
$3.99
SaleBestseller No. 10
Apple 35W Dual USB-C Port Compact Power Adapter ​​​​​​​
Apple 35W Dual USB-C Port Compact Power Adapter ​​​​​​​
The compact size and folding prongs make it easy to pack and store.; Charging cable sold separately.
$52.00

You may also like

Leave a Comment

Iman Hearts is one of the biggest lifestyle news and articles portals, we provide the latest news and articles about family, lifestyle, entertainment, and many more, follow us to get the latest news about what matters to you.

 

© 2022 Iman Hearts. All rights reserved. Sitemap