Install an MQTT and Node-RED server on Raspberry Pi for home automation

The Mail Queue Telemetry Protocol, or MQTT, is a lightweight messaging protocol designed for low-bandwidth devices and environments. It is a fast, easy to deploy and energy-efficient solution for connecting and exchanging information between multiple IoT devices. It requires an MQTT broker/server and the subject to which messages are published and subscribed by 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 create 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 (8 GB or more).
  • A microSD card reader.
  • A Linux, 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 the operating system > Raspberry Pi operating system (other)then select Raspberry Pi OS Lite (64 bit). Also, you’ll want to use Imager’s advanced settings to enable SSH (we used ft and raspberry as the username and password for this guide) and set up the Wi-Fi connection. For more details, see our guide on how to install an operating system on a Raspberry Pi. After writing the system on the board, eject it and insert it into your Raspberry Pi.


Step 2: Connect via SSH and update packages

You can connect to your Raspberry Pi via SSH using PuTTY in Windows or Terminal on macOS and Linux. However, you need to know the Raspberry Pi’s IP address to connect via 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 Relate. Type your username and password to login.

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

sudo apt update && sudo apt upgrade y

Step 3: Install the MQTT broker

To install and activate the MQTT (Mosquitto) broker, follow these steps.

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

    This will launch the installation of Mosquitto (MQTT broker) and client services.

  2. After 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 delighted is the username (you can use whatever you want). Enter the password when prompted. Write down this username and password for later use.

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

    Paste the following code into 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

    Hurry CTRL+X followed by Yes and Walk in to save changes.

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

    If you see the output above, MQTT is running.


Step 4: Test the MQTT broker

To test whether MQTT broker is working or not, install MQTTLens or similar MQTT app in Chrome or any other web browser and launch it. Enter the Raspberry Pi IP address and MQTT username/password. Click on Create a connection.

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

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

Step 5: Install Node-RED

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

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

bash <(curl -sL https:

This will download and install the Node-RED on your Raspberry Pi. This may take some time. 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 Walk in key.
  2. Do you want to configure user security? Select Yes. Enter a username and password. Keep it safe as it will be used to connect and manage Node-RED through a web interface.
  3. Picking out Full Access.
  4. You can add another user or select Nope proceed.
  5. Select Nope when asked to enable project features.
  6. Hurry Walk in.
  7. Type a passphrase and keep it in a safe place.
  8. Choose the default theme > Monaco (default).
  9. Select Nope when asked to load external nodes.

Once 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 streams in Node-RED. Below is an example Node-RED stream that we use to run our WS2812B 8×32 matrix display which displays various information about the sensors installed in our home in real time.

For starters, you can also create a DIY smart clock using the MAX7219 dot matrix and connect it to your MQTT server to display text or messages on it.

Enable home automation with MQTT and Node-RED

You can now create streams 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 server and Node-RED. You can create streams 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 to or publish to.

Comments are closed.