UNPKG

signalk-raspberry-pi-sht31

Version:

SignalK node server plugin that reads data from SHT31 temperature/humidity sensors on Raspberry-Pi

97 lines (80 loc) 2.14 kB
/* * Copyright 2025 John Blackwood * * Add the MIT license */ const { SHT31 } = require('sht31-sensor') module.exports = function (app) { let timer = null let plugin = {} plugin.id = 'signalk-raspberry-pi-sht31' plugin.name = 'Raspberry-Pi SHT31' plugin.description = 'SHT31 temperature/humidity sensors on Raspberry-Pi' plugin.schema = { type: 'object', properties: { rate: { title: "Sample Rate (in seconds)", type: 'number', default: 60 }, path: { type: 'string', title: 'SignalK Path', description: 'This is used to build the path in Signal K. It will be appended to \'environment\'', default: 'inside.salon' }, } } plugin.start = function (options) { function createDeltaMessage (temperature, humidity) { var values = [ { 'path': 'environment.' + options.path + '.temperature', 'value': temperature }, { 'path': 'environment.' + options.path + '.humidity', 'value': humidity } ]; return { 'context': 'vessels.' + app.selfId, 'updates': [ { 'source': { 'label': plugin.id }, 'timestamp': (new Date()).toISOString(), 'values': values } ] } } const sht31 = new SHT31() // Read SHT31 sensor data function readSensorData() { sht31.readSensorData() .then((data) => { // temperature_C and humidity are returned by default. temperature = data.temperature_C + 273.15; humidity = data.humidity; // create message var delta = createDeltaMessage(temperature, humidity) // send temperature app.handleMessage(plugin.id, delta) }) .catch((err) => { console.log(`SHT31 read error: ${err}`); }); } readSensorData(); timer = setInterval(readSensorData, options.rate * 1000); } plugin.stop = function () { if(timer){ clearInterval(timer); timeout = null; } } return plugin }