树莓派与HomeKit智能家庭

分类:IOT

前言

前段时间入了一块树莓派Model 3B, 目的是实现在Pi上运行兼容HomeKit协议的Homebridge插件进而实现用Siri控制家里的米家台灯、小米空气净化器2等硬件的目的.

插件安装

换源:

由于某些原因,使用默认的源下载可能不太稳定

$ sudo nano /etc/apt/sources.list
# 添加以下内容
deb http://mirrors.aliyun.com/raspbian/raspbian/ jessie main non-free contrib
deb-src http://mirrors.aliyun.com/raspbian/raspbian/ jessie main non-free contrib
# 按ctrl+o, 回车, ctrl+x 退出
# 更新列表
$ sudo apt-get update
$ sudo apt-get upgrade

安装Node.js:

这里要使用6.x版本, 否则后面会报错

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

安装Avahi:

$ sudo apt-get -f remove libdbus-1-3
$ sudo apt-get install aptitude
$ sudo aptitude install libavahi-compat-libdnssd-dev

安装Homebridge和其他插件:

# 安装homebridge插件
$ sudo npm install -g --unsafe-perm homebridge
# 可选, 用于小米网关2、aqara墙壁开关等硬件
$ sudo npm install -g homebridge-mi-aqara
# 可选,yeelight灯具,比如米家台灯
$ sudo npm install -g homebridge-yeelight
# 可选,用于小米空气净化器2
$ sudo npm install -g homebridge-xiaomi-air-purifier miio

更新npm插件

#查看过时插件
npm -g outdated
#更新到最新版本
sudo npm update homebridge -g --unsafe-perm

Homebridge配置

查找设备token

#查看所有小米设备:
miio --discover

添加配置

$ nano /home/pi/.homebridge/config.json

{
    "bridge": {
        "name": "Homebridge",
        "username": "CC:21:3D:E5:C4:38", #随意填写
        "port": 51826,
        "pin": "321-45-013" #添加设备到HomeKit要用的密码
    },
    "platforms": [{
        "platform": "MiAqaraPlatform",
        "sid": ["34ce008be61c"],#米家app,网关,关于,连续点击空白处
        "password": ["ADE5EA05091A4D30"]},
        {
        "platform": "yeelight",
        "name": "yeelight"
        }],
    "accessories": [
    {
      "accessory": "XiaoMiAirPurifier",
      "name": "Air Purifier",
      "showTemperature": true,
      "showHumidity": true,
      "showAirQuality": true,
      "address": "192.168.1.133",#路由器中绑定的空气净化器的IP
      "token": "7d52f6dfebe6c27990dfddaf********",
      "model": "zhimi.airpurifier.m1"
    }
    ]
}

开机自启

通过 init.d 实现开机自动启动 Homebridge

sudo vi /etc/init.d/homebridge

粘贴以下内容:

#!/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/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

保存,退出,运行下面的命令:

sudo chmod 755 /etc/init.d/homebridge
sudo update-rc.d homebridge defaults

手动开启服务:

sudo /etc/init.d/homebridge start

查看log和error:

tail -f /var/log/homebridge.log
tail -f /var/log/homebridge.err

小米空气净化器2:

Device ID: 61261267 Model info: zhimi.airpurifier.m1 (air-purifier) Address: 192.168.11.133 (zhimi-airpurifier-m1_miio61261267) Token: 7d52f6dfebe6c27990dfddaf9d0c037d via auto-token Support: At least basic

参考资料

  1. 入门智能家居,从米家到HomeKit
  2. 智能宿舍搭建 1 | 借助树莓派与 HomeBridge ,将 YeeLight 彩光灯接入 Apple HomeKit
  3. 普通家具变智能 用Siri声控家里电器