Vuzi / raspi-sensors

Deprecated - Nodejs C++ plugin, allowing to easily read data from raspberryPi's sensors.
https://www.npmjs.com/package/raspi-sensors
Apache License 2.0
26 stars 3 forks source link

Impossible to use for custom Node-RED node #9

Closed Arsach closed 7 years ago

Arsach commented 7 years ago

Hello, I am currently trying to use the 'raspi-sensors' plugin to use a BMP180 sensor in a Node-RED project.

The installation went fine and I am able to read the sensor values using a test js program:

var RaspiSensors = require('raspi-sensors');

var BMP180 = new RaspiSensors.Sensor({
    type    : "BMP180",
    address  : 0X77
}, "BMP180");

BMP180.fetch(function(err, data) {
    if(err) {
        console.error("An error occured!");
        console.error(err.cause);
        return;
    }
    // Log the values
    console.log(data);
});

I am now trying to create a custom Node-RED node to read data from the sensor and all my previous attempts failed. Here is the code I am using for the bmp180.js Node-RED file, could you figure out what is going wrong?

var RaspiSensors = require('raspi-sensors');
module.exports = function(RED) {
    function bmp180Sensor(config) {
        RED.nodes.createNode(this,config);
        var node = this;
        this.on('input', function(msg) {
            msg.payload = 'node activated';
            var BMP180 = new RaspiSensors.Sensor({
                type    : "BMP180",
                address  : 0X77
            }, "BMP180");
            BMP180.fetch(function(err, data) {
                if(err) {
                    msg.payload = "An error occured!";
                }
                // Log the values
                msg.payload = data;
            });            node.send(msg);
        });
    }
    RED.nodes.registerType("bmp180",bmp180Sensor);
}

Thanks a lot for your help!

Vuzi commented 7 years ago

Your node.send(msg); is outside the fetch callback ?

Also, the plugin needs root rights to fetch data, so be sure to use as root.

Arsach commented 7 years ago

Hi, I didn't realize that the fetch function was asynchronous! The message was sent before the fetch function was finished (sorry my js skills are not crazy). Adding the node.send(msg); inside the fetch is a start but then a message is sent for every measure (one message for temperature, one message for pressure). I would like to send one single message with all the measures. At the end I used a timeout to wait for the fetch function to end and send all the measures in the message payload:

var RaspiSensors = require('raspi-sensors');
module.exports = function(RED) {
    function bmp180Sensor(config) {
        RED.nodes.createNode(this,config);
        var node = this;
        var BMP180 = new RaspiSensors.Sensor({
            type    : "BMP180",
            address  : 0X77
        }, "BMP180");

        this.on('input', function(msg) {
            var measures = [];
            function getResult(){
                if (measures.length > 0) {
                    msg.payload = measures;
                    node.send(msg);
                } else {
                    setTimeout(getResult, 100);
                }
            }
            BMP180.fetch(function(err, data) {
                if(err) {
                    msg.payload = 'An error occured';
                }
                // Log the values
                measures.push (data);
            });
            getResult();
        });
    }
    RED.nodes.registerType("bmp180",bmp180Sensor);
}

Hope that it is not too dirty...