Skip to content

Commit

Permalink
temperature: Support htu21d controller
Browse files Browse the repository at this point in the history
   node lib/temperature '{ "controller": "htu21d" }'

Change-Id: Ifadc2e9d3199d260b48e41a661762bb672ce9cd3
Origin: https://github.com/dror3go/generic-sensors-lite/commit/6727023aa6420b645451780039b326f6d17b8521.patch
Author: Dror T <[email protected]>
Relate-to: #14
  • Loading branch information
dror3go authored and rzr committed Mar 3, 2020
1 parent fe141cf commit 22271d9
Showing 1 changed file with 56 additions and 26 deletions.
82 changes: 56 additions & 26 deletions lib/temperature/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

var console = require('console');
var BMP085 = null;
var HTU21D = null;

try {
BMP085 = require('@abandonware/bmp085-sensor');
HTU21D = require('htu21d-i2c');
} catch (err){
// console.log(err);
}
Expand Down Expand Up @@ -59,20 +61,37 @@ TemperatureSensor.prototype.update = function update() {
var self = this;
try {
self.hasReading = false;
self.sensor.read(function (err, data) {
if (err) {
return self.onerror(err);
}
if ((data === null) || (typeof data === 'undefined')) {
return self.onerror("Invalid data");
} else {
self.timestamp = new Date();
self.celsius = Number(data.temperature);
self.hasReading = true;
self.onreading();
self.hasReading = false;
}
});
if (typeof self.sensor.readTemperature === 'function') {
self.sensor.readTemperature(function (err, data) {
if (err) {
return self.onerror(err);
}
if ((data === null) || (typeof data === 'undefined')) {
return self.onerror("Invalid data");
} else {
self.timestamp = new Date();
self.celsius = data;
self.hasReading = true;
self.onreading();
self.hasReading = false;
}
});
} else {
self.sensor.read(function (err, data) {
if (err) {
return self.onerror(err);
}
if ((data === null) || (typeof data === 'undefined')) {
return self.onerror("Invalid data");
} else {
self.timestamp = new Date();
self.celsius = Number(data.temperature);
self.hasReading = true;
self.onreading();
self.hasReading = false;
}
});
}
} catch (err) {
self.onerror(err);
}
Expand All @@ -96,6 +115,11 @@ TemperatureSensor.prototype.start = function start() {
(this.options.controller === 'bmp180')
) {
this.sensor = new BMP085(this.options.sensor);
} else if ((this.options.controller === 'htu21d') ||
(this.options.controller === 'node-htu21d') ||
(this.options.controller === 'htu21d-i2c')
) {
this.sensor = new HTU21D(this.options.sensor);
} else {
throw new Error("TODO: unsupported controller:" + this.options.controller);
}
Expand All @@ -107,18 +131,24 @@ TemperatureSensor.prototype.start = function start() {
}

try {
self.sensor.calibrate(function (err /* , data*/) {
if (err) {
throw err;
}
if (!self.interval) {
self.interval = setInterval(function() { self.update(); },
1000. / self.options.frequency);
self.onactivate();
self.state = 'activated';
}
});

if (typeof self.sensor.calibrate !== 'function') {
self.interval = setInterval(function() { self.update(); },
1000. / self.options.frequency);
self.onactivate();
self.state = 'activated';
} else {
self.sensor.calibrate(function (err /* , data*/) {
if (err) {
throw err;
}
if (!self.interval) {
self.interval = setInterval(function() { self.update(); },
1000. / self.options.frequency);
self.onactivate();
self.state = 'activated';
}
});
}
} catch(err) {
self.onerror(err);
}
Expand Down

0 comments on commit 22271d9

Please sign in to comment.