Turn a Raspberry Pi Into a Homekit Accessory
HomeKit offers a way to control various home monitoring and automation accessories from your iOS (and recently Mac) devices. With a Rasberry Pi, the GPIO pins can be used to interface with various components and control devices with no built-in HomeKit support.
Here are some examples of using a Raspberry Pi as a controller:
- Gesture Based TV Remote Control
- reef-pi: Automate your Aquarium
- Pi Fridge Controller
- Controlling Christmas Lights via HTTP
There are ways to use Node.js (homebridge) or Ruby (ruby_home) to turn those devices into HomeKit accessories. Unsupported, unofficial devices. What you should be aware is that there are some limitations as to what kind of HomeKit accessories you can create, and what kind of interactions you can do.
In my case, I have a Raspberry Pi that will send a signal to a set of holiday lights. This is a one-way signal and the Pi has no way of knowing if the signal was successful or the current state of the lights. You have to either guess or look out a window. This doesn’t lend itself too well to HomeKit.
HomeKit has “Accessories”, “Services”, and “Characteristics”. Accessories are devices that can provide one or more Services to HomeKit. For example, a HomeKit enabled Lightbulb would provide a “Lightbulb” service.
There is a predefined list of allowed Service Types, and these are part of the limitations of what you can do with HomeKit. The limitations also mean these specific types are very well supported and have a decent UI and Siri integration.
Each Service must also provide mandatory and optional Characteristics. A Characteristic is a capability of a device, and each Service Type has a set of characteristics that it is required/allowed to support. A “Lightbulb” must support the “On/Off” characteristic, and may support “Hue”, “Saturation”, or “Brightness”.
As you are limited by these characteristics, there is no way to have an “on” button and “off” button for a HomeKit accessory, it has to be a slider toggle switch. The HomeKit device is supposed to be able to report it’s current state and because my Pi is unable to retrieve the current state, it doesn’t completely work with HomeKit.
So that means you can’t have a Home app interface like this:
Or this:
Which is somewhat disappointing. It is somewhat offset by being able to control the states using Siri (or Shortcuts). Siri is able to ignore the current state and send “on” or “off” commands, which is convenient.
This may change in the future, as Apple seems to be iterating on HomeKit as more compatible devices are released. It may be awhile though and in the mean time having custom apps for devices is the best way to have more customization.
Shortcuts
iOS 12 includes Shortcuts, an app for automating tasks on iPhones and iPads. It can also do what HomeKit cannot, by allowing us to add custom functions.
For my holiday lights, I have a small web server running on the Raspberry Pi that can also control the lights from a web browser. This provides an HTTP API that we can access with Shortcuts.
POST /lights HTTP/1.1
Host: rpi.local
Accept: application/json
Content-Type: application/x-www-form-urlencoded
action=<action>
Where <action>
is one of:
ON SYNC OFF PROGRAM#1 PROGRAM#2 PROGRAM#3 PROGRAM#4 PROGRAM#5 PROGRAM#6 PROGRAM#7 PROGRAM#8
To control this in Shortcuts, add an action for “URL” with the URL to the API endpoint. In my case, it is http://rpi.local/lights
.
Add a second action for “Get Contents of URL”, open the “Advanced” section and set the method to “POST”. Add a Header for key Accept
and value application/json
. Set the Request Body to “Form”, and add a new field (type: text) with key action
and value ON
.
Add a name to the action such as “Turn On Holiday Lights” and save it. You can now activate custom actions from the Shortcuts app, from status screen widgets, or via Siri. This covers some functionality you cannot get in HomeKit, but is limited to the devices with the Shortcut installed.