Home / Home Automation / All about Homebridge, drive your smarthome from iOS

All about Homebridge, drive your smarthome from iOS

Homebridge is an Open Source project developed by Nick Farina (Github, website ). It is a modular project which can add plugins depending on the material you want to add to the iPhone (or iPad). There are already more than 260 plugins (the list complete here) and you can even expand your (examples to start).

You have plugins for leading home automation software (Domoticz, Home Assistant, OpenHAB, Jeedom, FHEM…) and for many Devices (bulbs Philips Hue, Synology NAS…).

Install Homebridge on Raspbian

Let’s start with updating the system

sudo apt-get update 
sudo apt-get upgrade

If your distribution does not support the automatic expansion of the partition (at first launch, it is for example the case for Domoticz image), run raspi-config.

 sudo raspi-config

Go to Option 1 – Expand Filesystem. Then Finish and finally Allow reboot.

Once re-connected, we can install homebridge

sudo npm set prefix '/usr' -g 
sudo update -g homebridge --unsafe-perm

Find the Mac address of your Raspberry PI. It is unrated but without this configuration (trick found here), you may not be able to detect the HB bridge on iPhone (or iPad).

Run ifconfig . The Mac address of the RPI is HWaddr right .

hombridge domoticz adresse mac rpi hwaddr

Open the configuration file in a text editor (pico or nano)

 sudo nano ~/.homebridge/config.json

Change the value of the key username Mac using the address of your RPI. You can assign a personal Pin code. Save (Ctrl + X) then O (or Y). The following example is taken from the installation for Domoticz tutorial . You will need to adjust the plugin you need.

homebrige domoticz edomoticz config
Example of configuration of Homebridge for Domoticz.

Launch HB manually with the command homebridge.

homebrige domoticz premier demarrage
At startup, home automation devices are presented to iOS

At startup, HB search all devices configured and makes them visible by an iPhone or an iPad connected to the local network.

At he moment, “hot change” are not able. We need to restart Homebridge manually whenever you add a material. This is so a few useful commands.

Launch Homebridge automatically at startup Raspberry

Check first of all that the sources of HB have been installed globally.

sudo npm install -g homebridge

Then it publishes the init.d file

 sudo nano etc/init.d/homebridge

Copy this code in the file (original source code).

#!/bin/sh
### BEGIN INIT INFO
# Provides: homebridge
# Required-Start: $network $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO

dir="/home/pi"
cmd="DEBUG=* /usr/local/bin/homebridge"
user="pi"

name=`basename $0`
pid_file="/var/run/$name.pid"
stdout_log="/var/log/$name.log"
stderr_log="/var/log/$name.err"

get_pid() {
cat "$pid_file"
}

is_running() {
[ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
}

case "$1" in
start)
if is_running; then
echo "Already started"
else
echo "Starting $name"
cd "$dir"
if [ -z "$user" ]; then
sudo $cmd >> "$stdout_log" 2>> "$stderr_log" &
else
sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" &
fi
echo $! > "$pid_file"
if ! is_running; then
echo "Unable to start, see $stdout_log and $stderr_log"
exit 1
fi
fi
;;
stop)
if is_running; then
echo -n "Stopping $name.."
kill `get_pid`
for i in {1..10}
do
if ! is_running; then
break
fi

echo -n "."
sleep 1
done
echo

if is_running; then
echo "Not stopped; may still be shutting down or shutdown may have failed"
exit 1
else
echo "Stopped"
if [ -f "$pid_file" ]; then
rm "$pid_file"
fi
fi
else
echo "Not running"
fi
;;
restart)
$0 stop
if is_running; then
echo "Unable to stop, will not attempt to start"
exit 1
fi
$0 start
;;
status)
if is_running; then
echo "Running"
else
echo "Stopped"
exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac

exit 0

We need to make the script executable

 sudo chmod 755 etc/init.d/homebridge

And now we ask the OS to start the script at startup

 sudo update -rc.d homebridge defaults

You can run it manually with the command

 sudo /etc/init.d/homebridge start

If HB is already running, the script will inform you and will do nothing more.

Other useful commands for fly Homebridge

Stop

To manually stop

 sudo /etc/init.d/homebridge stop

Know the State

Run the following command to see if the service is started or stopped.

 sudo /etc/init.d/homebridge status

Restart

You come to make a change in the configuration file ~/.homebridge/config.json, run the option restart to revive.

 sudo /etc/init.d/homebridge restart

Check logs

Once HB is configured to start at boot, we have more visual feedback of what’s happening. Here are two other very useful commands to access logs in real time.

 tail-f var/log/homebridge.log

And look at the log of errors

 tail-f var/log/homebridge.err

Sources :