{"id":286,"date":"2017-11-20T22:03:15","date_gmt":"2017-11-20T21:03:15","guid":{"rendered":"https:\/\/npham.dk\/?p=286"},"modified":"2017-11-20T22:04:51","modified_gmt":"2017-11-20T21:04:51","slug":"homebridge-with-sonoff","status":"publish","type":"post","link":"https:\/\/npham.dk\/?p=286","title":{"rendered":"Homebridge with Sonoff"},"content":{"rendered":"<h2>Sonoff<\/h2>\n<p>I bought\u00a0two of those cheap wifi remote controlled switches called Sonoff.<br \/>\nThese switches allow you to turn them on and off with an app on your phone.<br \/>\n<a href=\"https:\/\/www.itead.cc\/sonoff-wifi-wireless-switch.html\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/www.itead.cc\/sonoff-wifi-wireless-switch.html<\/a><\/p>\n<p>Inside these switches are a ESP8266 , which means that we can flash the chip with our own custom firmware.<br \/>\nAnd with the help of a Raspberry pi\u00a0we can enable homekit.<\/p>\n<p>Firstly I reflashed the sonoff with my custom firmware, using the Arduino IDE. (Google how to flash ESP8266)<br \/>\n(This disables the use of the official sonoff app)<\/p>\n<p><a href=\"https:\/\/github.com\/kim82\/sonoff\">https:\/\/github.com\/kim82\/sonoff<\/a><\/p>\n<p>Remember to change the\u00a0hostname, if you have multiple sonoff.<\/p>\n<pre>#define HOSTNAME        \"sonoff_00\"\r\n<\/pre>\n<h2>Raspberry Pi &#8211; Homebridge<\/h2>\n<p>Download and install Raspbian on the Raspberry Pi.<br \/>\n<a href=\"https:\/\/www.raspberrypi.org\/downloads\/raspbian\/\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/www.raspberrypi.org\/downloads\/raspbian\/<\/a><\/p>\n<p>When this is done we can install Homebridge, which is the service that sits between your apple devices and the sonoff switch.<\/p>\n<p>The full install guide can be found here:<br \/>\n<a href=\"https:\/\/github.com\/nfarina\/homebridge\/wiki\/Running-HomeBridge-on-a-Raspberry-Pi\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/nfarina\/homebridge\/wiki\/Running-HomeBridge-on-a-Raspberry-Pi<\/a><\/p>\n<p><strong>TLDR; (Raspberry Pi 3)<\/strong><\/p>\n<pre>sudo apt-get update\r\nsudo apt-get upgrade\r\nsudo apt-get install git make\r\nsudo apt-get install g++<\/pre>\n<pre>#install node\r\ncurl -sL https:\/\/deb.nodesource.com\/setup_8.x <span class=\"pl-k\">|<\/span> sudo -E bash -\r\nsudo apt-get install -y nodejs<\/pre>\n<pre>#install Avahi and other dependencies\r\nsudo apt-get install libavahi-compat-libdnssd-dev<\/pre>\n<h3>Script2<\/h3>\n<p>Next we need to install Script2, which is a homebridge plugin that\u00a0allows you to run bash-scripts though the homebridge<\/p>\n<pre>npm install -g homebridge-script2<\/pre>\n<h3>Start at bootup<\/h3>\n<p>Lastly we need to start Homebridge\u00a0whenever the Raspberry pi boots up.<br \/>\nAnd the\u00a0following method was found here:<br \/>\n<a href=\"https:\/\/gist.github.com\/johannrichard\/0ad0de1feb6adb9eb61a\/\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/gist.github.com\/johannrichard\/0ad0de1feb6adb9eb61a\/<\/a><\/p>\n<p>We need to create the following two files.<\/p>\n<p><strong>\/etc\/default\/homebridge<\/strong><\/p>\n<pre># Defaults \/ Configuration options for homebridge\r\n# The following settings tells homebridge where to find the config.json file and where to persist the data (i.e. pairing and others)\r\nHOMEBRIDGE_OPTS=-U \/var\/lib\/homebridge\r\n<\/pre>\n<p><strong>\/etc\/systemd\/system\/homebridge.service<\/strong><\/p>\n<pre>[Unit]\r\nDescription=Node.js HomeKit Server \r\nAfter=syslog.target network-online.target\r\n\r\n[Service]\r\nType=simple\r\nUser=homebridge\r\n\r\n#<em><strong>could be \/usr\/local\/bin\/homebridge - check where the homebridge folder exists<\/strong><\/em>\r\nEnvironmentFile=\/etc\/default\/homebridge \r\n\r\nExecStart=\/usr\/bin\/homebridge $HOMEBRIDGE_OPTS\r\nRestart=on-failure\r\nRestartSec=10\r\nKillMode=process\r\n\r\n[Install]\r\nWantedBy=multi-user.target<\/pre>\n<p>And to enable the newly created service. (first time only)<\/p>\n<pre>sudo systemctl daemon-reload\r\nsudo systemctl enable homebridge\r\nsudo systemctl start homebridge\r\n<\/pre>\n<p>We also need to create a new user and the folder for the config.json file<\/p>\n<pre>#create a new user\r\nsudo useradd -M --system homebridge\r\n\r\n#creates a new folder\r\nsudo mkdir \/var\/lib\/homebridge\r\n\r\n#changes the owner of the folder to new homebridge-user\r\nsudo chown homebridge:homebridge \/var\/lib\/homebridge\r\n<\/pre>\n<h3>Configuration<\/h3>\n<p>To configure the script2, we need to create a config.json file in the \/var\/lib\/homebridge folder.<\/p>\n<p>Mine looks like this:<\/p>\n<pre>{\r\n    \"bridge\": {\r\n        \"name\": \"Sonoff\",\r\n        \"username\": \"AA:BB:CC:DD:EE:FF\", <em><strong>(THIS NEEDS TO BE CHANGED)<\/strong><\/em>\r\n        \"port\": 40500,\r\n        \"pin\": \"XXX-XX-XXX\" <em><strong>(THIS NEEDS TO BE CHANGED - WITH NUMBERS)<\/strong><\/em>\r\n    },\r\n\r\n    \"description\": \"Sonoff Homebridge\",\r\n\r\n    \"accessories\": [\r\n                    {\r\n                    \"accessory\": \"Script2\",\r\n                    \"name\": \"Bedroom lamp\",\r\n                    \"on\": \"curl http:\/\/sonoff_00.local\/on\",\r\n                    \"off\": \"curl http:\/\/sonoff_00.local\/off\",\r\n                    \"state\": \"curl --max-time 1 http:\/\/sonoff_00.local\",\r\n                    \"fileState\": \"\",\r\n                    \"on_value\": \"on\"\r\n                    },\r\n                    {\r\n                    \"accessory\": \"Script2\",\r\n                    \"name\": \"Living room lamp\",\r\n                    \"on\": \"curl http:\/\/sonoff_01.local\/on\",\r\n                    \"off\": \"curl http:\/\/sonoff_01.local\/off\",\r\n                    \"state\": \"curl --max-time 1 http:\/\/sonoff_01.local\",\r\n                    \"fileState\": \"\",\r\n                    \"on_value\": \"on\"\r\n                    }\r\n                   ],\r\n\r\n    \"platforms\": []\r\n}\r\n<\/pre>\n<p>Restart the homebridge service<\/p>\n<pre>sudo systemctl restart homebridge<\/pre>\n<p>Now you should be able to add the sonoff homebridge to your Home-app on the iphone\/ipad, and control the switches.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sonoff I bought\u00a0two 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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/npham.dk\/index.php?rest_route=\/wp\/v2\/posts\/286"}],"collection":[{"href":"https:\/\/npham.dk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/npham.dk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/npham.dk\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/npham.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=286"}],"version-history":[{"count":6,"href":"https:\/\/npham.dk\/index.php?rest_route=\/wp\/v2\/posts\/286\/revisions"}],"predecessor-version":[{"id":292,"href":"https:\/\/npham.dk\/index.php?rest_route=\/wp\/v2\/posts\/286\/revisions\/292"}],"wp:attachment":[{"href":"https:\/\/npham.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/npham.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/npham.dk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}