DIY Home Automation for less than $75: Part 1 – Configuring the MQTT Broker

3D Printing Inspiration MP CNC Product Reviews Technology Uncategorized Wood Working

The foundation of my home automation solution is an inexpensive Linux single board computer (SBC) and a product called a SonOff Basic smart switch. The two primary component needed on the server to automate the control the SonOff switches is OpenHAB and an MQTT Broker (Mosquitto).

 

 

Shopping Guide

Item Source
USB to FTDI Adapter
  Buy
Buy

Jumper wires with DuPont Connectors

 
Buy
Buy

 

OpenHAB is “vendor and technology agnostic open source automation software” for the home. It is the One Ring to rule them all. Like the the One Ring it seems simple at first glance, while really being complex. But like the Hobbits, the Wannabe Maker has taken on task of making the perilous journey and in doing so making mistakes so you don’t have to.

 

The SonOff devices and a lot of other hardware that exist or can be created by a hobbyist makes use of MQTT to communicate between devices and centralized servers. While OpenHAB does not contain an MQTT Broker it does support using a separate MQTT broker server thru an OpenHAB binding. Both OpenHAB and Mosquitto can run on the same SBC running Linux with power to spare.

Mosquitto, the MQTT broker, needs to be installed and tested before the OpenHAB server. If we can’t ensure Mosquitto is running and available on our network, we will not be able to SonOff switches and OpenHAB talk to each other. This article will concentrate of installing any needed Linux utilities; then installing and testing  Mosquitto server. Part II of this series will focus on installing and configuring the OpenHAB server. Part III will cover hacking the SonOff switch, updating the firmware, and setting it up to communicate with Mosquitto. Part IV we will begin laying out OpenHAB and integrating the SonOFFs so they can be controlled from anywhere in the home.

Prerequisites:

  • Your single board computer (SBC) is has a version of Linux installed, powered on, and connected to your network. I am using a Pine A64 with 2GB of memory running Ubuntu 16.04. This is overkill. In the future I plan on running my server on a 1GB board. You can also use a Raspberry Pi, but if your Rasbian distro may not use some of the same commands, that I do.
  • You also know the IP address of the SBC. The IP address should either be a static address or if you are using DHCP on your network, ensure your router is capable of reserving the address. The key is to make sure you servers IP address does not change if it ever rebooted for any reason.
  • Install some tools on your desktop and phone/tablet.
    • Putty: link here – This is a widely-used, free application for establishing
    • MQTT.FX: link here – Java application intended to be quick and easy to use tool for testing and debugging of MQTT. It allows me to see all MQTT messages in one location, regardless of the source.
    • MQTT Dashboard: Google Play Store – Android App for either your tablet or phone. With your Android device connected to your network, MQTT Dashboard allows you to subscribe to MQTT topics and publish MQTT messages. When publishing it provides the added benefit of GUI controls for switches, sliders, color pickers, etc.

Step 1: Connecting to server using Putty

Almost all of the Linux distros available for small board computers are configured to allow SSH connections. If for some reason you are using a distro that does not have SSH/Telnet configured, you will have to do so yourself or choose a distro that does.

When you first start putting you are presented with the following screen. In the Host Name field enter the IP for your server, select SSH under Connection Type, and to save yourself time later enter a name for the the connection configuration in the Saved Sessions field. Then click the save button. This will allow you to double click on the name in the listed under Save Sessions to quickly open a new connection to the server without having to remember the IP information and typing it in.

Almost all of the Linux distros available for small board computers are configured to allow SSH connections. If for some reason you are using a distro that does not have SSH/Telnet configured, you will have to do so yourself or choose a distro that does.

Now by double-clicking on the Save Sessions name, a terminal window will be open and you will see a Linux login prompt. Use the default user id and password for your Linux distro. Some distros may ask you to change the password.

sudo apt install oracle-java8-set-default
If you are using a Pine A64 and the Ubuntu 16.04 distro for it, the user id/default password is ubuntu/ubuntu.

TIP: Most people think that Putty does not have the ability to paste text from another window into the terminal. However this is not the case, you can copy the desired text, such as the commands listed in this article, using your favorite standard method such as CTRL-C. Then click the Putty terminal window to make sure it is active and has focus. Then right-mouse click. The text you copied to the clipboard will then be pasted where ever the cursor is in the terminal window.

Step 2: Install some Linux utilities to make later parts of the installation and setup easier.

You will be using “sudo” so will be prompt to enter your password again to execute the command with the required elevated permissions.

Install the command line editor “nano”. The nano editor is easier to navigate and using when you are editing configuration files and other text-based files. Some of the newer Linux distros may have it pre-installed, but executing the command to install it will be OK if it is already installed. Better to install it now, so you know it will be available later.

Ubuntu 16.04Debian Jessie
sudo apt-get install nano
sudo apt-get install nano

The command below will install add-apt-repository and  will make it easier to add new software repositories so they will be available for installation using the apt-get application. This will be very helpful installing the official Oracle JRE correctly.

sudo apt-get install software-properties-common

Install the official Oracle JRE. The official Oracle JRE is required by the OpenHAB server for it to run. Linux distros normally come with an unofficial open source version of the Java JRE. The commands below will add the official signed Oracle Java repository as a source to the Linux package manager, install the Official Oracle JRE, and then set it to the default version of Java.

Ubuntu 16.04Debian Jessie
sudo add-apt-repository ppa:webupd8team/java
sudo apt update
sudo add-apt-repository "deb http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main"
sudo apt update

Begin installing Oracle JRE.
sudo apt-get install oracle-java8-installer
javac -version

sudo apt-get install oracle-java8-set-default
javac -version

Step 3: Install Mosquitto and test locally

sudo apt-get install mosquitto mosquitto-clients
sudo systemctl status mosquitto

mosquitto_sub -h localhost -p 1883 -v -t switches/mySwitch01/status or mosquitto_sub -v -t "#"

Now, using Putty open a couple of additional terminal windows. You will use one of the windows to subscribe to a topic and receive messages from the MQTT broker. The second window will be used to publish messages to the MQTT broker. My screen looks like this.

Step 4: Test Mosquitto across the network

Comments are closed.