ESP8266 based HomeKit accessories

What is this about?

This video should give you an idea what we’re trying to achieve here. Of course HomeKit is a lot more powerful than just being able to switch a power outlet.

Background

Apple announced HomeKit a while ago. Lately, quite a few HomeKit devices have been showing up at stores. Despite being convinced about HomeKit's awesomeness I haven’t really bought a single official accessory yet. A few months ago, I stumbled upon thesevideos. They opened up a whole new world of ESP8266 to me. In the next few weeks I got this working with NodeMCU firmware running on the ESP8266. It did work but frankly I didn't find the Lua firmware very appealing.

Since then I've been tinkering a lot with the esp8266/Arduino port. Last week I published this Christmas Light project. Again, it does work, but I'd rather not open a webpage every time I want to turn on the Christmas light. So we are back to HomeKit!

Involving HomeKit perhaps makes this HelloWorld kind of project a little too complicated. However, once you get it working, it will definitely be fun to explore.

Topology

Let’s talk about that video a litte. Being completely new to ESP8266, Node, MQTT, etc. I was really cofused about how this stuff was working. Now that I have the same thing working, let me try and explain a little. Here are the components involved:

HAP-NodeJS running on the Raspberry Pi

MQTT Broker running on the Raspberry Pi

iPhone with the HomeKit accessory paired and configured

LED connected to an ESP8266 module

And of course a wireless router

Here’s how it works: On boot up HAP-NodeJS server connects to a MQTT broker (which is also happens to be running on the Raspberry Pi) so that it can publish messages on a certain topic. The ESP8266 module also connects to the MQTT broker and subscrbes to a certain topic. Telling Siri to turn on/off the light, triggers a certain accessory action on the HAP-NodeJS server. This action publishes a message "LightOn" on the topic "Light". The ESP8266, which has also subscribed to same topic, receives the message, parses it, and turns on the LED.

Let’s make it work

I’m assuming you have a working Raspberry Pi running Raspbian. If not go here and read how to get that working.

HAP-NodeJS

These instructions are specific to RaspberryPi. I didn't want to keep a computer running all the time, so I chose to install HAP-NodeJS on a RaspberryPi. You might wanna install HAP-NodeJS on a computer too just as a backup option for testing.

All this sketch does is to connect to WiFi, connect to the MQTT broker and subscribe to the topic “AdyLight”. Once it receives a message on that topic, it parses it and toggles the relay GPIO accordingly.

Screen: Since we’re gonna have two things running on the RPi, HAP-NodeJS and the MQTT broker, we need a way to monitor both these processes separately. Right now I’m just using two separate terminal windows to SSH into the RPi. Screen would be a better option, once you have both these processes running at boot up.