How to Connect Any Smart Device to HomeKit (with a Raspberry Pi)

Raspberry Pi with Homekit logo
Raspberry Pi Foundation, Apple

Many smart devices ignore Apple HomeKit support and integrate only with Alexa, Google and other platforms. However, with this Raspberry Pi hack, you can add HomeKit support to any smart device with the open source Homebridge software.

HomeKit for any smart device

If you’re all-in on HomeKit, a big problem is the lack of smart devices that support it. For example, the only thing holding back some cheap smart bulbs is their lack of HomeKit support. Amazon’s Choice for a “smart bulb” is a pack of four from TECKIN, which to date costs about $ 40, less than a single LIFX bulb.

Tuya Smart Lights

Of course, they are not as premium as LIFX; the colors aren’t as vibrant and they emit an audible buzz in the bathroom, but for $ 10 a pop, they’re pretty unbeatable value.

The main problem, however, is that they don’t support HomeKit. They’re not completely stupid – they work with Google Home, Alexa, IFTTT and the manufacturer’s app. They are suitable for someone who only has TECKIN smart bulbs.

However, since you can’t access them from HomeKit, you can’t control them from the Home app, the widget in Control Center, or Siri. You also cannot include them in scenes with bulbs from other brands or use them in Automations. If you’ve ever invested in HomeKit, it’s probably a dealbreaker.

Meet Homebridge

Fortunately, there is a hack that makes these special bulbs much more useful. The HomeKit API allows devices called bridges, such as that of Philips Hue, to connect child devices that operate on other protocols. You simply add the bridge as a device in HomeKit, and it records every light connected to it in HomeKit. Each time you request a light update, your phone speaks to the bridge and the bridge speaks to the light.

Thus, a bridge simply relays information from one API to another. Because you can control TECKIN bulbs on the Internet, it is quite possible to connect them to HomeKit with just software – no proprietary hardware is required.

If you have a Raspberry Pi around ($ 5 Pi Zero is fine), you can configure it as a bridge with a frame called Homebridge. This lightweight NodeJS app emulates the HomeKit API and forwards requests to your non-HomeKit smart devices.

Basically, you run it on the Pi and it adds each “stupid” device to the Home app. When you try to control the bulb via the Home or Siri app, Homebridge talks to the devices for you. Once you’ve set it up, it’s as if the device has supported HomeKit in the first place.

This requires the device to run Homebridge at all times, so it’s not something that you would install on your laptop. A Raspberry Pi is ideal, but if you have an old device, you can reuse it as a server or desktop that still works, you can install it there.

Homebridge is a framework, and you can extend it with plug-ins. It has fairly strong community support, so chances are a given smart device will likely have a Homebridge plug-in to support it. If your device does not have a plug-in, but your smart device has an API and you are tech savvy, you can create one yourself.

For most people, however, setup is as simple as installing Homebridge and the branded plug-in for the device, as well as a little setup. Whether you can use the command line and having a little time is quite easy.

Homebridge installation and configuration

Homebridge is a NodeJS application, so you need to install node and npm to use it. If your machine is running Linux, you can probably get it from your package manager.

On Ubuntu, you must type the following to manually configure the Node repository, then install nodejs:

curl -sL https://deb.nodesource.com/setup_13.x | sudo -E bash -
sudo apt-get install -y nodejs

Otherwise, you can consult Node download page to find out how to install it for your particular operating system.

If you are on Linux, you must also install some dependencies, as shown below:

sudo apt-get install libavahi-compat-libdnssd-dev

Then you can install Homebridge worldwide via npm, as shown below:

sudo npm install -g --unsafe-perm homebridge

You also want to install the branded plug-ins you need, because Homebridge is just a frame. For TECKIN bulbs, for example, the plug-in is homebridge-tuya-web, which is also established worldwide.

You must type the following:

npm i homebridge-tuya-web -g

Once everything is installed, you can actually use the thing! Type the following to run Homebridge once and initialize everything:

homebridge

He will complain about a lack of configuration that you will have to create. The default directory is ~/.homebridge/, but you can use the -U if you want to move it.

Type the following to create a new JSON configuration file in this folder:

nano ~/.homebridge/config.json

Whichever plug-ins you use, you need the following basic configuration:

{
  "bridge": {
    "name": "Homebridge",
    "username": "CC:22:3D:E3:CE:30",
    "port": 51826,
    "pin": "031-45-154"
  },

  "description": "Custom HomeBridge Server",

  "ports": {
    "start": 52100,
    "end": 52150,
  },

  "platforms": [

  ]
}

This configures Homebridge with a default port, name, PIN, and range of ports available to allocate to other devices.

Inside the void platforms bay, you place the configuration for each plug-in. You should be able to find instructions and examples of this on the GitHub page of each plug-in.

In the example below, the homebridge-tuya-web The TECKIN bulb plug-in wants to know my username and password to connect to the API of the bulb application, and a few other things:

  "platforms": [
     {
       "platform": "TuyaWebPlatform",
       "name": "TuyaWebPlatform",
       "options":
         {
           "username": "username",
           "password": "password",
           "countryCode": "1",
           "platform": "smart_life",
           "pollingInterval": 10
         }
     }
   ]

Once everything is set up, Homebridge should be ready to go. Run it again and your device should display a giant QR code which could force you to zoom out. Scan it with the Home app to add it and all devices connected to HomeKit.

QR code in the terminal

Homebridge loads your plug-ins and must record an on-screen message for each device it finds. You should see them all in HomeKit after adding them, and they should be fully functional.

I noticed a slight delay compared to my LIFX bulbs. This is probably because the bulbs are controlled via an API rather than directly. At first, the bulbs also didn’t display certain whites and warm whites correctly, but after a few adjustments, I was able to set up appropriate scenes.

You can always configure the devices in their own applications, wait for the Home application to update, then define the scene in HomeKit with the predefined configuration.

If you need to add Homebridge, you will want to delete the persist/ folder in the configuration directory, then delete the HomeKit bridge from the parameters of any bulb connected under the “Bridge” tab.

Adding Homebridge as a Service

If you want Homebridge to work all the time, you will probably want to configure it to restart if it hangs or if your Raspberry Pi restarts. You can do this via a Unix service. Configure it after verifying that Homebridge works as expected.

First, add a new service user, called homebridge:

sudo useradd -M --system homebridge

Set a password:

sudo passwd homebridge

Then you will have to move the homebridgeconfiguration outside of your personal directory. /var/lib/homebridge/ should be fine:

sudo mv ~/.homebridge /var/lib/homebridge/

Make sure that the person using homebridge has ownership of this directory and all subfolders:

sudo chown -R homebridge /var/lib/homebridge/

Once done, you can create the service. To do this, create a new file called homebridge.service in /etc/systemd/system/:

sudo nano /etc/systemd/system/homebridge.service

And then paste the following configuration:

[Unit]
Description=Homebridge service
After=syslog.target network-online.target

[Service]
Type=simple
User=homebridge
ExecStart=/usr/bin/homebridge -U /var/lib/homebridge
Restart=on-failure
RestartSec=10
KillMode=process


[Install]
WantedBy=multi-user.target

Reload the service daemon to update it with your changes:

sudo systemctl daemon-reload

Now you should be able to activate your service (by configuring it to run on startup):

sudo systemctl enable homebridge

And launch it:

sudo systemctl start homebridge

If you need to debug errors resulting from configuring the service, you can view the service logs by typing:

journalctl -fn 50 -u homebridge

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.