Views, guides, tech stuff, Cryptocurrencies and ICOs from the mind of Andreas Ahrens


Setting up Mosquitto on Raspian Jessie

4th April 2016

Following up the last post, I decided to install [Mosquitto](" target="new) to be able to use [MQTT](" target="new) for communication between my Arduinos and my Raspberry Pi server. Mosquitto is a MQTT broker that lets you set up a server that Subscribe to a topic and then other machines can Publish to the topic. It's very well suited for low power devices like Arduinos.

I've slightly modified the guide at [Raspberry Pi forums](" target="new) to work for Jessie instead of Wheezy.

sudo apt-key add mosquitto-repo.gpg.key
rm mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo wget
sudo apt-get update
sudo apt-get install mosquitto mosquitto-clients

To test it, start two terminal windows/tabs. In one, enter

mosquitto_sub -d -t hello/world

This starts a subscription to the topic hello/world. You can also set up something like

mosquitto_sub -t sensors/rooms/+/temperature/+

To subscribe to room temperatures, that will be in the format sensors/rooms/LIVINGROOM/temperature/CEILING

To publish to this queue, enter this into the second terminal:

mosquitto_pub -d -t hello/world -m "Greetings from Terminal window 2!"

You will get a response like this:

Client mosqpub/14796-raspberry sending CONNECT
Client mosqpub/14796-raspberry received CONNACK
Client mosqpub/14796-raspberry sending PUBLISH (d0, q0, r0, m1, 'hello/world', ... (22 bytes))
Client mosqpub/14796-raspberry sending DISCONNECT

If you switch to the first terminal, you will see that you got the message:

Client mosqsub/14806-raspberry received PUBLISH (d0, q0, r0, m0, 'hello/world', ... (33 bytes))
Greetings from Terminal window 2!

I also wanted to try to get a message from my Mac to the Raspberry Pi. You first need [Brew](" target="new) if you don't have it. Then just run

brew install mosquitto

and to publish to the Raspberry Pi, run

mosquitto_sub -t hello/world -h raspberrypi.local

As you can see, you have to specify the actual machine name. If you don't, you just try to publish to your local host. In this case, my test Pi is has the hostname raspberrypi and thus the address raspberrypi.local.

In the end, I also decided I wanted the Python bindings for Mosquitto and ran

sudo apt-get install python-mosquitto

I'll now be reading the [Mosquitto Documentation](" target="new) to set up my broker as good as possible.

I very well might try out [CloudMQTT](" target="new) in the future, especially if I need to communicate between MQTT brokers across locations. They do have a free tier for 10 connections limited at 10 Kbit/s but that should suffice for simple connections between brokers.

View Comments