How to Use MQTT on Raspberry Pi: Simple Setup and Example
To use
MQTT on Raspberry Pi, first install an MQTT broker like mosquitto and a Python client library such as paho-mqtt. Then write Python scripts to publish and subscribe messages using paho-mqtt to communicate over MQTT.Syntax
MQTT communication on Raspberry Pi typically involves these parts:
- Broker: The server that routes messages (e.g.,
mosquitto). - Client: The device or script that sends (publishes) or receives (subscribes) messages.
- Topics: Named channels where messages are sent and received.
- QoS: Quality of Service level for message delivery (0, 1, or 2).
Python client code uses paho.mqtt.client with methods like connect(), publish(), and subscribe().
python
import paho.mqtt.client as mqtt client = mqtt.Client() client.connect("broker_address", 1883, 60) client.publish("topic/name", "message") client.subscribe("topic/name") client.loop_forever()
Example
This example shows how to install the MQTT broker and client, then run a simple Python script to publish and subscribe messages on Raspberry Pi.
bash/python
# Step 1: Install Mosquitto broker and client sudo apt update sudo apt install -y mosquitto mosquitto-clients python3-pip # Step 2: Install Python MQTT client library pip3 install paho-mqtt # Step 3: Python script to subscribe and publish import paho.mqtt.client as mqtt import time # Callback when a message is received def on_message(client, userdata, msg): print(f"Received message: {msg.payload.decode()} on topic {msg.topic}") client = mqtt.Client() client.on_message = on_message client.connect("localhost", 1883, 60) client.subscribe("test/topic") client.loop_start() # Publish messages every 2 seconds for i in range(3): message = f"Hello MQTT {i}" client.publish("test/topic", message) print(f"Published: {message}") time.sleep(2) client.loop_stop() client.disconnect()
Output
Published: Hello MQTT 0
Received message: Hello MQTT 0 on topic test/topic
Published: Hello MQTT 1
Received message: Hello MQTT 1 on topic test/topic
Published: Hello MQTT 2
Received message: Hello MQTT 2 on topic test/topic
Common Pitfalls
- Broker not running: Forgetting to start
mosquittocauses connection errors. - Wrong broker address: Using incorrect IP or hostname prevents connection.
- Not subscribing before publishing: Subscribers must be active to receive messages.
- Firewall blocking port 1883: Ensure port 1883 is open for MQTT traffic.
bash/python
## Wrong way: Not starting broker client.connect("localhost", 1883, 60) # Fails if mosquitto not running ## Right way: Start broker first sudo systemctl start mosquitto client.connect("localhost", 1883, 60) # Connects successfully
Quick Reference
| Command | Description |
|---|---|
| sudo apt install mosquitto mosquitto-clients | Install MQTT broker and clients |
| sudo systemctl start mosquitto | Start MQTT broker service |
| pip3 install paho-mqtt | Install Python MQTT client library |
| client.connect('broker', 1883, 60) | Connect Python client to broker |
| client.publish('topic', 'message') | Send message to topic |
| client.subscribe('topic') | Listen for messages on topic |
Key Takeaways
Install mosquitto broker and paho-mqtt client on Raspberry Pi to use MQTT.
Run the broker before connecting clients to avoid connection errors.
Use Python scripts with paho-mqtt to publish and subscribe messages easily.
Always subscribe before expecting to receive messages on a topic.
Check network and firewall settings to allow MQTT traffic on port 1883.