rwaldron / johnny-five

JavaScript Robotics and IoT programming framework, developed at Bocoup.
http://johnny-five.io
Other
13.26k stars 1.76k forks source link

Barometer: read pressure only once #1080

Closed microspace closed 8 years ago

microspace commented 8 years ago

I launched barometer example from here http://johnny-five.io/examples/grove-barometer-edison/ How to read pressure data only ONCE, that exit program?

rwaldron commented 8 years ago

Just so I understand correctly, you want to completely exit the program after reading the barometer? What happens the read value?

microspace commented 8 years ago

Yes, I want to completely exit the program. Program should return pressure value. Is it possible?

rwaldron commented 8 years ago

Here's a complete example, including a test program to try it out:

(test.js)

var cp = require("child_process");
var barometer = cp.spawn("node", [ "read-once-barometer" ]);

barometer.stdout.on("data", function (data) {
  console.log("stdout: " + data);
});

barometer.stderr.on("data", function (data) {
  console.log("stderr: " + data);
});

barometer.on("exit", function (code) {
  console.log("child process exited with code " + code);
});

(read-once-barometer.js)

var five = require("johnny-five");
var Edison = require("edison-io");
var board = new five.Board({
  io: new Edison(),
  // this ensures that no other information is output
  repl: false,
  debug: false,
});

board.on("ready", function() {
  var barometer = new five.Barometer({
    controller: "BMP180"
  });

  barometer.on("data", function() {
    process.stdout.write(this.pressure);
    process.exit();
  });
});
microspace commented 8 years ago

Thank you so much!