How to Deploy an MQTT Broker on Raspberry Pi
Table of Contents
Introduction to MQTT and Raspberry Pi
MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol crucial for IoT (Internet of Things) applications. It facilitates efficient communication between devices using a publish-subscribe model, enabling scalability, low bandwidth usage, reliability, flexibility, and interoperability.
Raspberry Pi is a credit-card-sized single-board computer developed by the Raspberry Pi Foundation, initially intended to promote computer science education. However, its affordability, versatility, and low power consumption have made it immensely popular in IoT applications.
Use Cases of Using MQTT on Raspberry Pi
Projects deployed on Raspberry Pi extend MQTT's reach, making IoT development accessible to a broader audience.
- Home Automation: With MQTT, you can control and automate smart home devices such as lights, thermostats, door locks, and security systems connected to the Raspberry Pi. This enables personalized home automation solutions tailored to your preferences.
- Remote Monitoring and Control: You can remotely monitor and control Raspberry Pi-connected devices over the internet using MQTT. This is useful for scenarios like checking home security cameras, adjusting thermostat settings, or receiving notifications from IoT sensors while away from home.
- Low-Cost Solution: Raspberry Pi offers a cost-effective solution for deploying an MQTT broker compared to dedicated hardware or cloud services. This makes it accessible for DIY projects, small businesses, and educational institutions with budget constraints.
What is NanoMQ
NanoMQ is an ultra-lightweight and blazing-fast Messaging broker/bus for IoT edge & SDV written in C, which is suitable for resource-constrained devices like Raspberry Pi.
The advantages of using NanoMQ on Raspberry Pi are as below:
- Lightweight and Efficient: NanoMQ is designed to be lightweight, minimizing resource consumption on Raspberry Pi, which is particularly beneficial for devices with limited processing power and memory.
- Ease of Deployment: NanoMQ's simplicity makes it easy to install and configure on Raspberry Pi, allowing for quick setup and deployment of MQTT broker functionality without extensive technical expertise.
- Low Latency Communication: NanoMQ prioritizes low-latency communication, ensuring fast and responsive data exchange between IoT devices connected to the Raspberry Pi, ideal for time-sensitive applications.
- Compatibility: NanoMQ is compatible with a wide range of MQTT clients and libraries, enabling seamless integration with various IoT devices and platforms and making it versatile for diverse IoT projects.
- Scalability: Despite its lightweight nature, NanoMQ offers scalability, allowing it to handle increasing numbers of connected devices and messages as IoT deployments grow, making it suitable for both small-scale and large-scale applications.
- Community Support: While NanoMQ is lightweight, it benefits from an active community of developers who contribute to its improvement, providing ongoing support, updates, and enhancements, ensuring its reliability and stability over time.
- Cost-Effectiveness: NanoMQ is open-source software, eliminating licensing fees and reducing overall project costs, making it an economical choice for IoT projects deployed on Raspberry Pi.
Set up Raspberry Pi for MQTT Broker Deployment
- First, choose your raspberry. Here, we use Raspberry Pi 4b as an example.
- Install the OS. You can install Debian, Raspberry Pi OS, or any other Linux-based OS you choose.
- SSH and login, or just open a terminal.
Install and Configure NanoMQ MQTT Broker
Install NanoMQ
Download a package here.
Raspberry has an arm64 CPU. So we choose this package.
$ wget https://github.com/nanomq/nanomq/releases/download/0.21.8/nanomq-0.21.8-linux-arm64-full.deb
Then install this package via apt.
$ sudo apt install ./nanomq-0.21.8-linux-arm64-full.deb
We now finished the installation.
$ nanomq --version
Usage: nanomq { start | stop | restart | reload } [--help]
NanoMQ Messaging Engine for Edge Computing & Messaging bus v0.21.8-8
Copyright 2023 EMQ Edge Computing Team
Configuration
Here, we edit the configuration file of NanoMQ /etc/nanomq.conf
. In this configuration, NanoMQ will listen to 1883
port and accept tcp connections. It will also listen on port 8883 to accept TLS connections and port 8083 to accept WebSocket connections. The log will be printed to /tmp/nanomq.log
and console.
vim /etc/nanomq.conf
listeners.tcp {
bind = "0.0.0.0:1883"
}
listeners.ssl {
bind = "0.0.0.0:8883"
keyfile = "/etc/certs/key.pem"
certfile = "/etc/certs/cert.pem"
cacertfile = "/etc/certs/cacert.pem"
verify_peer = false
fail_if_no_peer_cert = false
}
listeners.ws {
bind = "0.0.0.0:8083/mqtt"
}
log {
to = [file, console]
level = info
dir = "/tmp"
file = "nanomq.log"
}
Start NanoMQ.
nanomq start --conf /etc/nanomq.conf
For more information about the configuration of NanoMQ, please refer to: NanoMQ Docs.
Testing NanoMQ on Raspberry Pi
Open two additional terminals.
In terminal 1:
$ nanomq_cli sub -p 1883 -t topic1
In terminal 2:
$ nanomq_cli pub -p 1883 -t topic1 -m "Hello-NanoMQ"
If the nanomq_cli in terminal 1 gets the message Hello-NanoMQ, it shows that you have deployed NanoMQ on Raspberry Pi successfully.
FAQ
Can NanoMQ be installed on Raspberry Pi 1, 1B, and 2?
Yes. But no GitHub releases were provided. You need to cross compile NanoMQ by yourself. The tutorial on cross-compile can be found here: How to Install a Scalable MQTT Broker on OpenWRT
What are the hardware and OS requirements of NanoMQ?
NanoMQ requires at least 10MB of memory. It can be deployed on any POSIX-compatible operation system.
Conclusion
In wrapping up, we've covered the essential intersection of MQTT and Raspberry Pi for IoT projects. We've seen how MQTT facilitates efficient communication, while Raspberry Pi serves as an accessible and versatile platform.
Deploying an MQTT broker like NanoMQ on Raspberry Pi offers significant advantages, from local processing to scalability. With NanoMQ's lightweight design and ease of deployment, it's an excellent choice for Raspberry Pi-based projects.
Now equipped with the know-how, it's time for you to explore, experiment, and implement your MQTT projects on Raspberry Pi.
Resources
- MQTT on ESP32: A Beginner's Guide
- A Developer's Journey with ESP32 and MQTT Broker
- A Guide on Collecting and Reporting Soil Moisture with ESP32 and Sensor through MQTT
- Using MQTT on ESP8266: A Quick Start Guide
- Remote control LED with ESP8266 and MQTT
- How to Use MQTT on Raspberry Pi with Paho Python Client
- MicroPython MQTT Tutorial Based on Raspberry Pi