Apple HomeKit hacking
DIY inside the walled garden
A special thanks to fictional Astronaut Mark Watney’s DIY spirit for inspiring this article. If you haven’t already please go read The Martian, or if you’re lazy watch the movie.
I had a simple goal: turn the lights on and off in my house using my voice and finally live in the future Star Trek had promised me so many years ago.
2015 saw a massive boom in home automation with many products flooding the shelves and confusing consumers. My goal could be accomplished with the use of consumer products. There’s just one problem: I’m cheap. I’m not George Costanza cheap, but enough to think there has to be a better way to get what I want and not shell out the big bucks.
The average price in Canada for a smart outlet, smart bulb, or smart switch runs around $60. This price point doesn’t scale well. Assuming I replace ten lamps it will cost me around $680 after taxes. Does that sound ludicrous to anyone else? I have to find a solution that cuts the price down by at least half.
Problem number two. My household uses Apple hardware. Laptops, tablets, phones, etc. Any solution I find has to work with Apple products.
Let’s recap my prime directives:
- Be cheaper than current off the shelf solutions
- Work well with Apple products
- Work with my voice (STAR TREK!!)
Let’s talk about HomeKit. Apple’s framework for home automation allows you to set up of virtual Homes and Rooms that can contain different accessories. These map 1:1 with your home. You then set up Actions and Triggers to control those accessories. If you’re new to HomeKit there’s an excellent primer here.
I chose HomeKit over the sea of other “standards” because it satisfies my last two directives. HomeKit is built for iOS and works with iPhone, iPad and soon with the new AppleTV. It also has full Siri integration (Yay!). So what’s are the downsides of such a magnificent technology?
- Slow adoption from hardware vendors
- Many existing products don’t support it
- The products that do are still very pricey
How then do we satisfy the first prime directive?
Now the hard part: what can I use to control the lamps? I recently came across a Geek Dad article singing the praises of the Etekcity Wireless Remote Control Electrical Outlets. These are very inexpensive outlet adapters that can turn on and off anything up to 120V connected to an electrical outlet. They work by transmission of a signal from an included remote control over the 433Mhz RF band. At ~$45 for 5 outlets they are cheaper than buying a single smart outlet on the market today, plus remote controls can program in a variety of different ways. I affixed a remote to the wall so I can turn the lights on and off if I don’t have my phone handy, a feature you don’t see with any smart outlets on the market today.
My phone can now talk to HomeKit using HomeBridge and I have a device that can turn on and off the lights. How do I get HomeBridge to talk to these devices which can only receive signals over 433Mhz radio frequency?
The answer involves the use of a Raspberry Pi. It turns out I am not the first person to encounter this problem. Tim Leland put together a guide for building a circuit using the Raspberry Pi to send the appropriate signals using a 433Mhz transmitter connected to the Pi’s general purpose input/output pins. He put up a code repository with two command line utilities RFSniffer and codesend. The former detects button presses (interpreted as RF codes) by listening to RF signals.. The latter transmits an RF signal by passing an RF code as a parameter to the command. Each Switch has two codes, one for on the other for off.
I wrote a small Node.js HTTP server that executes the codesend utility as a remote procedure call. This allows me to open a URL from any device on the internal network and have the Pi send a code over RF. The code is sent as a parameter in the URL. If the code were for example 123456 the URL to execute would be the following:
The Raspberry Pi can now receive HTTP requests and turn a switch on and off. The last piece of the puzzle is having HomeBridge execute those HTTP requests when a HomeKit command is given. To accomplish this the homebridge-http Node.js plugin is required. The plugin creates HomeKit accessories and attaches HTTP URLs to the accessories on and off states. Here is an example JSON configuration for a single switch.
"name": "bunny lamp",
"username" : "",
"password" : ""
Each switch requires an accessory object added to the accessories array. HomeBridge will register “bunny lamp” as an accessory which is also the name that Siri will respond do. Everything should now be in place and the flow of our Siri requests should resemble the following diagram.
The last piece is to set up HomeKit itself. This setup process can only be done through third party apps on an iOS device at the moment. Two highly recommended apps are Insteon+ which is free and Home which costs around $15. I recommend Insteon+ to get started because it has a far superior user experience IMHO and can perform almost every operation Home can without the costly price tag. The reason to get Home is it supports the use of Triggers which no other app can currently do. It also has Apple Watch integration. Read more about Home’s features here.
That’s it. The project is now complete. Let’s see how the pricing worked out.
- Etekcity Wireless Remote Control Outlet Switch x 2 = $90.37
- Raspberry Pi 2 Model B Ultimate Starter Kit = $124.29
- 433Mhz RF Transmitter Module+Receiver Kit = $18.85
This project ended up costing me around $233.51. The consumer off the shelf HomeKit compatible outlets would have cost around $680. This solution cost a little more than 1/3 the price. In addition to costing significantly less I was also able to integrate my non-HomeKit compatible Nest thermostat and Logitech Harmony Hub using the community plugins for HomeBridge.
I have been living with this solution at home for about two weeks and aside from one minor issue the project has been working flawlessly.
There are a few next steps I would like to do:
- Clean up the code repository for the Node RPC server I created
- Write a followup article that outlines in detail the setup procedure step by step.
- Begin looking for similar hardware that will work for hard wired light switches. Many of the rooms in my house have fixtures instead of lamps.
With a little planning and a day’s worth of tinkering a DIY solution can live in the Apple ecosystem and allow the flexibility we are used to in open-source software. Many thanks to those in the open source community who put in all the legwork to create such invaluable projects without which I wouldn’t have been able to live in the Star Trek future I always dreamed of.