di-sensors
Version:
Drivers and examples for using DI_Sensors in Node.js
91 lines (73 loc) • 3.78 kB
JavaScript
;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
// https://www.dexterindustries.com/GoPiGo/
// https://github.com/DexterInd/DI_Sensors
//
// Copyright (c) 2017 Dexter Industries
// Released under the MIT license (http://choosealicense.com/licenses/mit/).
// For more information see https://github.com/DexterInd/GoPiGo3/blob/master/LICENSE.md
var DHTDevice = require('dht-drivers');
var DHT = function () {
function DHT() {
var moduleType = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : DHT.DHT11;
var scale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DHT.SCALE_C;
_classCallCheck(this, DHT);
this.moduleType = moduleType;
this.scale = scale;
}
_createClass(DHT, [{
key: 'convertCtoF',
value: function convertCtoF(temp) {
return temp * 1.8 + 32;
}
}, {
key: 'convertFtoC',
value: function convertFtoC(temp) {
return (temp - 32) / 1.8;
}
}, {
key: 'getHeatIndex',
value: function getHeatIndex(temp, hum, scale) {
// http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
var needsConversion = typeof scale === 'undefined' || scale === DHT.SCALE_C;
temp = needsConversion ? this.convertCtoF(temp) : temp;
// Steadman's result
var heatIndex = 0.5 * (temp + 61 + (temp - 68) * 1.2 + hum * 0.094);
// regression equation of Rothfusz is appropriate
if (temp >= 80) {
var heatIndexBase = -42.379 + 2.04901523 * temp + 10.14333127 * hum + -0.22475541 * temp * hum + -0.00683783 * temp * temp + -0.05481717 * hum * hum + 0.00122874 * temp * temp * hum + 0.00085282 * temp * hum * hum + -0.00000199 * temp * temp * hum * hum;
// adjustment
if (hum < 13 && temp <= 112) {
heatIndex = heatIndexBase - (13 - hum) / 4 * Math.sqrt((17 - Math.abs(temp - 95)) / 17);
} else if (hum > 85 && temp <= 87) {
heatIndex = heatIndexBase + (hum - 85) / 10 * ((87 - temp) / 5);
} else {
heatIndex = heatIndexBase;
}
}
return needsConversion ? this.convertFtoC(heatIndex) : heatIndex;
}
}, {
key: 'read',
value: function read() {
// 2 = RapsberryPi platform ID
// 15 = Serial PIN
var data = DHTDevice.read(2, this.moduleType, 15);
var temp = +Number(parseFloat(data.temperature).toFixed(2));
var hum = +Number(parseFloat(data.humidity).toFixed(2));
if (this.scale === DHT.SCALE_F) {
temp = this.convertCtoF(temp);
}
var heatIndex = +Number(parseFloat(this.getHeatIndex(temp, hum, this.scale)).toFixed(2));
return [temp, hum, heatIndex];
}
}]);
return DHT;
}();
DHT.DHT11 = 11;
DHT.DHT22 = 22;
DHT.AM2302 = 22;
DHT.SCALE_C = 'c';
DHT.SCALE_F = 'f';
module.exports = DHT;