Raspberry Pi MQTT Broker Setup Using Mosquitto (Step-by-Step Guide)
Introduction
If you’re getting started with IoT, one of the first challenges you’ll encounter is enabling communication between multiple devices. For example, how does an ESP32 temperature sensor send data to a dashboard? Or how can a smart relay receive commands from a mobile application?
This is where MQTT comes in.
MQTT is a lightweight messaging protocol designed specifically for IoT applications. At the heart of every MQTT-based system is an MQTT broker—a server responsible for receiving messages from devices and delivering them to interested subscribers.
While cloud-based MQTT brokers are available, many makers and developers prefer hosting their own broker locally. A Raspberry Pi is an excellent choice for this purpose. It’s affordable, consumes very little power, and can run continuously as the communication hub for an entire IoT network.
In this tutorial, you’ll configure a Raspberry Pi as a dedicated MQTT broker using Eclipse Mosquitto. By the end of this guide, you’ll have a Raspberry Pi MQTT server ready to connect ESP32 devices, Node-RED dashboards, Home Assistant, and other IoT applications.
Whether you’re building a smart home system, monitoring environmental sensors, or learning MQTT, this project provides a solid foundation for future IoT projects.
Why Use Raspberry Pi as an MQTT Broker?
There are several reasons why Raspberry Pi is one of the most popular platforms for hosting MQTT brokers.
Affordable
A Raspberry Pi costs significantly less than a dedicated server while providing enough performance for most IoT applications.
Low Power Consumption
Unlike desktop computers, Raspberry Pi consumes very little power, making it ideal for running 24/7.
Local Control
Your MQTT data stays within your local network, reducing dependency on cloud services and improving privacy.
Always Available
A Raspberry Pi can continuously run as the central communication hub for all your IoT devices.
Easy to Expand
As your projects grow, you can integrate:
- ESP32 devices
- Home Assistant
- Node-RED
- Grafana
- InfluxDB
- Docker containers
All from the same Raspberry Pi.
How MQTT Communication Works
Before configuring the broker, let’s quickly understand the communication flow.
- An ESP32 measures room temperature.
- The ESP32 publishes data to an MQTT topic.
- Mosquitto receives the message.
- Node-RED subscribes to the topic.
- Node-RED displays the data on a dashboard.

The broker acts as a middleman between publishers and subscribers.
Hardware Requirements
Recommended Raspberry Pi Models
- Raspberry Pi 5
- Raspberry Pi 4
- Raspberry Pi 3B+
Minimum Requirement
- Raspberry Pi Zero 2 W
Additional Components
- MicroSD Card (16 GB or larger)
- Raspberry Pi OS installed
- Stable network connection
- Power supply
Step 1: Install Mosquitto MQTT Broker
If Mosquitto is not already installed, follow the installation guide: How to Install Mosquitto MQTT Broker on Ubuntu and Raspberry Pi
Or run:
sudo apt update sudo apt upgrade -y sudo apt install mosquitto mosquitto-clients -y
Step 2: Configure Mosquitto for Network Access
Open the Mosquitto configuration file.
sudo nano /etc/mosquitto/mosquitto.conf
Add the following lines:
listener 1883 allow_anonymous true
Save and close the file.
Restart Mosquitto.
sudo systemctl restart mosquitto
Important
The allow_anonymous true setting is suitable for learning and local development only.
For production deployments, enable authentication and TLS encryption.
Step 3: Enable Mosquitto at Startup
Configure Mosquitto to start automatically whenever the Raspberry Pi boots.
sudo systemctl enable mosquitto
Start the service.
sudo systemctl start mosquitto
Verify the service status.
sudo systemctl status mosquitto
Expected output:
Active: active (running)
Step 4: Assign a Static IP Address
One of the most important steps when using Raspberry Pi as an MQTT server is assigning a static IP address.
Without a static IP, the broker address may change whenever the router assigns a new address, causing ESP32 devices and dashboards to lose connection.
Open:
sudo nano /etc/dhcpcd.conf
Add a configuration similar to:
interface wlan0 static ip_address=192.168.1.150/24 static routers=192.168.1.1 static domain_name_servers=8.8.8.8
Save and reboot the Raspberry Pi.
sudo reboot
Adjust the IP addresses according to your network.
Step 5: Find Your Raspberry Pi IP Address
Verify the current IP address.
hostname -I
Example output:
192.168.1.150
This address will be used by ESP32 devices and Node-RED dashboards to connect to the broker.
Step 6: Test MQTT Communication
Open Terminal 1:
mosquitto_sub -h localhost -t home/test
Open Terminal 2:
mosquitto_pub -h localhost -t home/test -m "Hello MQTT"
Expected output:
Hello MQTT
Congratulations! Your Raspberry Pi MQTT broker is successfully receiving and delivering messages.
Understanding What Just Happened

Three MQTT components were involved.
Publisher
mosquitto_pub
Sends data.
Broker
Mosquitto
Receives and routes messages.
Subscriber
mosquitto_sub
Receives messages from subscribed topics.
This demonstrates the publish-subscribe model that makes MQTT efficient for IoT systems.
Connect ESP32 to the Raspberry Pi MQTT Broker
Once the broker is running, ESP32 devices can connect using:
Broker Address: 192.168.1.150 Port: 1883
In your ESP32 code:
client.setServer("192.168.1.150", 1883);
This allows ESP32 devices to publish sensor data and subscribe to control commands.
Continue with: ESP32 MQTT Publisher and Subscriber Using PubSubClient
Connect Node-RED to the Raspberry Pi MQTT Broker
Node-RED can also connect to the broker.
MQTT Server: 192.168.1.150
Port: 1883
Once connected, Node-RED can:
- Visualize sensor data
- Create dashboards
- Control devices
- Trigger automations
Continue with: Node-RED MQTT Dashboard
Verify Port 1883
Ensure Mosquitto is listening on the default MQTT port.
sudo netstat -tulpn | grep 1883
Expected output:
0.0.0.0:1883
This confirms the broker is ready to accept MQTT connections.
Common Use Cases
Smart Home Automation
- Smart switches
- Lighting control
- Fan control
- HVAC monitoring
Environmental Monitoring
- Temperature sensors
- Humidity sensors
- Air quality systems
Industrial Monitoring
- Machine status tracking
- Sensor data collection
- Alert generation
Agriculture
- Soil moisture monitoring
- Automated irrigation
- Weather stations
Troubleshooting
Mosquitto Service Won’t Start
Check status:
sudo systemctl status mosquitto
View logs:
journalctl -u mosquitto
Port 1883 Already in Use
Check which process is using the port.
sudo lsof -i :1883
Firewall Blocking MQTT
Allow MQTT traffic.
sudo ufw allow 1883 sudo ufw reload
What to Learn Next
Now that your Raspberry Pi MQTT broker is running, continue with:
- What is MQTT? A Complete Beginner’s Guide
- ESP32 MQTT Publisher and Subscriber Using PubSubClient
- Node-RED MQTT Dashboard
- MQTT Security with TLS
- MQTT-Based Smart Home Automation Using ESP32 and Node-RED
- MQTT Learning Path
Conclusion
Setting up a Raspberry Pi MQTT broker is one of the most useful projects for anyone interested in IoT. In just a short time, you’ve transformed a low-cost Raspberry Pi into a powerful communication hub capable of handling data from sensors, dashboards, automation platforms, and cloud services.
As your IoT projects grow, this broker can serve as the backbone of your smart home, monitoring system, or industrial application. The combination of Raspberry Pi and Mosquitto offers a reliable, scalable, and cost-effective solution for MQTT communication.
With the broker running successfully, you’re ready to connect ESP32 devices, build Node-RED dashboards, and create complete IoT solutions.
Frequently Asked Questions
What is an MQTT broker?
An MQTT broker is a server that receives messages from publishers and forwards them to subscribers based on topics.
Why use Raspberry Pi as an MQTT broker?
Raspberry Pi is affordable, energy-efficient, easy to maintain, and ideal for running MQTT services continuously.
Which MQTT broker is best for Raspberry Pi?
Mosquitto is one of the most popular choices because it is lightweight, free, and easy to install.
What port does Mosquitto use?
The default MQTT port is 1883. Secure MQTT communication typically uses port 8883.
Can Raspberry Pi run MQTT 24/7?
Yes. Many Raspberry Pi devices operate continuously as MQTT servers for smart homes and IoT deployments.
Can multiple devices connect to one MQTT broker?
Yes. A single MQTT broker can manage multiple publishers and subscribers simultaneously.
Can ESP32 connect to a Raspberry Pi MQTT broker?
Yes. ESP32 devices can publish and subscribe to MQTT topics hosted on a Raspberry Pi running Mosquitto.
Is Mosquitto free?
Yes. Eclipse Mosquitto is a free and open-source MQTT broker.

