From 22271d9d8c866db61e11ff5a11731b7ef817bd17 Mon Sep 17 00:00:00 2001 From: Dror T Date: Mon, 20 Jan 2020 23:58:04 +0200 Subject: [PATCH] temperature: Support htu21d controller node lib/temperature '{ "controller": "htu21d" }' Change-Id: Ifadc2e9d3199d260b48e41a661762bb672ce9cd3 Origin: https://github.com/dror3go/generic-sensors-lite/commit/6727023aa6420b645451780039b326f6d17b8521.patch Author: Dror T Relate-to: https://github.com/rzr/generic-sensors-lite/issues/14 --- lib/temperature/index.js | 82 +++++++++++++++++++++++++++------------- 1 file changed, 56 insertions(+), 26 deletions(-) diff --git a/lib/temperature/index.js b/lib/temperature/index.js index 98d7cfe..ce0ed24 100644 --- a/lib/temperature/index.js +++ b/lib/temperature/index.js @@ -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); } @@ -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); } @@ -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); } @@ -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); }