Home automation – the beginning

In the year of 2019 home automation is finally starting to be a thing. However the industry is walking in an uncharted territory where most of the companies are coming up with their very own devices, protocols and mobile apps. And if you don’t want to use a separate app to switch on your lights and another to open the door you can sometimes even connect them to a walled garden network proprietary smart-home framework or even your home surveillance device assistant!

OK now seriously. Smart home systems are necessarily handling very sensitive personal information. Depending on how you set them up they can know when you are home, what other people are there, when you don’t come home for the night, and many other things that you should keep under your control and not upload to random servers.

So, what would I like to achieve in my humble example home? Let’s see:

  • Lights
    • Switch on and off the room lights according to time of the day and my presence
    • Switch the outside lights when the sun sets
  • Security
    • Unlock the door using bio-metric identification
    • Arm or disarm the alarm system based on my presence
    • Automatically notify of any security incident
    • Integrate motion sensors, cameras, …
  • Monitoring
    • Measure how much home appliances use energy and calculate the cost
    • Measure temperature, humidity, CO level, …
  • Control
    • Smart climate control – different temperature at different day times
    • Keep water system from freezing (boiler and pipes)
  • Remote
    • Allow remote control and notification

Luckily there are free software systems to help with all that.

One of the promising systems that allows you to keep your private info for yourself and configure everything the way you want is OpenHAB. It’s a framework that runs on your home server of choice (for example Raspberry Pi) and serves as a hub that all devices connect to. It can talk many protocols and is extensible by add-ons. With these you can for example connect your fancy RGB light bulbs.

There are other systems like Mozilla WebThings and HASS but OpenHAB seems like the most established one at the moment.

As a first step I decided to hook up a temperature sensor and remotely control a LED. The whole concept was to run OpenHAB server on a Raspberry and connect the sensor and LED to an ESP32. The two would communicate over MQTT. Hardware needed

  • Raspberry Pi
  • Wroom32 – ESP32 board
  • DHT22 sensor
  • Router (LAN + WiFi)

First step is to install OpenHAB on a Raspberry. OpenHAB maintains it’s own Raspbian modification called Openhabian. Just download the image, flash it, run and go through the installation process. Then you should be able to connect to your OpenHAB server. Simple.

Next install the MQTT binding. The easiest way is to use openhabian-config to install the mosquitto server and then connect to it from OpenHAB using the Paper UI. Paper UI is the new configuration interface. It is meant to replace the old way of configuration using hand-written config files. It seems to be a work in progress but usable.
Note: since version 2.4 OpenHAB has a reworked MQTT/mosquitto integration. Everything can be configured from Paper UI. Don’t follow the old tutorials that insist on writing configuration files by hand.

Create a MQTT thing that represents the ESP32. Each property it controls/reads should have a channel. In my case three channels

  • Temperature (MQTT state topic “esp32/temperature”)
  • Humidity (MQTT state topic “esp32/humidity”)
  • LED control (MQTT command topic “esp32/led”)

For each channel create a “thing”. This thing further represents the property – for example you can add things to UI.

Now you can test how if you did everything right by using mosquitto_pub and mosquitto_sub.

Next the ESP32 is on. The DHT22 sensor is easy to connect. Just power it from the 3V3 rail and connect the data pin to any digital input. To be sure use a pullup for the data line. 10k seems to work. The easiest way to program the ESP32 is to use the Arduino IDE with ESP32 libraries. Be careful not to use the ESP8266 versions. Also download libraries for MQTT and DHT22 (again be careful not to use DHT11). These libraries have examples that will make it very easy to set everything up. Register to the same topics as you set up in the OpenHAB server and you are good to go!

Connecting to ESP32 board over MQTT

Now you can add items to the Basic UI using a sitemap (necessary to write a config file by hand) or add them to a HABPanel UI by clicking – easier.

Displaying measured values and control switches in Basic UI

As a next step you can use persistently store measured values using InfluxDB, display the values in Grafana and even use the OpenHAB Android app – more about that maybe later.

Displaying measured values in Grafana
Android OpenHAB app

As you can see OpenHAB offers quite a lot of options for building a home automation system. Working with it is not exactly the smoothest user experience there is but it seems to get the work done. And if it can’t, you can just write a plugin to do what you need. Try it too!

Leave a Reply

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