Skip to content

Instantly share code, notes, and snippets.

@smockle
Last active May 1, 2019 20:10
Show Gist options
  • Save smockle/295d7f1f837a45390e6125bea7c56b3a to your computer and use it in GitHub Desktop.
Save smockle/295d7f1f837a45390e6125bea7c56b3a to your computer and use it in GitHub Desktop.

Homebridge

On the host

# Download latest Raspbian Stretch Lite from https://www.raspberrypi.org/downloads/raspbian/

# Insert a Micro SD card (at least 8 GB) into the host

# Install Etcher, a utility that flashes SD cards
$ brew cask install balenaetcher
# Use Etcher to flash the downloaded Raspbian image to the inserted Micro SD card.

# Remove and reinsert the Micro SD card to mount the `boot` volume

# Enable SSH
$ touch /Volumes/boot/ssh

# Enable Wi-Fi
$ tee /Volumes/boot/wpa_supplicant.conf << EOF
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US

network={
	ssid="YOUR_WIFI_SSID"
	psk="YOUR_WIFI_PASSWORD"
	key_mgmt=WPA-PSK
}
EOF

# Unmount and remove the Micro SD card from the host
# Insert the SD card in the Raspberry Pi, then connect it to power.

# After a few moments, the Raspberry Pi should connect to your Wi-Fi network and be assigned an IP address.
# You can find the assigned address (and reserve a static IP address) using the AirPort utility.
# Alternately, you can find the assigned IP address using the Pi’s MAC address
$ arp -na | grep -i b8:27:eb

# Create a public + private RSA key pair (e.g. ~/.ssh/id_rsa & ~/.ssh/id_rsa.pub)
$ ssh-keygen -t rsa

# Store SSH connection information for your Raspberry Pi
$ tee -a ~/.ssh/config << EOF
Host raspberrypi
  HostName YOUR_PI_IP_ADDRESS
  IdentityFile /Users/YOUR_USERNAME/.ssh/id_rsa
  User pi
EOF

# Connect to the Raspberry Pi via SSH
$ ssh pi@RASPBERRY_PI_IP_ADDRESS
# When prompted for a password, use `raspberry`

On the Raspberry Pi

# Change the password for the default user
$ passwd

# Append the contents of the host’s public key to ~/.ssh/authorized_keys
# Then, prevent further writes:
$ chmod 444 ~/.ssh/authorized_keys
# Now, you’ll be able to connect to your Raspberry Pi using just `ssh raspberrypi`

# Update Raspbian
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get dist-upgrade

# EITHER Install the pre-built Node.js LTS (currently 10.x) ARMv6 binaries
$ mkdir ~/Downloads && cd $_
$ wget https://nodejs.org/dist/v10.13.0/node-v10.13.0-linux-armv6l.tar.gz
$ tar -xvf node-v10.13.0-linux-armv6l.tar.gz
$ cd node-v10.13.0-linux-armv6l
$ rm CHANGELOG.md LICENSE README.md
$ sudo cp -Rf * /usr/local/

# OR Add the NodeSource repository and install using apt-get (ARMv7)
$ curl -sL https://deb.nodesource.com/setup_10.x | sudo bash -
$ sudo apt-get install -y nodejs

# Install depedencies
$ sudo apt-get install git libavahi-compat-libdnssd-dev

# Change install location for globally-installed NPM modules
$ mkdir ~/.npm-global
$ npm config set prefix '~/.npm-global'
$ sudo tee /etc/profile.d/npm-global.sh << EOF
if [ -d "/home/pi/.npm-global" ] ; then
    PATH="/home/pi/.npm-global/bin:$PATH"
fi
EOF
$ sudo chmod +x /etc/profile.d/npm-global.sh
$ source /etc/profile

# Update NPM
$ npm i -g npm@latest

# Install Homebridge
$ npm i -g homebridge

# Make Homebridge start when your Raspberry Pi boots (using systemd)
# You can repeat these steps to set up multiple Homebridge accessories in HomeKit.
# This is useful because one slow Homebridge accessory will cause Homebridge to report `Updating` for *all* Homebridge accessories.

# Create systemd service config files
$ sudo tee /etc/default/homebridge-YOUR_INSTANCE_NAME << EOF
# Defaults / Configuration options for homebridge
# The following settings tells homebridge where to find the config.json file and where to persist the data (i.e. pairing and others)
HOMEBRIDGE_OPTS=-U /var/lib/homebridge-YOUR_INSTANCE_NAME

# If you uncomment the following line, homebridge will log more
# You can display this via systemd's journalctl: journalctl -f -u homebridge
# DEBUG=*
EOF

$ sudo tee /etc/systemd/system/homebridge-YOUR_INSTANCE_NAME.service << EOF
[Unit]
Description=Node.js HomeKit Server
After=syslog.target network-online.target

[Service]
Type=simple
User=homebridge
EnvironmentFile=/etc/default/homebridge-YOUR_INSTANCE_NAME
ExecStart=/home/pi/.npm-global/bin/homebridge \$HOMEBRIDGE_OPTS
Restart=on-failure
RestartSec=10
KillMode=process

[Install]
WantedBy=multi-user.target
EOF

# Create systemd service user
$ sudo useradd --system homebridge

# Create Homebridge config files
$ sudo mkdir -p /var/lib/homebridge-YOUR_INSTANCE_NAME
$ sudo cp -R ~/.homebridge/persist /var/lib/homebridge-YOUR_INSTANCE_NAME
$ sudo chmod -R 0777 /var/lib/homebridge-YOUR_INSTANCE_NAME
$ sudo tee /var/lib/homebridge-YOUR_INSTANCE_NAME/config.json << EOF
{
  "bridge": {
    "name": "Homebridge-YOUR_INSTANCE_NAME",
    "username": "1A:2B:3C:4D:5E:6F",
    "port": 45525,
    "pin": "937-19-468"
  },
  "description": "SmartHome with Homebridge",
  "accessories": [],
  "platforms": []
}
EOF
# Replace "1A:2B:3C:4D:5E:6F" with a username generated by this tool: https://www.miniwebtool.com/mac-address-generator/
# Replace "937-19-468" with other digits, in the same format

# Enable the `homebridge` systemd service user to access files in /var/lib/homebridge-YOUR_INSTANCE_NAME
$ sudo chown -R homebridge:homebridge /var/lib/homebridge-YOUR_INSTANCE_NAME

# Enable the systemd service
$ sudo systemctl daemon-reload
$ sudo systemctl enable homebridge-YOUR_INSTANCE_NAME
$ sudo systemctl start homebridge-YOUR_INSTANCE_NAME

# Restart your Raspberry Pi
$ sudo reboot

# After reconnecting, verify the systemd service started successfully
$ sudo systemctl status homebridge-YOUR_INSTANCE_NAME

# View a QR code which is scannable in the Home app to add the Homebridge accessory
$ sudo journalctl -u homebridge-YOUR_INSTANCE_NAME -a -n 35

# View systemd service logs from the top
$ sudo journalctl -u homebridge-YOUR_INSTANCE_NAME -a

# View latest systemd service logs
$ sudo journalctl -u homebridge-YOUR_INSTANCE_NAME -f

Additional Resources

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