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.