-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsensor.js
164 lines (136 loc) · 5.04 KB
/
sensor.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
160
161
162
163
164
'use strict';
const { Bme680 } = require('bme680-sensor');
var debug = require('debug')('BME680');
var logger = require('mcuiot-logger').logger;
const moment = require('moment');
var os = require('os');
var hostname = os.hostname();
let Service, Characteristic;
var CustomCharacteristic;
var FakeGatoHistoryService;
module.exports = (homebridge) => {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
CustomCharacteristic = require('./lib/CustomCharacteristic.js')(homebridge);
FakeGatoHistoryService = require('fakegato-history')(homebridge);
homebridge.registerAccessory('homebridge-bme680', 'BME680', BME680Plugin);
};
class BME680Plugin {
constructor(log, config) {
this.log = log;
this.name = config.name;
this.name_temperature = config.name_temperature || this.name;
this.name_humidity = config.name_humidity || this.name;
this.name_air_quality = config.name_air_quality || this.name;
this.refresh = config['refresh'] || 60; // Update every minute
this.options = config.options || {};
this.storage = config['storage'] || 'fs';
this.init = false;
this.data = {};
if ('i2cBusNo' in this.options) {
this.options.i2cBusNo = parseInt(this.options.i2cBusNo);
}
if ('i2cAddress' in this.options) {
this.options.i2cAddress = parseInt(this.options.i2cAddress);
}
this.log(`BME680 sensor options: ${ JSON.stringify(this.options) }`);
try {
this.sensor = new Bme680(this.options.i2cBusNo, this.options.i2cAddress);
} catch(ex) {
this.log('BME680 initialization failed:', ex);
}
if (this.sensor) {
this.sensor.initialize()
.then(() => {
this.log(`BME680 initialization succeeded`);
this.init = true;
this.devicePolling.bind(this);
})
.catch(err => this.log(`BME680 initialization failed: ${ err } `));
}
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, 'bme680')
.setCharacteristic(Characteristic.Model, 'RPI-BME680')
.setCharacteristic(Characteristic.SerialNumber, hostname + '-' + hostname)
.setCharacteristic(Characteristic.FirmwareRevision, require('./package.json').version);
this.temperatureService = new Service.TemperatureSensor(this.name_temperature);
this.temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({
minValue: -100,
maxValue: 100
});
this.temperatureService
.addCharacteristic(CustomCharacteristic.AtmosphericPressureLevel);
this.humidityService = new Service.HumiditySensor(this.name_humidity);
this.airQualityService = new Service.AirQualitySensor(this.name_air_quality);
setInterval(this.devicePolling.bind(this), this.refresh * 1000);
this.temperatureService.log = this.log;
this.loggingService = new FakeGatoHistoryService('weather', this.temperatureService, {
storage: this.storage,
minutes: this.refresh * 10 / 60
});
}
devicePolling() {
debug('Polling BME680');
if (this.sensor) {
this.sensor.getSensorData()
.then(data => {
if (data.data.heat_stable) {
this.log(`data(temp) = ${ JSON.stringify(data, null, 2) }`);
this.loggingService.addEntry({
time: moment().unix(),
temp: roundInt(data.data.temperature),
pressure: roundInt(data.data.pressure),
humidity: roundInt(data.data.humidity),
airQuality: roundInt(data.data.gas_resistance)
});
this.temperatureService
.updateCharacteristic(Characteristic.CurrentTemperature, roundInt(data.data.temperature));
this.temperatureService
.updateCharacteristic(CustomCharacteristic.AtmosphericPressureLevel, roundInt(data.data.pressure));
this.humidityService
.updateCharacteristic(Characteristic.CurrentRelativeHumidity, roundInt(data.data.humidity));
const airQuality = computeIAQ(roundInt(data.data.gas_reistance), roundInt(data.data.humidity));
this.airQualityService.updateCharacteristic(Characteristic.AirQuality, airQuality);
}
})
.catch(err => {
this.log(`BME read error: ${ err }`);
debug(err.stack);
});
} else {
this.log('Error: BME680 Not Initalized');
}
}
getServices() {
return [this.informationService, this.temperatureService, this.humidityService, this.airQualityService, this.loggingService]
}
}
function roundInt(string) {
return Math.round(parseFloat(string) * 10) / 10;
}
function computeIAQ(gas, humidity) {
const humidityWeight = 0.25;
let gasOffset = 50000 - gas;
let humidityOffset = humidity - 40;
let humidityScore = 0, gasScore = 0;
let valueMapping = [5, 4, 3, 2, 1]
if (humidityOffset > 0) {
humidityScore = ( 100 - 40 - humidityOffset );
humidityScore /= ( 100 - 40 );
humidityScore *= ( humidityWeight * 100 );
} else {
humidityScore = ( 40 + humidityOffset );
humidityScore /= 40;
humidityScore *= ( humidityWeight * 100 );
}
if (gasOffset > 0) {
gasScore = ( gas / 50000 );
gasScore *= ( 100 - ( humidityWeight * 100 ) );
} else {
gasScore = 100 - ( humidityWeight * 100 );
}
return valueMapping[Math.ceil(( humidityScore + gasScore ) / 20) - 1];
}