Skip to content

Simple SHT31 sensor library using Nodejs and i2c-bus

License

Notifications You must be signed in to change notification settings

aphotix/raspi-node-sht31

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

const SHT31 = require('raspi-node-sht31');

const sht31 = new SHT31();

// 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.
sht31.readSensorData().then(console.log, console.log).finally( () => {
  setTimeout(function(){
    sht31.getStatus().then(console.log, console.log);
  }, 3000)
  sht31.enableHeater(10000).then( () => sht31.getStatus() ).then(console.log, console.log);
});

A More Typical Usage

// Include the library
const SHT31 = require('raspi-node-sht31');
// Instantiate the class
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))

// Read temperature and display in console in F with Relative humidity
sht31.readSensorData().then((data) => {
  // I love arrow notation functions inside of promises.

  // Temp in Fahrenheit -- If you get floating point rouding errors, multiply by ten before rouding, divide by 10 after.
  const temp = Math.round(data.temperature * 1.8 + 32);
  const humidity = Math.round(data.humidity);

  console.log(`The temperature is: ${temp} degress F\nThe Humidity is: ${humidity}%`); // Template strings are great.

}).catch(console.log);

Notes

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