Using Rasberry Pi as a MQTT message broker

I got my Raspberry Pi up and running again (read how here), and I plan to use it as a message broker for a small sensor network at home. I haven’t decided exactly what I want to do, but I will need a message broker.

First, log in to your Raspberry Pi. I use ssh to access it:
ssh pi@192.168.2.5

Next, run:
sudo apt-get install mosquitto

Since most services start automatically when installed you should get a message like this:
Mosquitto installed

Now we want make sure it work properly by subscribing to a topic and passing some messages.
Open two terminal windows, and in one of them you write:
mosquitto_sub -h 192.168.2.5 -t raspberry/test
-h is for the messagebroker host, and -t is for the topic you want to subscribe to.

Use the other terminal window to publish a test message with:
mosquitto_pub -h 192.168.2.5 -t raspberry/test -m "First message"
The first two parameters are explained above, and the -m parameter is for the message.

You should now receive the message in the first window, like in the picture below.
mqtt_sub_pub

This was a short explanation for how to get started with using a raspberry pi as an MQTT message broker. You should now be able to use the message broker for whatever project your’re starting on.

Let me know what you think in the comment section below!

Leave a comment

Your email address will not be published. Required fields are marked *