Closed Arsach closed 8 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.
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...
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:
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?
Thanks a lot for your help!