Apple Watch Adventures

HomeKit Remote control your home

One thing that can be useful to do with the Watch is to control various devices at home, like check the temperature, turn on or off the heating, check a surveillance camera, open the door etc. The request for such integration between mobile devices and what today we would call Internet of Things (IoT) was so high that in 2014 Apple released the HomeKit, a protocol to interact with various object.

HomeKit

HomeKit Logo

As usual in this site we talk about things that work directly on the Apple Watch and we discuss how to do things in the most open possible way. HomeKit is today an open protocol (even if the license is a kilometre long Apple stuff, but for personal purpose it is ok) and we are going to explore three possible variants of its usage.

Commercial HomeKit Accessory

The market is full of accessory that implement the HomeKit protocol natively. A list (I think not exhaustive) is available here. This is for sure a good option and once the device is setup and installed it can be easily controlled with the Apple Watch.

There are two issues that I see in this approach: the first problem is that you do not know exactly what the device is doing. For example, if you install a camera that sends the images on some remore server to make it available “on the cloud” you do know know who the security of the entire data is managed. The second issue is that you are limited to the devices already available on the market and if you want to customize you experience you need to do it differently as I am going to explain.

Homebridge

HomeBridge Logo

HomeKit works in two different ways. You can have your device announcing directly on the network and available as accessory (like if you buy a camera, a light bulb etc.) or you can have a “bridge” that manages multiple devices. In general the bridges are commercial products used to control several devices of the same hardware manufacturer, but because the protocol is open, it exists an open version of the Homebridge that can be found at homebridge.io.

The Homebridge can run on almost any computer (Linux including Raspberry, Apple, Windows, etc.). Once it is installed you will have a dashboard on a website of the device with the possibility to add “plugins”. Every plugin is a device that you can add and broadcast on your HomeKit network. For example you can add the plugin for the Raspberry camera and start to see it on the HomeKit. But you can have also devices that are presented to the HomeKit as a real device but they do some specific function. An example is the plugin to download data from a website (for example a meteo website) and present it as a thermometer in HomeKit. To create and customize plugins is quite easy and this allows the user to add its own service effortless.

I think for most of the services the Homebridge solution is the most useful and customizable.

ESPHap

The Homebridge on Raspberry is for sure a good solution to customize the HimeKit experience, but sometime it can be better something different. A Raspberry is a full computer that needs updates, it can crash, the Homebridge application can hang for several bugs etc. Moreover the boot time is instantaneous because it has to boot up an entire linux and then it has to start up the Homebridge. For most application these things are more than acceptable but I was looking for some device that was reliable, cheap and possible with only the HomeKit and hardware management on top without any other piece of code to maintain.

Well the first thing that crossed my mind was: Adruino! It is a fantastic board and it does everything, but unfortunately it seems a bit too low in performances to run the multitude of things required by HomeKit (especially the encryption). But looking around I discovered that it exists a board, the ESP32, that does exactly what I want, it is a dual core Xtensa CPU with integrated Wifi and/or Bluetooth, moreover this board can be found mounted on an Arduino board, so it can be programmed with the same Arduino language and IDE. The price is also very affordable, around 10$, so it is perfect!

Not only this board can run easily the HomeKit protocol, but a nice guy ported it with all the extensions for Arduino, so it is possible to program it out of the box including the HomeKit library in the IDE. The repository for the Library is on github and the instructions are quite easy for someone that knows how to use Arduino. I recommend the ESP32 board, not the ESP8266. The reason is that the 8266 seems much slower. My board, a ESP32, is incredibly fast, I turn it on and I can see the accessory on HomeKit few seconds later.

The README is very good, and it works immediately (only in Big Sur there is a compilation issue but it can be addressed easily looking at a comment in the Issue section). There are several examples in the library to implement a led, a switch, a reading temperature sensor, etc. Overall I think this is the best method to implement customized devices for HomeKit because the entire device is contained in the ESP32 and once it works you do not need to take care of it anymore.

HomeHub

The last thing that I have to mention here about HomeKit is how to connect to your device when you are not on the same network. The Apple HomeKit protocol uses the mDNS (Bonjour) protocol for discovery of the devices, so if you are not on the same network you cannot see the device, even if you are using a VPN (and also this site is about Apple Watch and I haven’t find a VPN for the Watch so far). So when you are out of your home you are not able to see your HomeKit devices unless you use a HomeHub. What is the HomeHub? Well it is just a commercial name to say that the a device in your home listen to the mDNS and bounce the packets to some Apple server where your Watch (or telephone) can connect and see it. So it is a proxy. Unfortunately Apple does not give any specification of how this HomeHub works (probably it authenticates on iCloud with your credentials and use it) and a reverse engineering of the protocol would violate the terms of service of iCloud and I think no developer wants to be kicked out by the Apple ecosystem to reverse it.

So, long story short, I wasn’t able to find any open implementation of the HomeHub. If you want to use it (so if you want to access your HomeKit devices when you are away from home) the only option is to use one of these device: iPad, Apple TV or HomePod. These three devices can connect to iCloud and have the option to turn on the HomeHub on your network, that is to make the proxy between your home network and Apple. As usual, when you open the network to third party services consider that someone else can control your stuff. I don’t think that apple wants to turn off your heating, but Apple can be hacked (even if I think that the entire protocol is encrypted and no data is saved on their servers, but I cannot be sure). So consider which data are you going to expose. A nice feature would be to be able to select what to send to Apple, for example I don’t mind to see the water level of the plants, while I am concerned by the surveillance cameras potentially exposed to the public. The only way to do this manually is to do a VLan for the devices at home connected to the HomeHub and keep the other devices on another VLan, not really user friendly.