How to connect any smart device to HomeKit (with a Raspberry Pi)

Cupertino, March 19, 2020
Raspberry Pi Foundation, Apple

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

HomeKit for any smart device

If you are integrated into HomeKit, a big problem is how few smart devices support it. For example, the only thing holding back inexpensive smart bulbs is the lack of HomeKit support. Amazon's choice of a "smart bulb" is a four-pack from TECKIN, which at this writing costs around $ 40 - less than a single LIFX bulb.

Smart lights Tuya

Of course, they are not as premium as LIFX; The colors are not as vibrant and emit a sound in the bathroom, but for $ 10 per pop, they are pretty unbeatable.

But the main problem is that they do not have support for HomeKit. They are not entirely dumb, they work with Google Home, Alexa, IFTTT and the manufacturer's application. I'm fine for someone who only has TECKIN smart bulbs.

However, because you can't access them from HomeKit, you can't control them from the Home app, the Control Center widget, or Siri. You also can't include them in light bulb scenes from other brands or use them in Automation. If you've already invested in HomeKit, it's most likely a dealbreaker.

Meet with Homebridge

Fortunately, there is a hack that makes these bulbs more useful. The HomeKit API allows devices called bridges, like this one from Philips Hue, to connect child devices that operate on other protocols. Simply add the deck as a device in HomeKit and record every light connected to it in HomeKit. Whenever you make a request to update a light, your phone speaks to the bridge, and the bridge speaks to the light.

So a bridge only transmits information from one API to another. Because you can control TECKIN bulbs on the internet, it is completely possible to connect them to HomeKit only with the software - no proprietary hardware is required.

If you have a surrounding Raspberry Pi (a $ 5 Zero Pi is fine), you can set it up as a bridge with a frame called Homebridge. This easy-to-use application, NodeJS, emulates the HomeKit API and sends requests to non-HomeKit smart devices.

Basically, you run Pi and add each "mute" device to your Home application. When you try to control the light bulb through the Home or Siri app, Homebridge talks to the devices for you. Once you've set it up, it's like having your HomeKit support first.

This requires the device to run Homebridge permanently, so it's not something you install on your laptop. A Raspberry Pi is ideal, but if you have an old device, you can always rebuild it as a server or desktop that is always running, you can install it there.

Homebridge is a framework and you can extend it with plugins. It has fairly large community support, so chances are that any given smart device will probably have a Homebridge plug-in to add support for it. If your device doesn't have a plug-in, but your smart device has an API and you are tech-savvy, you can write one yourself.

However, for most people, the setup is just installing Homebridge and the branded plug-in for the device, along with a bit of configuration. If you can use the command line and have a little time, it is quite easy.

Installing and configuring Homebridge

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

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

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

Otherwise, you can refer to the Node download page for information on how to install it for your particular operating system.

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

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

After that, you can install Homebridge globally through npm, as shown below:

sudo npm install -g --unsafe-perm homebridge

You also want to install the branded plug-ins you need, as Homebridge is just a framework. For TECKIN bulbs, for example, the plug-in is homebridge-tuya-web, which is also installed globally.

You would enter the following:

npm i homebridge-tuya-web -g

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

homebridge

It will complain about the lack of configuration you need to create. The default director is ~/.homebridge/, but you can use -U parameter if you want to move it.

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

nano ~/.homebridge/config.json

No matter which plugins 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": [

  ]
}

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

Inside the hole platforms matrix, place the configuration for each plug-in. You should find instructions and examples to this effect on the GitHub page of each plug-in.

In the example below, homebridge-tuya-web The TECKIN bulb plug-in wants to know my username and password to connect to the bulb application API 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 terminal should display a huge QR code that may cause you to shrink. Scan this with the Home app to add it and all devices connected to HomeKit.

QR code in the terminal

Homebridge uploads your plugins and should record a message on the screen for each device it finds. You should see them all in HomeKit after they are added and they should be fully functional.

I noticed a slight delay compared to the LIFX bulbs. This is probably due to the fact that the bulbs are controlled on an API rather than directly. At first, the bulbs did not display correctly white and warm white, but after a bit of adjustment, I was able to set appropriate scenes.

You can configure the devices at any time in your own applications, wait for the Home application update, and then set the scene in HomeKit with the premade configuration.

If you want to add Homebridge again, you will want to delete it persist/ the folder in the configuration directory, then remove the bridge from HomeKit from the settings of any bulb connected under the "Bridge" tab.

Adding Homebridge as a service

If you want Homebridge to work at all times, you may want to configure it to restart if it crashes or if Raspberry Pi restarts. You can do this through a Unix service. Configure this after you have verified that Homebridge is operating as required.

First, add a new called service homebridge:

sudo useradd -M --system homebridge

Set a password:

sudo passwd homebridge

Next, you'll have to move homebridgeconfiguration outside the home personal directory. /var/lib/homebridge/ it should be fine:

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

Make sure the person who uses it homebridge is owned by that director and all subfolders:

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

Once you have done this, you can create the service. To do this, create a new file named homebridge.service in the /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 the changes:

sudo systemctl daemon-reload

Now, you should be able to enable the service (setting it to work on startup):

sudo systemctl enable homebridge

And start it:

sudo systemctl start homebridge

If you need to troubleshoot service configuration errors, you can view service logs by typing:

journalctl -fn 50 -u homebridge

Best selling & Top trending HomeKit product in our shop at this moment

HomeKit.Blog is in no way affiliated with or endorsed by Apple Inc. or Apple related subsidiaries.

All images, videos and logos are the copyright of the respective rights holders, and this website does not claim ownership or copyright of the aforementioned.

All information about products mentioned on this site has been collected in good faith. However, the information relating to them, may not be 100% accurate, as we only rely on the information we are able to gather from the companies themselves or the resellers who stock these products, and therefore cannot be held responsible for any inaccuracies arising from the aforementioned sources, or any subsequent changes that are made that we have not been made aware of.

HomeKit.Blog Is A Participant In The Amazon Services LLC Associates Program, An Affiliate Advertising Program Designed To Provide A Means For Sites To Earn Advertising Fees By Advertising And Linking To Amazon Store (Amazon.com, Or Endless.com, MYHABIT.com, SmallParts.com, Or AmazonWireless.com).

The opinions expressed on this website by our contributors do not necessarily represent the views of the website owners. 

Copyright © 2022 . All rights reserved
United States