Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to read bytes from I2C arduino #151

Open
miguel719 opened this issue Feb 10, 2017 · 2 comments
Open

Unable to read bytes from I2C arduino #151

miguel719 opened this issue Feb 10, 2017 · 2 comments

Comments

@miguel719
Copy link

I'm trying to read bytes from an slave arduino. But i'm unable to get the correct bytes values.

The slave arduino code:

#include <Wire.h> 

void setup() {
  Wire.begin(8);
  Wire.onRequest(requestEvent);
}

void loop() {
  delay(500); 
}

void requestEvent() {
  uint8_t buffer[4];
  buffer[0] = 12;
  buffer[1] = 23;
  buffer[2] = 39;
  buffer[3] = 78;
  Wire.write(buffer, 4);
}

The Firmata code:


let firmata = require("firmata");

board = new firmata('COM3', {samplingInterval: 1000});
board.on("ready", function() {
    this.i2cConfig();
    this.i2cRead(8,4, function(data) {
      console.log("received data");
      console.log(data);
    });
});

The response i get is:

received data
[189,255,255,255]
@soundanalogous
Copy link
Member

I'm assuming you have an Arduino running StandardFirmata (or one of the variants) connected to a computer running firmata.js and then a second Arduino acting as an I2C slave connected the Arduino running StandardFirmata, right?

The first thing I suspect is that 500ms delay is messing things up so remove that from your Arduino code since it's blocking and unnecessary.

@rwaldron
Copy link
Collaborator

Using exactly the arduino slave code you provided, I created the following program:

var Board = require("firmata");

Board.requestPort(function(error, port) {
  if (error) {
    console.log(error);
    return;
  }

  var board = new Board(port.comName); // no change to sampling interval

  board.on("ready", function() {
    this.i2cConfig();
    this.i2cRead(8,4, function(data) {
      console.log("received data");
      console.log(data);
    });
  });
});

Which produces the following results:

$ node examples/i2c-slave-request.js
received data
[ 12, 23, 39, 78 ]
received data
[ 12, 23, 39, 78 ]
received data
[ 12, 23, 39, 78 ]
received data
[ 12, 23, 39, 78 ]
received data
[ 12, 23, 39, 78 ]
received data
[ 12, 23, 39, 78 ]
received data
[ 12, 23, 39, 78 ]
received data
[ 12, 23, 39, 78 ]
received data
[ 12, 23, 39, 78 ]
received data
[ 12, 23, 39, 78 ]
received data
[ 12, 23, 39, 78 ]
received data
[ 12, 23, 39, 78 ]
received data
[ 12, 23, 39, 78 ]
received data
[ 12, 23, 39, 78 ]
received data
[ 12, 23, 39, 78 ]
received data
[ 12, 23, 39, 78 ]
received data

When I added {samplingInterval: 1000}, the results are the same:

$ node examples/i2c-slave-request.js
received data
[ 12, 23, 39, 78 ]
received data
[ 12, 23, 39, 78 ]
received data
[ 12, 23, 39, 78 ]
received data
[ 12, 23, 39, 78 ]
received data
[ 12, 23, 39, 78 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants