项目作者: aphotix

项目描述 :
Simple SHT31 sensor library using Nodejs and i2c-bus
高级语言: JavaScript
项目地址: git://github.com/aphotix/raspi-node-sht31.git
创建时间: 2017-11-09T21:47:24Z
项目社区:https://github.com/aphotix/raspi-node-sht31

开源协议:MIT License

下载


SHT31 Library using i2c-bus for Node.js

Read temperature and humidity from the SHT31 sensor on the Raspberry Pi using i2c through the i2c-bus package.

Credit where due

Requirements

  • A raspberry Pi
  • Node v6 or newer (tested on 6.11.4 and 9.1.0)
  • I²C must be enabled.
  • SHT31 or SHT35 sensor Adafruit

Dependancies

Usage Example

  1. const SHT31 = require('raspi-node-sht31');
  2. const sht31 = new SHT31();
  3. // Read the temp, after it resolves turn on the sensor heater and get the status, then turn off the heater after 10 seconds and show the status.
  4. sht31.readSensorData().then(console.log, console.log).finally( () => {
  5. setTimeout(function(){
  6. sht31.getStatus().then(console.log, console.log);
  7. }, 3000)
  8. sht31.enableHeater(10000).then( () => sht31.getStatus() ).then(console.log, console.log);
  9. });

A More Typical Usage

  1. // Include the library
  2. const SHT31 = require('raspi-node-sht31');
  3. // Instantiate the class
  4. const sht31 = new SHT31(); // Paramteres unecessary when using a B+, A+, Zero, Zero W, Pi 2, or Pi 3 (basically an orignal Pi uses 0 and must be set: new SHT31(0x44, 0))
  5. // Read temperature and display in console in F with Relative humidity
  6. sht31.readSensorData().then((data) => {
  7. // I love arrow notation functions inside of promises.
  8. // Temp in Fahrenheit -- If you get floating point rouding errors, multiply by ten before rouding, divide by 10 after.
  9. const temp = Math.round(data.temperature * 1.8 + 32);
  10. const humidity = Math.round(data.humidity);
  11. console.log(`The temperature is: ${temp} degress F\nThe Humidity is: ${humidity}%`); // Template strings are great.
  12. }).catch(console.log);

Notes

Issuing multiple commands before while one is unresolved will result in an error.