Introduction: Raspberry Pi 2 Homekit - From Zero to Hey, Siri (Updated)

UPDATE:

Added the "ready to use" image file link to the youtube video description.

Download, write to SD, and ready to use.

UPDATE 2:

Rpi ZERO version:

Hi,

In this instructable, i will show you, how to install Homekit Bridge to your RPi 2, and control your home accessories.

There is a youtube video too, with step by step tutorial.

For me, Homekit on the RPi2 not worked well with other OS (Raspian), only with a Jessie image.

Tutorial video:

STEP 1 >>

Step 1: Step 1: Install Jessie

Tools needed:

- Jessie OS (version 2015-08-30)

http://sysprogs.com/files/SD/2015-08-30-jessie-ras...

- WinFlashTool (for write image to SD)

http://winflashtool.sysprogs.com/

Download the image and the WFTool from the links, put the image file into the WFTool, and install the OS to the SD

(First step in the video)

Step 2: Step 2: Installs, Installs Everywhere...

1:

Boot the RPi2 with Jessie.

Open a terminal program on your PC/MAC (im using putty)

Connect to your RPi2 with the putty (RPi2 ip found in your router DHCP client list)

Username: pi

Password: raspberry

In the youtube video find this step at 1:17.

2:

next, the ragular update and upgrade:

sudo apt-get update && sudo apt-get upgrade -y

In the youtube video find this step at 1:35.

3:

Add yourself superuser privileges (less "sudo" type)

sudo su

In the youtube video find this step at 2:13

4:

Install python-dev and python-pip

apt-get install python-dev python-pip

In the youtube video find this step at 2:21

5:

Install libavahi and libdnssd

apt-get install libavahi-compat-libdnssd-dev

In the youtube video find this step at 2:27

6:

Download and install the latest node

wget http://node-arm.herokuapp.com/node_latest_armhf.d...

dpkg -i node_latest_armhf.deb

In the youtube video find this step at 2:37

7:

Check node and npm version and installation

node -v

npm -v

8:

Clone Homekit Bridge from github, and go into the HAP-NodeJS folder

git clone https://github.com/KhaosT/HAP-NodeJS.git

cd HAP-NodeJS

In the youtube video find this step at 3:04

9:

There is a lot of missing modules, so install all of them in one command.

npm install -g node-gyp
npm install node-persist && npm install srp && npm install mdns && npm install ed25519 && npm install curve25519 && npm install debug && npm install python-shell

In the youtube video find this step at 3:37

10:

Rebuild

npm rebuild


Step 3: Step 3: Add New Light Accessory

1:

Go into accessories folder, and make a new light accessory:

cd accessories

nano myLight_accessory.js

In the youtube video find this step at 5:13

2:

Copy in this empty .js file everything from the linked homekit.txt file, from the

//Light accessory line

Homekit.txt link:

https://drive.google.com/file/d/0B6GR9Hj5Ut61bDZle...

Rename the accessory, change a little the "username" (looks MAC address, watch out the HEX rules)

Save the file and exit

In the youtube video find this step at 5:46

Step 4: Step 4: Make the Python Files to Control GPIO Pins

1:

in the HAP-NodeJS folder, make a new python folder, go into.

cd ..

mkdir python

cd python

In the youtube video find this step at 7:07

2:

Make the GPIO "on" and "off" python file

nano light1.py

Copy this into:

import RPi.GPIO as GPIO
GPIO.setwarnings(False)

GPIO.setmode(GPIO.BOARD)

GPIO.setup(16, GPIO.OUT)

GPIO.output(16, 1)

Save, and close the file

nano light0.py

Copy this into:

import RPi.GPIO as GPIO
GPIO.setwarnings(False)

GPIO.setmode(GPIO.BOARD)

GPIO.setup(16, GPIO.OUT)

GPIO.output(16, 0)

Save, and close the file

In the youtube video find this step at 7:19

3:

Connect a led GND into one of the RPi2 GND pin. Im used the outer line 3. pin, and connect the led "+" pin tho the RPi2 GPIO.16 pin. Thats the 8. pin in the outer line.

Test the python codes:

python light1.py (led turn on)

python light0.py (led tudn off)

If everything ok, go back to HAP-NodeJS folder.

cd ..

npm rebuild

In the youtube video find this step at 8:14

4:

Start the Homekit Bridge.

node Core.js

Step 5: Stap 5: Config Ios Device

1:

In the video, im using Apple HMCatalog app, but find better homekit app, like EVE elgato.

Add a new home, some room, and one of the rooms, add your new light accessory.

The PIN: 03145154

Add anyway.

In the youtube video find this step at 9:28

2:

Test it, enjoy it!

Thank you.

Comments

author
DanieleM26 (author)2017-08-15

hi wehn i run "sudo node Core.js" it returns

*** WARNING *** The program 'node' uses the Apple Bonjour compatibility layer of Avahi.

*** WARNING *** Please fix your application to use the native API of Avahi!

*** WARNING *** For more information see <http://0pointer.de/avahi-compat?s=libdns_sd&e=node>

*** WARNING *** The program 'node' called 'DNSServiceRegister()' which is not supported (or only supported partially) in the Apple Bonjour compatibility layer of Avahi.

*** WARNING *** Please fix your application to use the native API of Avahi!

*** WARNING *** For more information see <http://0pointer.de/avahi-compat?s=libdns_sd&e=node&f=DNSServiceRegister>

HAP-NodeJS starting...

/home/pi/HAP-NodeJS/lib/Service.js:101

if (!this.linkedServices.includes(newLinkedService))

^

TypeError: this.linkedServices.includes is not a function

at Service.addLinkedService (/home/pi/HAP-NodeJS/lib/Service.js:101:30)

at Object.<anonymous> (/home/pi/HAP-NodeJS/accessories/AirConditioner_accessory.js:83:19)

at Module._compile (module.js:435:26)

at Object.Module._extensions..js (module.js:442:10)

at Module.load (module.js:356:32)

at Function.Module._load (module.js:311:12)

at Module.require (module.js:366:17)

at require (module.js:385:17)

at /home/pi/HAP-NodeJS/lib/AccessoryLoader.js:33:29

at Array.forEach (native)
what's the problem?

author
AndrewH431 (author)2017-03-18

Any chance someone can tell me what I did wrong?

pi@raspberrypi:~/HAP-NodeJS $ node Core.js

module.js:340

throw err;

^

Error: Cannot find module 'debug'

at Function.Module._resolveFilename (module.js:338:15)

at Function.Module._load (module.js:280:25)

at Module.require (module.js:364:17)

at require (module.js:380:17)

at Object.<anonymous> (/home/pi/HAP-NodeJS/lib/Accessory.js:3:13)

at Module._compile (module.js:456:26)

at Object.Module._extensions..js (module.js:474:10)

at Module.load (module.js:356:32)

at Function.Module._load (module.js:312:12)

at Module.require (module.js:364:17)

author
AndrewH431 (author)AndrewH4312017-03-18

I should mention that I tried:

sudo npm insgall debug

npm install mdns

npm rebuild

author
ShaotangW (author)2017-03-12

Will this work on pi 3, too ?

author
planetleak (author)ShaotangW2017-03-12

1 in 1 not work, need some modification. I have not pi 3, cannot test it.

author
planetleak (author)planetleak2017-03-12

Same with the newest pi 2 as i know.

author
ComputerGeek586 (author)2017-03-10

Ok I need Help

So I did everything like you said and every time I run node Core.js it says

Pls Help

Screen Shot 2017-03-10 at 2.50.37 PM.png
author

Maybe a syntactic error in the js file? check {}, characters.

author
WJBlom (author)2017-01-29

Good day Imre,

I have created a Homekit light switch with the Raspberry Pi
Zero. Everything work perfectly except I want to take it a step further:

Is the a line of code that I can run on the Raspberry Pi
itself to switch off the light AND notify the Core.js service that the light
has been turned off?

Currently I can run the light0.py file and turn off the
light but the Homekit service does not know about this change and my phone
(Home app) still shows that the light is on.

Ultimately what I want to do is to add a button (Switch) on
the GPIO pins to control the light from the switch and my phone. Can you please
help me?

Thank you in advance

author
MustafaD34 (author)2017-01-29

Help please ı realy need to do it

author
MustafaD34 (author)2017-01-24

This

IMG_0554.JPG
author
MustafaD34 (author)2017-01-24

Hello please someone help me ı reinstall rasperian 3 times but ı wıll take this error again to again whats the problem please someone help me

IMG_0553.JPG
author
WJBlom (author)2016-08-23

Hi Planetleak,

Thank you for the great tutorial! Everything works perfectly except the auto start on boot. I want to connect a light switch to the GPIO to control the light from the switch and from my phone. How will I be able to do this? Edit the light0.py files or run a separate python? If so what code do I need?

Please help me.

Kind Regards

Liam Blom

author
AndreaB259 (author)WJBlom2017-01-20

Hi, I want to do the same, have you solved?

author
planetleak (author)WJBlom2016-08-23

Hi, if you want to control light from gpio, use relay module, and control that relay with the GPIO pins. The code sample you find in this instructable STEP 4 or in the videos what i linked from my youtube channel. You can use that code. I buyed cheap RF controlled sockets for lamp, and i control the transmitter buttons with the rpi gpio pins, or if not secured the data transfer, you can use cheap RF433 transmitter direct with rpi, to control sockets. Unfortunately my rf sockets ar not work in that way, so im control the transmitter buttons with rpi gpio and optocouplers(opto-isolator). Best Regards: Imre

author
WJBlom (author)planetleak2016-09-01

Hi Imre,

I'm looking to use the existing wiring in my home. I want to use the existing light switches that are in my wall and connect them to my Pi Zero as a GPIO input? That way I can turn off the light at the wall switch as well as with Siri or the Home app. So to explain, I want to connect my existing Light switch to my Pi Zero as an input, connect a relay to my Pi Zero, as in your tutorial, as an output. That way I can see if the light is on or off on my phone. Where would I have to insert the code?

Kind Regrads

Liam

author
WJBlom made it! (author)WJBlom2016-09-01

Please see the schematic on what I mean. I don't describe things very well :)

Homekitt.png
author
planetleak made it! (author)WJBlom2016-09-02

Hi, and what if you change the wall lamp switch to wireless wall switch, and control that switch with raspberry via rf modul? Or you can control the remote controller pins directly with an opto isolator. Im using this way for my wireless socket control prototype.

image.jpg
author
BilalŞ19 (author)2016-07-13

hi, Thank you for your valuable tutorial. everything works fine except I get error messages when I add Test_accessory.js which is for to control Led. the code is same with you shared in text document except some names. when I want to run node Core.js I get some errors. any idea for these errors. thanks..

pi@raspberrypi:~/HAP-NodeJS $ node Core.js

*** WARNING *** The program 'node' uses the Apple Bonjour compatibility layer of Avahi.

*** WARNING *** Please fix your application to use the native API of Avahi!

*** WARNING *** For more information see <http://0pointer.de/avahi-compat?s=libdns_sd&e=node>

*** WARNING *** The program 'node' called 'DNSServiceRegister()' which is not supported (or only supported partially) in the Apple Bonjour compatibility layer of Avahi.

*** WARNING *** Please fix your application to use the native API of Avahi!

*** WARNING *** For more information see <http://0pointer.de/avahi-compat?s=libdns_sd&e=node&f=DNSServiceRegister>

HAP-NodeJS starting...

Parsing accessory: Fan_accessory.js

Parsing accessory: GarageDoorOpener_accessory.js

Parsing accessory: Light_accessory.js

Parsing accessory: Lock_accessory.js

Parsing accessory: TemperatureSensor_accessory.js

Parsing accessory: Test_accessory.js

Parsing accessory: Thermostat_accessory.js

events.js:160

throw er; // Unhandled 'error' event

^

Error: listen EADDRINUSE :::51826

at Object.exports._errnoException (util.js:1008:11)

at exports._exceptionWithHostPort (util.js:1031:20)

at Server._listen2 (net.js:1253:14)

at listen (net.js:1289:10)

at Server.listen (net.js:1385:5)

at EventedHTTPServer.listen (/home/pi/HAP-NodeJS/lib/util/eventedhttp.js:58:19)

at HAPServer.listen (/home/pi/HAP-NodeJS/lib/HAPServer.js:155:20)

at Accessory.publish (/home/pi/HAP-NodeJS/lib/Accessory.js:480:16)

at /home/pi/HAP-NodeJS/Core.js:32:13

at Array.forEach (native)

author
PonchoB1 (author)BilalŞ192016-11-28

I'm getting the same errors. Did you solve the problem?

author
SayonsomC (author)PonchoB12016-12-26

To solve the EADDRINUSE :::51826 error, we just need to reboot the RPi.

author
LI5ERG10 (author)2016-10-03

hi!

i try to install for myself... and get some errors in the step 2-9..

and when execute : node Core.js get they error and dont run...

(Thank for help)

module.js:339

throw err;

^

Error: Cannot find module 'debug'

at Function.Module._resolveFilename (module.js:337:15)

at Function.Module._load (module.js:287:25)

at Module.require (module.js:366:17)

at require (module.js:385:17)

at Object.<anonymous> (/home/pi/HAP-NodeJS/lib/Accessory.js:1:75)

at Module._compile (module.js:435:26)

at Object.Module._extensions..js (module.js:442:10)

at Module.load (module.js:356:32)

at Function.Module._load (module.js:311:12)

author
ThothLoki (author)LI5ERG102016-10-23

looks like you need is install debug

Sudo npm install debug

author
LI5ERG10 (author)2016-10-18

finally works!, the server start well, i add the accessory, but the led dont turn on...

in the test of the python files, led turn on and off..

author
planetleak (author)LI5ERG102016-10-20

Where is your python file?

author
LI5ERG10 (author)planetleak2016-10-21

the same as in the example, light1.py and light0.py

I put the file path and it works,

/python/light0.py

/python/light1.py

author
planetleak (author)LI5ERG102016-10-21

Move python files from python folder to the HAP-NodeJS/ folder.

author
stantang986 (author)2016-10-20

when I install srp, it shown npm err

npmERR!argv "/usr/bin/nodejs" "/usr/bin/npm" "install" "-g" "--unsafe-perm" "homebridge"

npmERR!node v6.9.1

npmERR!npm v3.10.8

npmERR!code 128

npmERR! Command failed: git clone --template=/root/.npm/_git-remotes/_templates --mirror git://github.com/KhaosT/node-srp.git /root/.npm/_git-remotes/git-github-com-KhaosT-node-srp-git-7ab735e3

npmERR! 克隆到裸版本库 '/root/.npm/_git-remotes/git-github-com-KhaosT-node-srp-git-7ab735e3'...

npmERR! fatal: Could not read from remote repository.

npmERR!

npmERR! Please make sure you have the correct access rights

npmERR! and the repository exists.

npmERR!

npmERR!

npmERR! If you need help, you may report this error at:

npmERR! <https://github.com/npm/npm/issues>

npmERR! Please include the following file with any support request:

npmERR! /home/pi/npm-debug.log

author
planetleak (author)stantang9862016-10-20

Try this:

npm install

author
Tony Hazel (author)2016-10-13

Errors and wrong version of curve25519?

Change
npm install curve25519
to
npm install ccurve25519-n
then
sudo npm install

On fresh Jessie install on pi Zero.

It took me one whole day to work that out

You're welcome.

author
DerwentM (author)2016-10-10

Hey, FYI if you clone the repo as root, it causes problems when you try and npm rebuild, so I would suggest jumping out of root to clone the repo

author
lordhichkas (author)2016-10-06

Hi,

Core.js works perfectly but I can't turn on led when I open lamp with apple home app.

I used 16th pin (greep led + 330ohms capacitance) for (+) (Power) and 39th pin for (-) (Ground)

author
LI5ERG10 (author)2016-10-03

i try to reinstall npm, and get this:

npmERR!install Couldn't read dependencies

npmERR! Linux 4.1.7-v7+

npmERR!argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"

npmERR!node v4.0.0

npmERR!npm v2.14.2

npmERR!path /home/pi/package.json

npmERR!code ENOPACKAGEJSON

npmERR!errno -2

npmERR!syscall open

npmERR!package.json ENOENT: no such file or directory, open '/home/pi/package.json'

npmERR!package.json This is most likely not a problem with npm itself.

npmERR!package.json npm can't find a package.json file in your current directory.

npmERR! Please include the following file with any support request:

npmERR! /home/pi/npm-debug.log

author
planetleak (author)LI5ERG102016-10-03

Hi, try this:

npm install

npm update

author
LI5ERG10 (author)planetleak2016-10-04

nothing!

get errors...

:-(

author
planetleak (author)LI5ERG102016-10-04

Delete non used accessories. Many peaople got problem with fan accessory.js

If not help, try to make a clean install. There is a step by step video tutorial for zero and pi2 on my youtube channel, or you can see the videos here too in this instructable.

author
LI5ERG10 (author)2016-10-03

i try the "ready to use" versión... and get the same error... :-(

author
FranciscaPerez_ (author)2016-03-03

Thank you for this awesome instructable! I made it, it works perfectly and I can add more accessories. However, node Core.js stops working and all accessories become inaccessible once I close the putty session. Do I need an autostart script? Do I need to launch it using different commands? I used the one that is included in the instructable: node Core.js

Thank you!

author

Hello, thanks.

Use "forever" to continue run.

Install:

sudo npm install forever -g

Run:

sudo forever start Core.js (from the HAP-NodeJS folder)

author

Thank you for your fast response! it works perfect. What do I need to execute the 'sudo forever start Core.js' in case that there is a power cut?

author

Try this (not tested):

nano /etc/init.d/corestart


cd home/pi/HAP-NodeJS
sudo forever start Core.js


chmod 755 /etc/init.d/corestart
update-rc.d corestart defaults

author
planetleak (author)planetleak2016-03-03

Sorry, forgot: only the "cd home..." and "sudo forever.." lines need in the corestart. The last two lines need to run after exited from nano.

author

Thank you, I achieved the autostart on reboot using PM2 at the end. Do you know how I can create an accessory to manage a blind? I created a light accessory to manage it but I would like to know if there is a better way.

author
WJBlom (author)FranciscaPerez_2016-09-02

Hi Francisca,

How did you get PM2 to start Core.js on boot? Please help I've tried everything!!

Kind Regards

Liam

author
FranciscaPerez_ made it! (author)planetleak2016-03-03

Thanks! I made it but it is not working because I force a power cut and the accessories become inaccessible from the app. I got two warnings in the last line (I do not know if it is the cause of the problem).

CapturaRP2.PNG
author

I got the unaccessible accessories with older HAP-NodeJS git too, try to update the git.

author
OlivierC29 (author)2016-08-16

Awsome tutorial.

I only had to made a small modification to the myLight.js file to change the path of the python files; from light1.py to python/light1.py (same for ligth0.py)

author
RajatV1 made it! (author)2016-07-30

awsome but need some code for powercut.

Movie on 30-07-16 at 6.46 PM.mov
author
RajatV1 made it! (author)2016-07-30

awsome but need some code for powercut.

Movie on 30-07-16 at 6.46 PM.mov
author
RajatV1 made it! (author)2016-07-30

awsome but need some code for powercut.

Movie on 30-07-16 at 6.46 PM.mov

About This Instructable

124,062views

134favorites

License:

More by planetleak:iPhone Touch ID windows loginESP8266 TV remote for HomekitRaspberry Pi 2 Homekit - from zero to Hey, Siri (Updated)
Add instructable to: