UNPKG

co2monitor

Version:

Node for the Dostmann CO2 Monitor mini. If there are permission errors on LIBUSB, you have to add a new file to '/etc/udev/rules.d' with the following record SUBSYSTEM=="usb", MODE="0660", GROUP="plugdev"

76 lines (62 loc) 2.04 kB
module.exports = function(RED) { function co2Monitor(config) { RED.nodes.createNode(this,config); let info = { sensorid: config.sensorid, room: config.room, desc: config.desc }; let node = this; let msg = { payload : { sensorid : info.sensorid, room : info.room, description : info.desc, tempC : null, tempF : null, co2 : null, airQuailty : null, humidity : null } }; const airCo2ntrol = require('./airCo2ntrol'); const device = new airCo2ntrol(); device.connect((err) => { if (err) { node.error("Error connect HID interface: " + err); } node.log("device connected"); device.transfer(); }); device.on('tempC', (temperature) => { if(msg.payload.tempC != temperature) { msg.payload.tempC = temperature; msg.payload.tempF = parseFloat(((temperature * 1.8) + 32).toFixed(2)); node.send([null, msg.payload.tempC, msg.payload.tempF, null, msg]); } node.status({fill:msg.payload.airQuailty,shape:"dot",text: "CO2 = " + msg.payload.co2 + "ppm / Temperature = " + msg.payload.tempC + "°C / " + msg.payload.tempF + "°F"}); }); device.on('co2', (co2) => { if(msg.payload.co2 != co2) { msg.payload.co2 = co2; msg.payload.airQuailty = _air_quality(msg.payload.co2); node.send([msg.payload.co2, null, null, null, msg]); } node.status({fill:msg.payload.airQuailty,shape:"dot",text: "CO2 = " + msg.payload.co2 + "ppm / Temperature = " + msg.payload.tempC + "°C / " + msg.payload.tempF + "°F"}); }); device.on('hum', (humidity) => { if(msg.payload.humidity != humidity) { msg.payload.humidity = humidity; node.send([null, null, null, msg.payload.humidity, msg]); } }); function _air_quality(co2Value) { if(co2Value <= 800) { return "green"; } else if(co2Value > 800 && co2Value <= 1200) { return "yellow"; } else { return "red"; } } } RED.nodes.registerType("co2Monitor",co2Monitor); }