树莓派上HomeBridge 的安装

Posted:   2020-04-14

Edited:   2020-04-15

Status:   Completed

Categories :   树莓派 HomeBridge

Previous:   BLE--CC2541的入门之特征值

Next:   树莓派设置


参考连接

安装

安装Node.js

1
2
3
4
curl -sL https://deb.nodesource.com/setup_12.x | sudo bash -
sudo apt-get install -y nodejs gcc g++ make python
node -v
sudo npm install -g npm

错误处理

  • 若安装出现错误,就清理缓存解决
1
2
npm cache verify
npm cache clean --force

安装Homebridge

1
sudo npm install -g --unsafe-perm homebridge homebridge-config-ui-x

设置

设置开机启动

1
sudo hb-service install --user homebridge

树莓派插件

安装摄像机插件

摄像机配置

  • 系统启动摄像机
  • 将摄像机加到modules里
1
sudo nano /etc/modules
  • 添加bcm2835-v4l2到文件,保存退出
  • 系统重启
1
sudo reboot
  • 安装ffmpeg
    1
    
    sudo apt install ffmpeg
    

安装插件

  • homebridge-config-ui-x(默认账号:admin 密码:admin)插件中,搜索rpi-camera
  • 选择Homebridge Camera Rpi安装

  • 编辑配置config.json,在platforms里添加以下内容
    1
    2
    3
    4
    
      {
        "platform": "rpi-camera",
        "cameras": [{"name": "Pi Camera"}]
      }
    
  • 重启homebridge

安装温湿度插件

  • homebridge-config-ui-x,搜索dht
  • 选择Homebridge Dht安装
  • 编辑config.json
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    
    {
      "bridge": {
          "name": "Homebridge",
          "username": "0E:25:C7:A5:27:FA",
          "port": 51864,
          "pin": "509-96-738"
      },
      "accessories": [
          {
              "accessory": "Dht",
              "name": "Outside"
               "service":"dht11"
          }
      ],
      "platforms": [
          {
              "name": "Config",
              "port": 8581,
              "auth": "form",
              "theme": "auto",
              "tempUnits": "c",
              "lang": "zh-CN",
              "platform": "config"
          },
          {
              "platform": "rpi-camera",
              "cameras": [
                  {
                      "name": "Pi Camera"
                  }
              ]
          }
      ]
    }   
    

小米插件

安装miio

  • 安装
    1
    
    sudo npm install -g miio
    
  • 配置
    1
    
    miio discover
    
  • millo读取不到token,暂时放弃

自定义插件

BH1750

package.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
    "name": "homebridge-rasspberypi-BH1750",
    "version": "1.0.0",
    "description": "This is an Homebridge accessory plugin for illuminance sensor connected via i2c.",
    "main": "index.js",
    "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1"
    },
    "engines": {
        "node": ">=0.12.0",
        "homebridge": ">=0.2.0"
      },
    "keywords":[
    "hombridge-plugin",
    "illuminance"
    ],
    "dependnices":{
    "i2c-bus":">=3.1.0"
    }
}

BH1750.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var i2c = require('i2c-bus');

var BH1750 = function (opts) {
    this.options = opts || {
        address: 0x23,
        bus: 1,
        device: '/dev/i2c-1',
        command: 0x10,
        length: 2
    };
    this.verbose = this.options.verbose || false;
    if (i2c.openSync) {
        this.wire = i2c.openSync(this.options);
     } else {
        this.wire = new i2c(this.options.address, {device: this.options.device});
     }
    if (!this.wire.readBytes) {
        this.wire.readBytes = function(offset, len, callback) {
            this.writeSync([offset]);
            this.read(len, function(err, res) {
                callback(err, res);
            });
        }
    }
};

BH1750.prototype.readLight = function (cb) {
    var self = this;
    if (!cb) {
        throw new Error("Invalid param");
    }
    self.wire.readBytes(self.options.command, self.options.length, function (err, res) {

        if (err) {
            if (self.verbose)          
            return cb(err, null);
        }
        var hi = res[0];
        var lo = res[1];
        if (Buffer.isBuffer(res)) {
           hi = res.readUInt8(0);
           lo = res.readUInt8(1);
        }

        var lux = ((hi << 8) + lo)/1.2;
        if (self.options.command === 0x11) {
            lux = lux/2;
        }
        cb(null, lux);
    });
};

module.exports = BH1750;

index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
var Service, Characteristic;
var BH1750 = require('./bh1750.js');

module.exports = function (homebridge) {
    Service = homebridge.hap.Service;
    Characteristic = homebridge.hap.Characteristic;
    homebridge.registerAccessory('homebridge-rasspberypi-BH1750', 'illuminance', illuminance);
};

function illuminance(log, config) {
    this.log = log;
    this.log("Adding Accessory");
    this.name = config.name;
    this.sensor = new BH1750();
    this.refresh = config.refresh || "60"; // Every minute
}
illuminance.prototype = {
    identity: function (callback) {
        this.log("Identify requested!");
        callback(); // success
    },
    getServices: function () {
        this.log("INIT: %s", this.name);
        this.services.informationService = new Service.AccessoryInformation();
        this.services.informationService
            .setCharacteristic(Characteristic.Manufacturer, 'ZYD')
            .setCharacteristic(Characteristic.Model, 'Illumiance Sensor')
            .setCharacteristic(Characteristic.SerialNumber, this.name);
        this.log('creating Illumiance Sensor');
        this.services.illumianceService = new Service.IllumianceSensor(this.name);
        this.services.illumianceService
            .getCharacteristic(Characteristic.CurrentIllumiance)
            .on('get', this.getIllumiance.bind(this));
        setInterval(function () {
            this.getIllumiance(function (err, illuminance) {
                if (err) {
                    illuminance = err;
                }
                this.illumianceService
                    .getCharacteristic(Characteristic.CurrentIllumiance).updateValue(illuminance);
            }.bind(this));
        }.bind(this), this.refresh * 1000);

        return [this.informationService, this.illumianceService];
    },
    getIllumiance: function (callback) {
        this.sensor.readLight(function (error, value) {
            if (err) {
                this.log("light error: " + err);
                callback(err);
            } else {
                this.log("light value is: ", value, "lx");
                callback(null, value);
            }
        }.bind(this));
    }
};

config.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"bridge": {
        "name": "Homebridge",
        "username": "CC:22:3D:E3:CE:50",
        "port": 55373,
        "pin": "033-73-874"
    },
"accessories": [
        {
            "accessory": "illuminance",
            "name": "illuminance"
        }
    ]
}

调试

1
DEBUG=* homebridge -D -U ~/homebridge-dev/config/ -P ~/homebridge-dev/plugin/