Software apps and online services
This is part 3 of our 4 part series documenting our home automation system we lovingly call HAL.
- Part 1 - Overview. We lay out the main components of our home automation system and install the core software component, OpenHAB
- Part 2 - Data Persistence. We install a MySQL database server and tools to manage it. We then integrate it with OpenHAB for the purposes of storing our home automation data indefinitely.
- Part 3 - Messaging. We install a lightweight messaging system (MQTT broker), Mosquitto, and integrate it with OpenHAB to exchange information with custom, home-grown IoT sensors and controls.
- Part 4 - Custom Interfaces. We install and configure an Apache web server and briefly discuss integrating custom web pages with OpenHAB. This will provide a platform for building custom user interfaces for our home automation system.
In this write-up, we will install a Mosquitto MQTT broker and configure our OpenHAB server to communicate with it. This will allow OpenHAB to integrate with several of our home-grown devices.Prerequisite
This write-up assumes you have already installed and configured Raspbian on a Raspberry Pi. For details, please see Part 1 - OverviewBackground
We needed a way for OpenHAB to talk to several of our custom IoT devices. Not only devices we have now, but ones we may create in the future. We needed a common, easily implemented protocol, or "language", that OpenHAB and many other platforms can utilize. MQTT quickly rose to the top of the list of candidates.
MQTT is a lightweight, machine-to-machine/IoT connectivity protocol. Sounds promising. OpenHAB has an MQTT binding so it "speaks" MQTT. Several of our current and planned devices are based on Particle Photons. There is an MQTT library available for the Photon. Same with Arduino and many other common IoT and home automation platforms. MQTT is just what the doctor ordered!
The heart of any MQTT implementation is the "broker". This is the hub that processes all the messages being passed between different devices. MQTT utilizes a publish/subscribe communication strategy. Devices can "subscribe" to "topics". Any time any device publishes a message for a particular topic, the broker will receive that message and then resend it to all the other devices that are subscribed to the specified topic. Nifty!
All we needed was a broker software that could be installed on a Pi. Once again, a simple Google search turned up a clear winner: Mosquitto. Very popular, with many successful implementations on the Pi.
Installation of Mosquitto is fairly straightforward using the apt repository management tool. We used this page as a guide. Here's the high-level process:
- Add keys to your Pi
- Add the Mosquitto repository to apt's repository list
- apt-get it
Open a console/command line interface to the Pi where you want to install Mosquitto. Execute the following commands in order:
pi@zapper:~ $ cd /usr pi@zapper:~ $ sudo mkdir keys pi@zapper:~ $ cd keys pi@zapper:~ $ wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key pi@zapper:~ $ sudo apt-key add mosquitto-repo.gpg.key pi@zapper:~ $ cd /etc/apt/sources.list.d/ pi@zapper:~ $ sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list pi@zapper:~ $ sudo apt-get update pi@zapper:~ $ sudo apt-get install mosquitto
You can also install the Mosquitto client on your Pi. This can be handy for troubleshooting or just watching pubs and subs go by:
pi@zaper:~ $ sudo apt-get install mosquitto-clients
We prefer more graphical client - MQTTLens. It's a Chrome browser app. Free and easy to use.
That's it for the Mosquitto side of things. Time to configure OpenHAB to talk to Mosquitto
OpenHAB - Mosquitto Integration
First, we need to install the MQTT binding in OpenHAB
Fire up the Paper UI - point a web browser to http://<your_OpenHAB_ip:8080>/paperui
Once the binding is installed, we need to configure it to connect to our Mosquitto instance. This is an older binding and has to be configured by editing a text file. Launch a console/command line session against the Pi where you installed OpenHAB and navigate to OpenHAB's services folder:
pi@HAL:~ $ cd /etc/openhab2/services
Here, there should be a file named "mqtt.cfg". We need to edit this file:
pi@HAL:/etc/openhab2/services $ sudo nano mqtt.cfg
There's only one line you have to change:
# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883 bloodsucker.url=tcp://192.168.1.127:1883
This gives a name to your MQTT broker that OpenHAB will use to connect to it. In our case, we've used the name "bloodsucker" (cheeky, no?). You also need to put the IP of the Pi where Mosquitto is installed. Leave the port at its default, 1883.
Once you've edited the URL, you can close and save the file:
- Hit control-X
- Nano will prompt to save the file. Enter Y to do so
- Nano will prompt for a file name. Hit [Enter] to write your changes to the same file.
Now items in OpenHAB can be configured to send and receive messages via Mosquitto.Conclusion
Look for an upcoming project write-up where we actually integrate one of our home-grown devices with OpenHAB via Mosquitto.
Head on over to the final installment: Step 4 - Custom Interfaces