Running HomeBridge on a Raspberry Pi

Introduction

Finding your Pi

Basic setup preparations

Install C++14 (Skip if this part if you are on Raspbian Jessie)

Install Node

Node.js manual install

Node.js package manager (apt-get) install

Node.js binaries install

Install Avahi and other Dependencies

Install Homebridge and dependencies

Proceed as Usual

Running Homebridge on Bootup

Running Homebridge on Bootup (systemd)

Configuration

Notes

Running Homebridge on Bootup (init.d)

Running Homebridge on Boot (/etc/rc.local) using Screen

Introduction

Running HomeBridge on a Raspberry Pi is straightforward. These instructions have been tested on a Raspberry Pi 2 with default Raspbian OS installed via NOOBS, and a Raspberry Pi 3 withRaspbian Jessie Lite.

For help installing an operating system like NOOBS on your Pi, check theofficial Raspberry Pi documentation.

For a video (in English), checkthisout.

Finding your Pi

After you install the operating system and connect your Pi to your network (and into power), you'll need to locate it so you cansshinto it and run some commands.

The default "Raspbian" OS will automatically broadcast its presence on your network under the mDNS name "raspberrypi". If you are using Mac or Linux, you can reach your Pi easily:

ssh pi@raspberrypi.local

Note that in SSH is disabled by default in recent version of Raspbian;see this page for instructions to re-enable it.

The default username for Raspbian ispiand the password israspberry.

If you have a different OS installed on your Pi or you can't find it viapi@raspberrypi.localthen you can try connecting to your home router by pointing your web browser at somewhere likehttp://192.168.0.1,http://192.168.1.1,http://10.1.1.1etc. (this depends on the router you're using and your network setup). Once you are logged in, you can usually find a list of devices connected to your network under "DHCP".

Windows does not supportsshon the command line, but you can use a free SSH client likePuttyto connect to your Pi.

Basic setup preparations

Once you're logged into your Pi, you should begin by updating the default system packages (note that these commands may be different if you are not running Raspbian OS).

sudo apt-get update

sudo apt-get upgrade

For OSMC or other stripped down Raspberry OS, you may need to install git and make

sudo apt-get install git make

Install C++14 (Skip this part if you are on Raspbian Jessie)

If you are running a version of the Raspbian OS based on Debian Wheezy, it comes with a C++ compiler that is too old for some of the packages Homebridge requires. You'll need tofollow these instructionsto install an updated version of the C++14 compiler.

You should be able to type:

>g++-4.9 -v...gcc version 4.9.2 (Raspbian 4.9.2-10)

Now you have the needed compiler, but it is not yet set to be thedefaultcompiler. You can fix that usingthe instructions here.

In my case I simply did this:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.9

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.7

You should be able to now type:

>g++ -v...gcc version 4.9.2 (Raspbian 4.9.2-10)

For OSMC or other stripped down Raspberry OS, you may need to install g++

sudo apt-get install g++

Install Node

Starting with version 4.0.0, NodeJS now supports ARM-based platforms like Raspberry Pi by default.

Node.js package manager install (Easiest, Preferred)

If you are running a newish Raspberry Pi with an ARMv7 chip or better, you can install NodeJS using their apt-get repository.Follow the instructions here.

Example:

curl -sL https://deb.nodesource.com/setup_7.x|sudo -E bash -sudo apt-get install -y nodejs

To check what chip your Raspberry Pi has, type:

uname -a

The response will look something like:

Linux raspberrypi 4.4.50-v7+ 970 SMP Mon Feb 20 19:18:29 GMT 2017 armv7l GNU/Linux

Notice thearmv7lwhich indicates that this method will work.

Node.js binaries install

If you have an older ARM chip, you can grab Node binaries forarmv6fromhere.

Example:

wget https://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-armv6l.tar.xztar xJvf node-v6.9.5-linux-armv6l.tar.xzsudo mkdir -p /opt/nodesudo mv node-v6.9.5-linux-armv6l/*/opt/node/sudo update-alternatives --install"/usr/bin/node""node""/opt/node/bin/node"1sudo update-alternatives --install"/usr/bin/npm""npm""/opt/node/bin/npm"1

Node.js manual install

To manually install Node.js you can follow either of these guides:

Installing Node.js v4.0.0 on a Raspberry Pi (All Models).

Installing Node.js on a Raspberry Pi 3

Following those instructions explicitly will install version 4.0.0. There are many new versions published since then; to install the latest version (or any other version, homebridge now requires a minimal nodejs version of v5.10.0), modify those instructions with the appropriate directory in thenodejs.org repository.

Install Avahi and other Dependencies

This is required by themdnspackage inHAP-NodeJSlibrary.

sudo apt-get install libavahi-compat-libdnssd-dev

Proceed as Usual

Now you can simply follow the instructions in theREADMEto install HomeBridge and start it up.

Alternate Installation (Untested)

If the "proceed as usual" section above didn't work for you, you might try these instructions which another user posted (but I have not verified because the simplenpm install -g homebridgeworked for me):

Only try these if normal way doesn't work!

sudo npm install -g --unsafe-perm homebridge hap-nodejs node-gypcd/usr/local/lib/node_modules/homebridge/sudo npm install --unsafe-perm bignumcd/usr/local/lib/node_modules/hap-nodejs/node_modules/mdnssudo node-gyp BUILDTYPE=Release rebuild

(note that/usr/local/libshould be replaced with/usr/lib/if you installed node using apt-get method above or with/opt/node/lib/if binary method used)

Running Homebridge on Bootup

Note that you should first make sure you can successfully run HomeBridge manually as described in the mainREADME

If you are new to Linux and the Raspberry Pi, it is recommended you read the instructions for all three methods before implementing your chosen method.

Running Homebridge on Bootup (systemd)

Follow thisGuide

On newer Raspberry Pi and Debian systems (Jessie +), managing of services withinit.dis (transparently) replaced withsystemd. If you wish to usesystemdfor running Homebridge on boot, you can find instructions in a Gist underjohannrichard/homebridge. As you can see, the service definition is much shorter than a comparable init.d script.

Download the two files and placehomebridgeunder/etc/defaultandhomebridge.serviceunder/etc/systemd/systemon your Raspberry Pi.

Configuration

In order to use the systemd service as is, the following folders and user have to exists:

A system user namedhomebridge. You can easily create this user withuseradd --system homebridgeor choose a different name

A directory called/var/lib/homebridge, writable by the user created above, and a correspondingconfig.jsonfile in that directory. Homebridge by default looks for its configuration in/home//.homebridge. This is unsuitable for services and the-U /var/lib/homebridgeflag ensures the config is read from a different place.

Then Enable and run the service (first time) with the following commands:

systemctl daemon-reload

systemctl enable homebridge

systemctl start homebridge

You can check the status of the service by calling

systemctl status homebridge

On subsequent reboots, it should start automatically, if not, use thejournalctl -u homebridgeto check the error cause.

Notes

The service will restart after 10 seconds if it fails for any reason (or if you kill it for example withkill -s SIGSEGV )

You might also try the tips in thisTutorial.

Running Homebridge on Bootup (init.d)

If you would like your Pi to start up Homebridge automatically on reboot, you will need to install an "init script". This freeinit script templateis a great place to start.

For example, first go to theraw template fileand select the whole page and copy it to your clipboard. Then connect to your pi:

ssh pi@raspberrypi.local

sudo nano /etc/init.d/homebridge

[paste clipboard contents]

Now you'll need to modify the top of the file. Here's an example:

#!/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 INFOdir="/home/pi"cmd="DEBUG=* /usr/local/bin/homebridge"user="pi"

This assumes you have installed Homebridge globally viasudo npm install -g homebridge.

Now type CTRL+o to save, then enter, then CTRL+x to exit. Now change the file permissions and "install" the script:

sudo chmod 755 /etc/init.d/homebridge

sudo update-rc.d homebridge defaults

It should now run when your Pi reboots. You can also start it up manually like this:

sudo /etc/init.d/homebridge start

To view the running logs, you cantailthe output log or error log:

tail -f /var/log/homebridge.log

tail -f /var/log/homebridge.err

Running Homebridge on Boot (/etc/rc.local) using Screen

If you would like your Pi to start up HomeBridge automatically in a Screen session on reboot, you need to install Screen and edit the Pi's/etc/rc.localfile.

Install Screen

sudo apt-get install screen

Edit/etc/rc.local

sudo nano /etc/rc.local

Add this line before theexit 0line:

su -c"screen -dmS homebridge homebridge"-s /bin/sh pi

Hit Ctrl+X, Y to save and exit.

Usingrc.localis similar to adding things to your Login items on macOS or putting things in your Startup folder on Windows. The command is run once when you boot the Pi.

The example above will open HomeBridge using Screen. Screen acts like a separate terminal session that is run on the server rather than on your local computer. The advantage of this is that you can disconnect from the server without disrupting whatever was running in the Screen session. Typically when you open a terminal session session from one computer to another, when you exit (or get disconnected) everything running in the session at that time would be stopped (read more here:https://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/).

Unless you are familiar with Screen or have a specific reason for using Screen, it is recommended that you set up HomeBridge as a Service usingsystemdorinit.d. If running Jessie or above then ideallysystemd(especially if you are new to Linux)

Whilst it would get HomeBridge running on boot, using Screen does add an extra layer of complexity. Using a Service is considered the "correct" way and has other advantages notably self restarting after a fail.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,748評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,165評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,595評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,633評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,435評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,943評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,035評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,175評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,713評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,599評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,788評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,303評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,034評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,412評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,664評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,408評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,747評論 2 370

推薦閱讀更多精彩內容