Sytone's Ramblings

The occasional posts of a guy who plays with technology.

Setting up Mosquitto on your Pi as part of the home hub

2015-10-25 2 min read General

To enable all your nodes to communicate we are using a protocol called MQTT, on the pi we are using Mosquitto as the broker to support this. The broker allows nodes to publish information or subscribe to get information when it is published. MQTT uses simple channels to dictate where messages go, here are some examples:

HOME/Garage/DoorOne/State HOME/Garage/DoorTwo/State HOME/Garage/Temperature

The structure of the channel is up to you, just keep it short and logical! In these articles I will have them set to how I like them, feel free to change them as you need. To install Mosquitto on your Raspberry Pi run the following commands.

sudo apt-get install mosquitto mosquitto-clients

Now you have it installed lets test it to ensure it is working. You can do this in a few ways but I’m going to use mqtt-spy. Head to the github site and download the latest version (0.3.0 at the time of writing). Once you have it open it up and ensure a default configuration is created. Now you can add in your nice new broker. In the connections menu select new connection. The Server URI is the IP address of your Pi running OpenHab. To get this if you do not have it run ifconfig in a SSH session. Click on Apply and the Open the Connection. If everything is good you will have a new green tab at the top of the window, this is the connection to your Mosquitto broker instance. Right click on the tab and select Show Broker Statistics to see the $SYS channel and data. This is a good indication of traffice going to and from the broker and you have a connection up and running from another machine. To test subscribing and publishing make a subscription to test/my/channel in the new subscription section and press enter. In the publish section enter test/my/channel as the topic and Hello World as the data (what else…). Press publish and you will see it turn up in the subscription you just made. mqtt-spy is a handy tool to have around so keep it in your home automation toolbox. You now have Mosquitto installed and running on your system. Next we need to configure OpenHab to work with MQTT to send and receive messages.