What is this about?

<iframe align=“middle” width="420" height="315" src="http://www.youtube.com/embed/tGQj1bp5cOY" frameborder="0" allowfullscreen></iframe>

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.


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 these videos. 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.


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.


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.

Install required libraries

  • HAP-NodeJS requires some libraries, let’s install them first
sudo su
apt-get install git-core libnss-mdns libavahi-compat-libdnssd-dev -y

Install Node

You can get the link to the latest version of Node from here. I meant to install Node v4.0 but installed v5.2 by mistake. It works anyway, at least so far.

  • Install node and some dependencies with the following commands

      wget https://nodejs.org/dist/v5.2.0/node-v5.2.0-linux-armv7l.tar.gz
      tar -xvf node-v5.2.0-linux-armv7l.tar.gz 
      cd node-v5.2.0-linux-armv7l
      sudo cp -R * /usr/local
      npm install -g node-gyp

Install HAPNodeJS

git clone https://github.com/KhaosT/HAP-NodeJS.git
cd HAP-NodeJS/
npm install node-persist && npm install srp && npm install mdns --unsafe-perm
npm install debug
npm install ed25519 --unsafe-perm
npm install curve25519 --unsafe-perm
npm install mqtt --unsafe-perm

Try running HAP-NodeJS node BridgedCore.js, should looks something like this


MQTT Broker

Now that HAP-NodeJS is running, next thing we need is an MQTT broker so that we can send and recieve commands to and from the ESP8266 modules.

  • Install an MQTT broker Mosca

npm install mosca bunyan -g --unsafe-perm

  • Run it

mosca -v | bunyan

If mosca runs without problems you should see something like this


ESP8266 Arduino sketch

You can find the source code for this part on GitHub at https://github.com/AdySan/HomeKitLight


<img src="http://AdityaTannu.com/images/christmas_light.png" align="middle"alt="Christmas Light" height="600">

For the ESP8266 side, I’m using the latest git version of the Arduino port of ESP8266. For the Arduino IDE, I’m specifically using arduino-PR-4107-BUILD-421 because it supports OTA firmware updates.

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.

MQTT Library

The sketch is based on an example in the PubSubclient MQTT library for ESP8266.

Making a new HomeKit accessory

You can refer to my fork of HAP-NodeJS here

  • You just need to copy and existing accessory, in this case I copied Light_accessory.js to AdyLight_accessory.js

  • Add connection to MQTT broker

// MQTT Setup
var mqtt = require('mqtt');
var options = {
  port: 1883,
  host: '',
  clientId: 'AdyPi_MQTT_Publisher'
var client = mqtt.connect(options);
  • Publish on topic “AdyLight”
  setPowerOn: function(on) { 
    console.log("Turning AdyLight %s!", on ? "on" : "off");

    if (on) {
			client.publish('AdyLight', 'on');
   	} else {
  • If everything works well, you should see something like this on HAP-NodeJS and mosca


mosca connected

HomeKit accessory pairing

Unfortunately Apple doesn’t provide a stock app for HomeKit. For adding and pairing the HomeKit accessory, you’ll need something. The most polished app I’ve seen is Matthias Hochgatterer’s Home app.

If you can build your own app, try Apple’s official HomeKit Catalog app. The maker of HAP-NodeJS, KhaosT, also has his own version of this app.

For Pairing use this pin code from the HAP-NodeJS source

light.pincode = "031-45-154";

Here are some screenshots:

<img src="http://AdityaTannu.com/images/HM1.png" align="middle"alt="Circuit" style="heigh: 400px;"> <img src="http://AdityaTannu.com/images/HM2.png" align="middle"alt="Circuit" style="heigh: 400px;"> <img src="http://AdityaTannu.com/images/HM3.png" align="middle"alt="Circuit" style="heigh: 400px;"> <img src="http://AdityaTannu.com/images/HM4.png" align="middle"alt="Circuit" height=“600"> <img src="http://AdityaTannu.com/images/HM5.png" align="middle"alt="Circuit" height=“600"> <img src="http://AdityaTannu.com/images/HM6.png" align="middle"alt="Circuit" height=“600"> <img src="http://AdityaTannu.com/images/HM7.png" align="middle"alt="Circuit" height=“600"> <img src="http://AdityaTannu.com/images/HM8.png" align="middle"alt="Circuit" height=“600”> <img src="http://AdityaTannu.com/images/HM9.png" align="middle"alt="Circuit" height=“600"> <img src="http://AdityaTannu.com/images/HM10.png" align="middle"alt="Circuit" height=“600"> <img src="http://AdityaTannu.com/images/HM11.png" align="middle"alt="Circuit" height=“600"> <img src="http://AdityaTannu.com/images/HM12.png" align="middle"alt="Circuit" height=“600"> <img src="http://AdityaTannu.com/images/HM13.png" align="middle"alt="Circuit" height=“600"> <img src="http://AdityaTannu.com/images/HM14.png" align="middle"alt="Circuit" height=“600">



<img src="http://AdityaTannu.com/images/AdyLightOn.PNG" align="middle"alt="Circuit" height="600"> <img src="http://AdityaTannu.com/images/AdyLightOff.PNG" align="middle"alt="Circuit" height="600">

Further reading

  • Here’s an Philips Hue clone I made
  • MQTT Lens: This Chrome app is super useful in testing MQTT