Homebridge with Sonoff

Sonoff

I bought two of those cheap wifi remote controlled switches called Sonoff.
These switches allow you to turn them on and off with an app on your phone.
https://www.itead.cc/sonoff-wifi-wireless-switch.html

Inside these switches are a ESP8266 , which means that we can flash the chip with our own custom firmware.
And with the help of a Raspberry pi we can enable homekit.

Firstly I reflashed the sonoff with my custom firmware, using the Arduino IDE. (Google how to flash ESP8266)
(This disables the use of the official sonoff app)

https://github.com/kim82/sonoff

Remember to change the hostname, if you have multiple sonoff.

#define HOSTNAME        "sonoff_00"

Raspberry Pi – Homebridge

Download and install Raspbian on the Raspberry Pi.
https://www.raspberrypi.org/downloads/raspbian/

When this is done we can install Homebridge, which is the service that sits between your apple devices and the sonoff switch.

The full install guide can be found here:
https://github.com/nfarina/homebridge/wiki/Running-HomeBridge-on-a-Raspberry-Pi

TLDR; (Raspberry Pi 3)

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git make
sudo apt-get install g++
#install node
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
#install Avahi and other dependencies
sudo apt-get install libavahi-compat-libdnssd-dev

Script2

Next we need to install Script2, which is a homebridge plugin that allows you to run bash-scripts though the homebridge

npm install -g homebridge-script2

Start at bootup

Lastly we need to start Homebridge whenever the Raspberry pi boots up.
And the following method was found here:
https://gist.github.com/johannrichard/0ad0de1feb6adb9eb61a/

We need to create the following two files.

/etc/default/homebridge

# 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

/etc/systemd/system/homebridge.service

[Unit]
Description=Node.js HomeKit Server 
After=syslog.target network-online.target

[Service]
Type=simple
User=homebridge

#could be /usr/local/bin/homebridge - check where the homebridge folder exists
EnvironmentFile=/etc/default/homebridge 

ExecStart=/usr/bin/homebridge $HOMEBRIDGE_OPTS
Restart=on-failure
RestartSec=10
KillMode=process

[Install]
WantedBy=multi-user.target

And to enable the newly created service. (first time only)

sudo systemctl daemon-reload
sudo systemctl enable homebridge
sudo systemctl start homebridge

We also need to create a new user and the folder for the config.json file

#create a new user
sudo useradd -M --system homebridge

#creates a new folder
sudo mkdir /var/lib/homebridge

#changes the owner of the folder to new homebridge-user
sudo chown homebridge:homebridge /var/lib/homebridge

Configuration

To configure the script2, we need to create a config.json file in the /var/lib/homebridge folder.

Mine looks like this:

{
    "bridge": {
        "name": "Sonoff",
        "username": "AA:BB:CC:DD:EE:FF", (THIS NEEDS TO BE CHANGED)
        "port": 40500,
        "pin": "XXX-XX-XXX" (THIS NEEDS TO BE CHANGED - WITH NUMBERS)
    },

    "description": "Sonoff Homebridge",

    "accessories": [
                    {
                    "accessory": "Script2",
                    "name": "Bedroom lamp",
                    "on": "curl http://sonoff_00.local/on",
                    "off": "curl http://sonoff_00.local/off",
                    "state": "curl --max-time 1 http://sonoff_00.local",
                    "fileState": "",
                    "on_value": "on"
                    },
                    {
                    "accessory": "Script2",
                    "name": "Living room lamp",
                    "on": "curl http://sonoff_01.local/on",
                    "off": "curl http://sonoff_01.local/off",
                    "state": "curl --max-time 1 http://sonoff_01.local",
                    "fileState": "",
                    "on_value": "on"
                    }
                   ],

    "platforms": []
}

Restart the homebridge service

sudo systemctl restart homebridge

Now you should be able to add the sonoff homebridge to your Home-app on the iphone/ipad, and control the switches.

Leave a Reply

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