Raspberry Pi に Homebridge をインストールする
CentOS 6 へインストールを試みたのだけれど全くうまくいかないので、常時動いている Raspberry Pi (IP: 192.168.11.20) がいたのでそちらで使えるようにすることにした。
基本的には Homebridge の Wiki を読んでその通りに行う。
システムをアップデート
$ sudo apt-get update $ sudo apt-get upgrade
アップデート中に次のように訊かれた。Diff を取ってみたらおそらくこの Raspberry Pi で動いている別プログラムの GPIO まわりの設定に関してだったので、弄らないほうがよかろうという判断で現在のバージョンを維持した。
Configuration file '/etc/udev/rules.d/99-com.rules' ==> File on system created by you or by a script. ==> File also in package provided by package maintainer. What would you like to do about it ? Your options are: Y or I : install the package maintainer's version N or O : keep your currently-installed version D : show the differences between the versions Z : start a shell to examine the situation The default action is to keep your current version. *** 99-com.rules (Y/I/N/O/D/Z) [default=N] ?
C++14 が利用可能であることを確認。
$ g++ -v ... gcc version 4.9.2 (Raspbian 4.9.2-10)
Node インストール
Raspberry Pi Model B+ なので armv6l
用のを選択(Raspberry Pi 2 は armv7l
のを選択するようだ)。現在の最新版を入れておく。
$ wget https://nodejs.org/dist/v6.2.2/node-v6.2.2-linux-armv6l.tar.gz $ tar -xvf node-v6.2.2-linux-armv6l.tar.gz $ cd node-v6.2.2-linux-armv6l $ sudo cp -R * /usr/local/ $ cd .. $ node -v v6.2.2
Avahi インストール
$ sudo apt-get install libavahi-compat-libdnssd-dev
Homebridge インストール
--unsafe-perm
をつけないとエラーが出てインストール出来ない。つけても下記のようなエラーが出るが、source compile にフォールバックしてなんとかなっているようだ。
$ sudo npm install -g homebridge --unsafe-perm ... node-pre-gyp ERR! Tried to download: https://rvagg-node.s3-us-west-2.amazonaws.com/bignum/v0.12.5/bignum-v0.12.5-node-v48-linux-arm.tar.gz node-pre-gyp ERR! Pre-built binaries not found for bignum@0.12.5 and node@6.2.2 (node-v48 ABI) (falling back to source compile with node-gyp) ...
これでとりあえず homebridge
を起動できるようにはなった。確認。
$ homebridge
番号みたいなのが表示されたら成功。Warning が6行ぐらい出るが無視しろと 公式に書かれている。
homebridge-irkit, homebridge-cmd インストール
$ sudo npm install -g homebridge-irkit $ sudo npm install -g homebridge-cmd
(2016-07-27 追記)IRKit を使う際には、常に Bonjour が使える環境にあるとも限らないので、IP アドレスを固定するのがよい。 IRKit 側からは固定できないので、公式 にある方法で IP アドレスを発見してから、ルーター側の設定で IRKit の MAC アドレスと IP アドレスの関連を固定させる。
自宅のルーターでは Internet/LAN > DHCPリース の項目から可能だった。
IRKit の現在の IP を調べるには Mac で以下のコマンドを実行。
$ dns-sd -B _irkit._tcp $ dns-sd -G v4 irkitxxxx.local
(2016-07-27 追記ここまで)
設定
設定ファイルの位置は ~/.homebridge/config.json
と決まっているので、このファイルを作成して設定を書いていく。今回は、リトライ数などのきめ細かい制御は Python で記述し、Homebridge からはそのスクリプトを呼び出すだけとした。
なお、このファイルは homebridge の daemon 化の際には使用しない。テスト用である。よってテストが不要の場合はここを飛ばして daemon 化に進んでしまってかまわない(そこでまたこの内容を入力するステップがある)。
$ vi ~/.homebridge/config.json
{ "bridge": { "name": "Homebridge", "username": "CD:22:3D:E3:CE:30", "port": 51826, "pin": "031-45-154" }, "description": "Samoyed!", "platforms": [], "accessories": [ { "accessory": "CMD", "name": "リビングの照明", "on_cmd": "python /home/pi/Documents/python/irkit.py living_light select_b", "off_cmd": "python /home/pi/Documents/python/irkit.py living_light off" }, { "accessory": "CMD", "name": "寝室の照明", "on_cmd": "python /home/pi/Documents/python/irkit.py bedroom_light full", "off_cmd": "python /home/pi/Documents/python/irkit.py bedroom_light off" }, { "accessory": "CMD", "name": "暖房", "on_cmd": "python /home/pi/Documents/python/irkit.py aircon heating_on", "off_cmd": "python /home/pi/Documents/python/irkit.py aircon off" }, { "accessory": "CMD", "name": "冷房", "on_cmd": "python /home/pi/Documents/python/irkit.py aircon cooling_on", "off_cmd": "python /home/pi/Documents/python/irkit.py aircon off" } ] }
daemon化
$ sudo useradd --system homebridge $ sudo mkdir /var/homebridge $ sudo chown homebridge:homebridge /var/homebridge/ $ sudo su homebridge -s /bin/bash $ vi /var/homebridge/config.json # 内容は上述の config.json と同一。 $ exit
$ sudo vi /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/homebridge # If you uncomment the following line, homebridge will log more # You can display this via systemd's journalctl: journalctl -f -u homebridge # DEBUG=*
$ sudo vi /etc/systemd/system/homebridge.service
システムによって実行ファイルの位置が異なる可能性があるので、事前に $ which homebridge
を実行しておき ExecStart にはそのパスを記載する。
[Unit] Description=Node.js HomeKit Server After=syslog.target [Service] Type=simple User=homebridge EnvironmentFile=/etc/default/homebridge ExecStart=/usr/local/bin/homebridge $HOMEBRIDGE_OPTS Restart=on-failure RestartSec=10 KillMode=process [Install] WantedBy=multi-user.target
$ sudo systemctl daemon-reload $ sudo systemctl enable homebridge $ sudo systemctl start homebridge
$ sudo systemctl status homebridge
で状態を確認。 $ sudo journalctl -u homebridge
でエラーを見る。
iOS 側からは新しい機器として認識されるので、再度登録し直す必要がある。再登録時に必要な番号を見るには、やはり $ sudo journalctl -u homebridge
を用いる。再登録に問題が生じる場合は、このあたり を参考に、/var/homebridge/persist/
を消したり username
を変更したりする。