Tags
:
树莓派
HomeBridge
Raspberrypi
Categories
:
树莓派
HomeBridge
Previous
:
BLE--CC2541的入门之特征值
Next
:
树莓派设置
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
1
sudo npm install -g --unsafe-perm homebridge homebridge-config-ui-x
1
sudo hb-service install --user homebridge
1
sudo nano /etc/modules
bcm2835-v4l2
到文件,保存退出1
sudo reboot
1
sudo apt install ffmpeg
rpi-camera
选择Homebridge Camera Rpi
安装
config.json
,在platforms里添加以下内容
1
2
3
4
{
"platform": "rpi-camera",
"cameras": [{"name": "Pi Camera"}]
}
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"
}
]
}
]
}
1
sudo npm install -g miio
1
miio discover
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"
}
}
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;
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));
}
};
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/