UNPKG

hih6130-sensor

Version:

A Node.js I2C module for the Honeywell HumidIcon HIH6130 Humidity and Temperature Sensor

66 lines (50 loc) 1.53 kB
/* HIH6130.js Node.js I2C module for the Honeywell HumidIcon HIH6130 Humidity and Temperature Sensor */ 'use strict'; class HIH6130 { constructor(options) { const i2c = require('i2c-bus'); this.i2cBus = i2c.openSync((options && options.hasOwnProperty('i2cBusNo')) ? options.i2cBusNo : 1); this.HIH6130_ADDRESS = 0x27; this.HIH6130_CMD_SIZE = 0x04; this.HIH6130_CMD_READ = 0x04; } readSensorData() { return new Promise((resolve, reject) => { this.i2cBus.readI2cBlock(this.HIH6130_ADDRESS, this.HIH6130_CMD_READ, this.HIH6130_CMD_SIZE, new Buffer(4), (err, bytesRead, data) => { if(err) { return reject(err); } /* data[0] // humidity high byte data[1] // humidity low byte data[2] // temperature high byte data[3] // temperature low byte */ const status = (data[0] & 0xc0) >> 6; const humidity = (((data[0] & 0x3f) << 8) + data[1]) / 0x3fff * 100; const temperature_C = (((data[2] << 8) + data[3]) >> 2) / 0x3fff * 165 - 40; return resolve({ status : status, humidity : humidity, temperature_C : temperature_C }); }); }); } static HIH6130_STATUS_NORMAL() { return 0x00; } static HIH6130_STATUS_STALE() { return 0x01; } static HIH6130_STATUS_COMMAND_MODE() { return 0x02; } static HIH6130_STATUS_DIAGNOSTIC() { return 0x03; } } module.exports = HIH6130;