1

I'm trying to read data continuously from a sensor using node.js. Assuming my sensor is connected to digital pin 4 of Arduino, in the node side of the code how do I program it?

I did try this: Node.js code:

var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
  this.pinMode(4, five.Pin.INPUT);
  this.loop(1,function(){
    this.digitalRead(4, function(value) {
      console.log(value);
    });
  });
});

This is the error that I got:

(node) warning: possible EventEmitter memory leak detected. 11 digital-read-4 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
    at addListener (events.js:239:17)
    at Board.digitalRead (C:\Users\Rahul\Desktop\NodeServoTest\node_modules\johnny-five\node_modules\firmata\lib\firmata.js:827:8)
    at Board.(anonymous function) [as digitalRead] (C:\Users\Rahul\Desktop\NodeServoTest\node_modules\johnny-five\lib\board.js:495:21)
    at Board. (C:\Users\Rahul\Desktop\NodeServoTest\digitalRead.js:12:8)
    at wrapper [as _onTimeout] (timers.js:275:19)
    at Timer.listOnTimeout (timers.js:92:15)
mscdex
  • 104,356
  • 15
  • 192
  • 153
Rakshith G B
  • 816
  • 2
  • 8
  • 24

1 Answers1

1

You should use something like this:

var five = require('johnny-five');
var board = five.Board();

board.on('ready', () => {
  var pin = new five.Pin('A8');
  pin.on('data', data => {
    console.log(data);
  })
});

In this case I am reading all data on analog pin 8.

Henri Cavalcante
  • 455
  • 5
  • 16