I'm trying to read some data from the Arduino slave and when I receive the data it shows some numbers but not the correct ones.
I'm using node and i2c-bus module, also tried with wiringPi but got the same result.
I do the following thing in node.
const i2c = require('i2c-bus');
const bus = i2c.open(1, (err) => {
setInterval(function() {
var buf = new Buffer(4);
bus.readI2cBlockSync(0x09, 50,4, buf);
console.log(buf );
}, 500);
});
And my Arduino code is:
#include <Wire.h>
int val = 0;
int receiveBuffer[9];
int toggle = 0;
void setup() {
pinMode(3, INPUT);
pinMode(13, OUTPUT);
Wire.begin(9);
Wire.onReceive(receiveEvent);
Wire.onRequest(sendData);
}
void loop() {
}
void receiveEvent(int howmany)
{
int counter = 0;
while(Wire.available()) {
receiveBuffer[counter] = Wire.read();
counter ++;
}
}
void sendData(){
if (receiveBuffer[0] == 50) {
if (toggle == 0) {
digitalWrite(13, HIGH);
toggle=1;
} else {
digitalWrite(13, LOW);
toggle = 0;
}
Wire.write(66);
Wire.write(67);
Wire.write(68);
Wire.write(69);
}
}
And the result in the RPi is this:
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 08 bf ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
<Buffer 10 7f ff ff>
See the at some point started to receive different numbers, but other times is mixed and show random things.
I try to do the same master/slave with 2 arduinos and I received the correct data.. I don't know why is failing in RPi and is driving me crazy.
I'm using a level shifter for the 5v i2c between the Rpi and the Arduino.
Also I know the i2c is working ok because I have 4 MCP23017 connected to the 5v i2c channel and they are working.
The arduino seems to receive the data okay as the led toggles alright.
Any ideas?