forked from NorthernMan54/homebridge-dht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (129 loc) · 5.2 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Homebridge plugin to reading DHT22 Sensor on a Raspberry PI. Assumes DHT22
// is connected to GPIO 4 by default.
// Uses pigpio library to access gpio pin, and a custom program dht22 read the sensor.
//"accessories": [{
// "accessory": "Dht",
// "name": "cputemp",
// "service": "Temperature"
//}, {
// "accessory": "Dht",
// "name": "Temp/Humidity Sensor",
// "service": "dht22"
//}]
//
// or Multiple
//
//"accessories": [{
// "accessory": "Dht",
// "name": "cputemp",
// "service": "Temperature"
//}, {
// "accessory": "Dht",
// "name": "Temp/Humidity Sensor - Indoor",
// "service": "dht22",
// "gpio": "4"
//}, {
// "accessory": "Dht",
// "name": "Temp/Humidity Sensor - Outdoor",
// "service": "dht22",
// "gpio": "5"
//}]
var Service, Characteristic;
var exec = require('child_process').execFile;
var cputemp, dhtExec;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-dht", "Dht", DhtAccessory);
}
function DhtAccessory(log, config) {
this.log = log;
this.log("Adding Accessory");
this.config = config;
this.name = config.name;
this.name_temperature = config.name_temperature || config.name;
this.name_humidity = config.name_humidity || config.name;
this.service = config.service || "dht22";
this.gpio = config.gpio || "4";
dhtExec = config.dhtExec || "dht22";
cputemp = config.cputemp || "cputemp";
}
DhtAccessory.prototype = {
getDHTTemperature: function(callback) {
exec(dhtExec, ['-g', this.gpio], function(error, responseBody, stderr) {
if (error !== null) {
this.log('dhtExec function failed: ' + error);
callback(error);
} else {
// dht22 output format - gives a 3 in the first column when it has troubles
// 0 24.8 C 50.3 %
var result = responseBody.toString().split(/[ \t]+/);
var temperature = parseFloat(result[1]);
var humidity = parseFloat(result[3]);
// this.humidity = humidity;
this.log("Got status of %s", result[0]);
this.log("Got Temperature of %s", temperature);
this.log("Got humidity of %s", humidity);
if (parseInt(result[0]) !== 0) {
this.log.error("Error: dht22 read failed with status %s", result[0]);
callback(new Error("dht22 read failed"));
} else {
// this.service.setCharacteristic(Characteristic.CurrentRelativeHumidity, humidity);
this.humidityService
.setCharacteristic(Characteristic.CurrentRelativeHumidity, humidity);
callback(null, temperature);
}
}
}.bind(this));
},
getTemperature: function(callback) {
exec(cputemp, function(error, responseBody, stderr) {
if (error !== null) {
this.log('cputemp function failed: ' + error);
callback(error);
} else {
var binaryState = parseFloat(responseBody);
this.log("Got Temperature of %s", binaryState);
callback(null, binaryState);
}
}.bind(this));
},
identify: function(callback) {
this.log("Identify requested!");
callback(); // success
},
getServices: function() {
this.log("INIT: %s", this.name);
// you can OPTIONALLY create an information service if you wish to override
// the default values for things like serial number, model, etc.
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "AOSONG")
.setCharacteristic(Characteristic.Model, this.service)
.setCharacteristic(Characteristic.SerialNumber, this.device);
switch (this.service) {
case "Temperature":
this.temperatureService = new Service.TemperatureSensor(this.name);
this.temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({
minValue: -100,
maxValue: 100
})
.on('get', this.getTemperature.bind(this));
return [informationService, this.temperatureService];
case "dht22":
this.service = new Service.TemperatureSensor(this.name_temperature);
this.service
.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({
minValue: -100,
maxValue: 100
})
.on('get', this.getDHTTemperature.bind(this));
//this.service.addCharacteristic(Characteristic.CurrentRelativeHumidity);
this.humidityService = new Service.HumiditySensor(this.name_humidity);
return [informationService, this.service,this.humidityService];
}
}
};