Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@petergerdes
Last active July 22, 2019 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petergerdes/422239985cdcc594ab2d74a4d289c66f to your computer and use it in GitHub Desktop.
Save petergerdes/422239985cdcc594ab2d74a4d289c66f to your computer and use it in GitHub Desktop.
My adventures with HomeKit and Homebridge

Adventures with HomeKit and Homebridge


Because Dax said I need to write this stuff down, so here I go.

Hardware in my setup

  • AppleTV 4K & HD
  • Raspberry Pi 2b
  • Philips Hue lights + Squared v2 bridge
  • Philips Hue Sensors
  • Amazon Echo Dot (3rd gen)
  • Sonos Connect & Sonos One
  • Onkyo TX-NR676E receiver
  • Philips 55PUS7502/12
  • Xiaomi Xiaofang 1S Camera
  • Plugwise Anna Thermostat
  • Koogeek KH02CN Switch
  • OSRAM Smart+ Switch

Getting Homebridge to run on a Raspberry Pi

First thing is to get the latest Raspbian installed on a MicroSD card (8GB should be more than enough, make sure to get a Class 10 speed card). Download the Raspbian Stretch Lite from Raspberry Pi website and write it on the SD card, I use ApplePi Baker (a nice GUI for OS X), but dd in a terminal should do the trick just as nice. Make sure to enable SSH access. Login via SSH to the Pi and update everything with the following commands:

sudo apt-get update
sudo apt-get upgrade

Install git & other tools

sudo apt-get install git pkg-config autoconf automake libtool

Installing NodeJS

Getting the last version of NodeJS (at the time of writing 10.x is LTS) is pretty easy by adding NodeSource repository to our apt repository list.

$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -

With this command we add it to our repository list. If we're going to install NodeJS it makes sure we get the right one for Raspberry Pi (ARM). Installing Node can be done with this command:

$ sudo apt install -y nodejs

Check if installation worked:

$ node -v

It should return v10.x.x.

Installing Homebridge

Follow the installation guide on Homebridge Github. We can skip step 1 as we just have done that. To get Homebridge to start on boot of device follow this. In the guide it will say

cmd="DEBUG=* /usr/local/bin/homebridge"

But it is actually installed at /usr/bin/homebridge, so change that in /etc/init.d/homebridge

sudo nano /etc/init.d/homebridge

Change line 13 to

cmd="DEBUG=* /usr/bin/homebridge"

If you run plugins that need 'insecure mode' like homebridge-alexa and homebridge-config-ui-x then change it to:

cmd="DEBUG=* /usr/bin/homebridge -I"

Now Homebridge should start on boot

sudo reboot

Installing x264 codec

If you need to going to stream a camera feed to Homekit, it needs to be in h264 format, for that we need to install the x264 codec for ffmpeg to use.

Download the x264 repo

git clone --depth 1 http://git.videolan.org/git/x264

Go into the created directory

cd x264

Configure x264

./configure --host=arm-unknown-linux-gnueabi --enable-static --disable-opencl

Compile x264

make -j4

adding -j4 will make make use 4 cores for compiling

Install x264

sudo make install

Installing fdk-aac

Download the fdk-aac repo

git clone https://github.com/mstorsjo/fdk-aac.git

Go into the created directory

cd fdk-aac

Configure fdk-aac

./autogen.sh

./configure --prefix=/usr/local --arch=armel --target-os=linux --enable-omx-rpi --enable-nonfree --enable-gpl --enable-libfdk-aac --enable-mmal --enable-libx264 --enable-decoder=h264 --enable-network --enable-protocol=tcp --enable-demuxer=rtsp

Compile fdk-aac

make -j4

Install fdk-aac

sudo make install

sudo ldconfig

Installing ffmpeg

Make sure to install ffmpeg after installing x264 and fdk-aac

Download the ffmpeg repo

git clone git://source.ffmpeg.org/ffmpeg --depth=1

Go into the created directory

cd ffmpeg

Configure ffmpeg

./configure --prefix=/usr/local --arch=armel --target-os=linux --enable-omx-rpi --enable-nonfree --enable-gpl --enable-libfdk-aac --enable-mmal --enable-libx264 --enable-decoder=h264 --enable-network --enable-protocol=tcp --enable-demuxer=rtsp

Compile ffmpeg

make -j4

Sit back, relax and get some ☕️ because this will take quite some time.

Install ffmpeg

sudo make install

To use the above ffmpeg installation with a Xiaomi Dafang camera make sure to use these settings

Video settings:

  • video format: FixedQp
  • Size: 1280x720
  • FrameRate: 21
  • BitRate 299

Audio settings

  • audio format: mp3
  • filter: filter 2
  • in rate 16000
  • out rate 16000

Making sure that logs don't get too big

In the case that mongodb is installed and used for MQTT messages (for the Xiaomi camera), the log can get big, very very big in just a couple of days. Which means it can clog up your drive space very quickly and crash your Raspberry Pi. In order to keep the mongodb logs in small sizes use logrotation.

Edit logrotation file at:

sudo nano /etc/logrotate.d/mongodb-server

Paste the following:

/var/log/mongodb/*.log {
    daily
    rotate 30
    compress
    missingok
    sharedscripts
    postrotate
    	kill -SIGUSR1 $(cat /var/lib/mongo/mongod.lock)
    endscript
}

Making sure that Homebridge keeps running

Homebridge crashes every once in a while without letting in the logs know why. So keeping Homebridge running with a very simple cronjob by starting it every hour (it won't start if it's still running).

Create a bash-script that starts homebridge

nano starthomebridge.sh

Enter the following:

#!/bin/sh
/etc/init.d/homebridge start

Make it executable:

chmod +x starthomebridge.sh

Add the script to cron:

sudo crontab -e

And enter the following:

0 * * * * /home/pi/starthomebridge.sh

Used HomeBridge plugins

You can install these using the command:

sudo npm i -g homebridge-{nameofplugin}

If it runs into trouble with errors about not having access/rights to certain folders, usually adding --unsafe-perm helps

sudo npm i -g homebridge-{nameofplugin} --unsafe-perm

Automations to solve things

Turn receiver on/off based on Sonos (Connect)

As most Onkyo receivers don't support Works with Sonos, an automation can help here.

  • When Sonos turns on -> Turn receiver on (in homebridge config set the default position to CD (optical in))
  • When Sonos turns off -> Turn on delay-switch[1]
  • When delay-switch-sensor is triggered -> Turn receiver off (if Sonos is still off)

[1] When Sonos goes to the next song it will briefly report 'off' thus triggering turn receiver off automation, so the delay switch waits for 10 seconds and triggers the sensor, with the Eve app (see link below) you can set up an 'if', so if the Sonos is still off, turn the receiver off.

Involved plugins:

  • homebridge-onkyo
  • homebridge-zp
  • homebridge-delay-switch

Grouping Sonos' together

Sonos is a great alarm clock with TuneIn as radio service. Every (work)day the Sonos in the bedroom starts playing around a certain time. The Sonos in the living room is not playing as I am upstairs still, around the time I am in the living room I want the Sonos to be playing the same radio station. That is easily possible by adding an alarm to the living room Sonos around the time I'm downstairs. Problem is, that it starts a separate stream that is out of sync with the bedroom Sonos, as I can hear both Sonos' at the same time (upstairs and downstairs) this get irritating. Solution: group the Sonos' at a certain time.

  • When certain time hits -> Turn on 'Sonos Coordinator' on bedroom Sonos
  • When certain time hits -> Turn on 'Sonos Speaker' on bedroom Sonos
  • When certain time hits -> Turn on 'Sonos Speaker' on living room Sonos

Make sure to have 'speakers: true' in the config.json for homebridge-zp config.

Involved plugins:

  • homebridge-zp

Alexa, Goodnight

On the Sonos in the bedroom Alexa is activated, but by itself Alexa can't do anything with Homekit accessories, like setting the thermostat to 16 degrees (celsius). The homebridge-alexa plugin exposes Homebridge devices to Alexa, just devices, no scenes. So to start the 'Goodnight' scene from Alexa some little help is needed. In the Alexa app a 'routine' is create and activated by saying "Alexa, goodnight", that routine turns on a fake light bulb in Homebridge, waits a minute and turn the fake light bulb off.

Back in homekit:

  • When fake-goodnight-light is turned on -> Start 'Goodnight' scene

The 'Goodnight' scene turns all lights off and sets thermostat to 16 degrees celsius

Involved plugins:

  • homebridge-alexa
  • homebridge-fakebulb

Useful links/apps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment