As you may or may not know, Apple has a technology called HomeKit. Put simply, it allows you to control devices (lights, heating, etc) from your iPhone, and it integrates with Siri.

Several companies make HomeKit enabled devices (e.g Phillips Hue) that you can easily install to take advantage of this… but what is the fun of that?

Luckily, there exists a very good HomeKit Java library, so armed with that, an ESP8266 I set out to see if I could make my own HomeKit enabled devices.

The rough plan was, have the ESP8266 handle the hardware side of things (e.g actually turn things on and off, via a relay) and built a Java application that acts as a HomeKit Bridge.

The first version of the application I built was very basic, it essentially used the example code provided with the library, but one thing I quickly learnt is that HomeKit is like a database, rather than an actual application. You need an app to add entries into the database in order to use it.

Elgato Eve is one such app (which is designed to work with their devices) or there is Home that is designed to work with any HomeKit device (commercial or otherwise). Another option is Apple’s own ‘example code’ called HomeKit Catalog, if you have a Mac with Xcode installed, you can install this on your device (see here for instructions)

I went with HomeKit Catalog, so with that installed and running I was able to test my application. Initially, it worked but would not maintain pairing if the Java application was stopped and re-started, to spite me persisting the data that was required for this. I solved that issue (a method returning weather a device has been paired was not implemented correctly) and was able to move onto the next problem…

How to communicate with the ESP8266 (and actually turn things on and off!) I decided to go with the MQTT protocol for this, its a lightweight and fairly secure publish/subscribe protocol that is often used in low bandwidth/low power applications. The ESP8266 is a very powerful piece of kit, so by using this I have plenty of headroom for other things.

MQTT uses a client/server architecture, meaning I would need a MQTT Broker (server) running, and both my Java application and my ESP8266 would be clients, with the ESP8266 listening for publications from the Java application in order to know when to turn the attached relay on and off. Eclipse make a very good MQTT client library (and application) called Paho that I used in my project. For my MQTT Broker I used Moquette (which I could integrate in to my Java application at a later date)

Next, I had to program the ESP8266. I have done a lot of tinkering in the past with Arduinos, and since you can now program an ESP8266 with the Arduino IDE, I used that. The sketch is fairly simple, it just listens to a topic (A, or B) and then for a value of 1 or 0 (on or off) and sets a GPIO pin high or low, triggering a relay.

So after adding the Paho library to my Java application, I had a means to communicate easily between the Java application and the ESP8266, so time to put it all together and try it out.

And it worked! The Java application is running on my MacBook, so the next step is to get this running on my raspberry pi (and perhaps integrate control of the GPIO pins) as well as support for devices other than simple switches.

I am also working on a design for a ESP8266 relay board that I will talk about in more detail in a future post.

If you want to try this for yourself, the code is available on my github, click here for the Java application and here for the ESP8266 sketch.

A word of warning: working with mains electricity (in this case 240v) is dangerous! Do not attempt this unless you know what you are doing, regardless I take no responsibility for your actions and any damage caused by them.

5 thoughts on “Siri, turn on the light (Part 1)

  1. Just wanted to say thanks for the link to my post! I’ll be looking forward to your update on the esp8266 relay board. Do you have a schematic anywhere for how you decided to wire up the esp8266?

      1. I had seen this, the Java library that I use is based on the NodeJS HomeKit bridge. I chose Java though because it is the language I am most proficient with. I have actually successfully run my code on my raspberry pi, and will build an optimised version for it eventually

Leave a Reply

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