forked from iainfarq/homebridge-bme280
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.js
160 lines (134 loc) · 5.9 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
'use strict';
const bme280 = require('bme280');
var debug = require('debug')('BME280');
var logger = require("mcuiot-logger").logger;
const moment = require('moment');
var os = require("os");
var hostname = os.hostname();
const delay = millis => new Promise(resolve => setTimeout(resolve, millis));
const fixed2 = number => (Math.round(number * 100) / 100).toFixed(2);
const round1 = number => Math.round(number * 10) / 10;
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('@townsen/homebridge-bme280', 'BME280', BME280Plugin);
};
class BME280Plugin {
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.refresh = config['refresh'] || 30; // Update every 30 seconds
this.options = config.options || {};
if (!("forcedMode" in config.options)) this.options.forcedMode = true; // default to forcedMode
this.spreadsheetId = config['spreadsheetId'];
if (this.spreadsheetId) {
this.log_event_counter = 59;
this.logger = new logger(this.spreadsheetId);
}
let servicename = `${hostname}-${this.name}`;
if (config['recordpath']) {
this.recording = {
size: 14400, // default is 4032
storage: 'fs',
path: config['recordpath']
}
debug(`Recording to file: ${config['recordpath']}/${servicename}`);
}
else {
this.recording = {};
debug("Not Recording");
}
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);
if ('humidityOversampling' in this.options) this.options.humidityOversampling = eval("bme280."+this.options.humidityOversampling);
if ('pressureOversampling' in this.options) this.options.pressureOversampling = eval("bme280."+this.options.pressureOversampling);
if ('temperatureOversampling' in this.options) this.options.temperatureOversampling = eval("bme280."+this.options.temperatureOversampling);
if ('filterCoefficient' in this.options) this.options.filterCoefficient = eval("bme280."+this.options.filterCoefficient);
if ('standby' in this.options) this.options.standby = eval("bme280."+this.options.standby);
this.log(`BME280 sensor options: ${JSON.stringify(this.options)}`);
bme280.open(this.options)
.then((sensor) => {
this.log(`BME280 initialization succeeded`);
this.sensor = sensor;
this.init = true;
this.devicePolling.bind(this);
})
.catch(err => this.log(`BME280 initialization failed: ${err} `));
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, "Bosch")
.setCharacteristic(Characteristic.Model, "RPI-BME280")
.setCharacteristic(Characteristic.SerialNumber, servicename)
.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);
setInterval(this.devicePolling.bind(this), this.refresh * 1000);
this.temperatureService.log = this.log;
this.loggingService = new FakeGatoHistoryService("weather", this.temperatureService, this.recording);
}
async forcedRead() {
await this.sensor.triggerForcedMeasurement();
await delay( 2* this.sensor.maximumMeasurementTime());
}
devicePolling() {
if (this.sensor) {
if (this.options.forcedMode) {
this.forcedRead();
}
this.sensor.read()
.then(data => {
this.log(`${fixed2(data.temperature)}°C, ` +
`${fixed2(data.pressure)} hPa, ` +
`${fixed2(data.humidity)}%`);
this.loggingService.addEntry({
time: moment().unix(),
temp: round1(data.temperature),
pressure: round1(data.pressure),
humidity: round1(data.humidity)
});
if (this.spreadsheetId) {
this.log_event_counter = this.log_event_counter + 1;
if (this.log_event_counter > 59) {
this.logger.storeBME(this.name, 0, round1(data.temperature), round1(data.humidity), round1(data.pressure));
this.log_event_counter = 0;
}
}
this.temperatureService
.setCharacteristic(Characteristic.CurrentTemperature, round1(data.temperature));
this.temperatureService
.setCharacteristic(CustomCharacteristic.AtmosphericPressureLevel, round1(data.pressure));
this.humidityService
.setCharacteristic(Characteristic.CurrentRelativeHumidity, round1(data.humidity));
})
.catch(err => {
this.log(`BME read error: ${err}`);
debug(err.stack);
if (this.spreadsheetId) {
this.logger.storeBME(this.name, 1, -999, -999, -999);
}
});
} else {
this.log("Error: BME280 Not Initalized");
}
}
getServices() {
return [this.informationService, this.temperatureService, this.humidityService, this.loggingService]
}
}