House Automaton: fun with MQTT


Published: March 2017

Recently my girlfriend got some of this old fashioned timer-based (non-digital!) AC switch timers. As a digital boyfriend I couldn't let this go unpunished so I replaced them with truly digital stuff :)

The hardware

I just got some really cheap Itead S20 devices that cost 10EUR and can be found on ebay or many other online shops. They come with our beloved ESP8266 that can connect to your home's wifi and interact over the internet.

Itead S20

Opening the device reveals a 4 pin header that maps to a UART as you probably expected. Also, the main front button happens to map to the GPIO0, which in the ESP world means boot to UART. That allows us to flash the device just by using a UART peripheal and pressing a button.

The software

As soon as I got one of them I flashed it with Espurna, by Xose Perez (kudos man!). That firmware is very simple but so effective and nicely built. It just requires some effort to build and flash it but it's pretty straightforward really. I managed to find a bug regarding authentication while using it, so make sure you use a recent revision and not a snapshot. Once flashed the device will boot and create a wifi AP which can be used to update the settings and make sure it connects to our wifi.

Once the device is up and running I tried to use mosquitto, a nice and lightweight MQTT server written in C. That's just awesome since it will allow us to control it from any other MQTT client. In my case I used user/pass authentication, plaintext. I know it sucks but I don't think it supports SSL just yet. Anyway, as long as my server has some minimal auth form we should be good to go.

Automation clients anybody?

I started to hunt for clients that would allow me, well my gf, to control the heating system from her mobile. But it's not that simple, it seems that most apps out there are really dodgy or are designed with another goal, like fully automating heating using sensors and PID loops and alike. Since she's not really into python programming just yet, I created a simple web based client to do the job.

The client is written in C++, cause I was too lazy to fight again with web.py, and cause I already had built myself a tiny C++ MQTT library (+CLI!) and a nice C++ http server (borrowed from another project). The client is mostly focused on controlling systems that do not require any feedback, like heating or lighting. It can be used for other stuff though with minimal modifications. The working paradigm is to asume that all devices are off by default and we can schedule a weekly plan to turn them on. Also, we have the concept ofan override, which is a one-time scheduled event starting right now that overrides whatever the schedule says, by turning something ON or OFF, this allows to start or stop heating outside of regular scheduling, cause you know, routine sucks :)

The interface looks like this:

Main screen UI

Add override

Scheduler UI

The screenshots are taken in something like a "mobile sized screen", the idea is that we don't need an App and at the same time it's possible to control from a PC, tablet, etc. The only requirment is that the controlled device has an endpoint that can be driven using "0" and "1" and that it can work over MQTT of course.

In my case I deployed it on a small server (VPS) behind an nginx server, using the reverse HTTP proxy option. Also I added user/pass basic auth for it so no one can find the endpoint and switcht the buttons. The C++ server supports HTTPs (using gnutls library) so it could run on its own using a proper SSL certificate. That means you can probably run it in a router or similar low-memory device (the binary is very small and resources are compiled in, except for CSS/JS files which are pulled from the internet).

You can use it as well! Check the github repo: https://github.com/davidgfnet/home_automaton