Simple SHT31 sensor library using Nodejs and i2c-bus
Read temperature and humidity from the SHT31 sensor on the Raspberry Pi using i2c through the i2c-bus package.
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);
});
// 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);
Issuing multiple commands before while one is unresolved will result in an error.