-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
73 lines (61 loc) · 1.96 KB
/
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var sensor = require('node-dht-sensor');
const DHT11 = 11;
const DHT22 = 22;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-dht-rpi", "dht-rpi", Accessory);
};
/**
* The DHT accessory.
*/
class Accessory {
/**
* Create a new accessory instance.
*
* @param {Homebridge.Logger} log
* @param {Object} config
*/
constructor(log, config) {
log.debug("constructor()", config);
this.log = log;
this.config = config;
this.name = config.name;
this.type = config.type === "DHT22" ? DHT22 : DHT11;
this.pin = config.pin || 4;
log.debug(`DHT${this.type} configured on pin ${this.pin}`);
}
identify(callback) {
this.log.debug("identify()");
callback();
}
getServices() {
this.log.debug("getServices()");
const info = new Service.AccessoryInformation();
info
.setCharacteristic(Characteristic.Manufacturer, "🤪")
.setCharacteristic(Characteristic.Model, "💥")
.setCharacteristic(Characteristic.SerialNumber, "🤷♂️")
.setCharacteristic(Characteristic.FirmwareRevision, require("./package.json").version);
this.temperature = new Service.TemperatureSensor(this.name);
this.humidity = new Service.HumiditySensor(this.name);
this.update();
setInterval(() => this.update(), 30000);
return [info, this.temperature, this.humidity];
}
update() {
sensor.read(this.type, this.pin, (error, temperatureValue, humidityValue) => {
if (error) {
this.log.warn("error!", error);
} else {
this.log.debug("got values", temperatureValue, humidityValue);
this.temperature
.getCharacteristic(Characteristic.CurrentTemperature)
.updateValue(temperatureValue);
this.humidity
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.updateValue(humidityValue);
}
});
}
}