Problem of using raspberry Pi to read multi bytes from arduino via i2c


dear all,

i connect raspberry pi (b) arduino via i2c.
the problem result read python via i2c different print via serial port.
from i2c, got:
[9, 0, 255, 255, 255, 255, 255, 255, 255]

print serial port, got:
11 2 3 4

i have try set digital ouput  , read analog input of arduino, either python 2 + smbus or python3 + quick2wire similar result:
------------------------------------------------------------------------------
[9, 0, 255, 255, 255, 255, 255, 255, 255] python 2 smbus
and

b'\t\x00\xff\xff\xff\xff\xff\xff\xff' python3 quick2wire.
------------------------------------------------------------------------------

here terminal result print via serial port of arduino:
-----------------------------------------
ready!
data received: 0
sensors  send: 11 2 3 4 count: 9
-----------------------------------------

the following th arduino code:
code: [select]

#include <wire.h>

#define slave_address 0x04

int number = 0;
int state = 0;
double temp;

int index = 0;

const char pinwritings[] = {2, 3, 4, 5, 6, 7, 8, 9};
int cmd=0;

const char pinreadings[] = {a0, a1, a2, a3};
typedef struct {
  byte address;
  int values[4];
} data;
data d;

void setup() {
  int i;
  //initialize pins relays
  (int = 0; < sizeof(pinwritings); i++)
  {
    pinmode(pinwritings[i], output);
  }
 
  //d.address = slave_address;
  d.address = byte(0x31);
  //initialize pins relays
  (int = 0; < sizeof(pinreadings); i++)
  {
    d.values[i] = 0x31 + i;
  }
 
 pinmode(led_builtin, output);
 serial.begin(9600);
 // initialize i2c slave
 wire.begin(slave_address);

 // define callbacks i2c communication
 wire.onreceive(receivedata);
 wire.onrequest(senddata);
 serial.println("ready!");
}

void loop() {
  delay(100);
  //getanalog();
  //senddata();
}

// callback received data
void receivedata(int bytecount){

 while(wire.available()) {
  number = wire.read();
  serial.print("data received: ");
  serial.println(number);
  if (number != 0)
    {
      setgpio(number);
    }
   if (number == 0)
   {
     senddata();
   }
 }
}

// callback sending data
void senddata(){
  int count;
  count = wire.write((byte *)&d, sizeof(d));
  serial.print("sensors ");
  serial.print(" send: ");
  serial.write((byte *)&d, sizeof(d));
  serial.print(" count: ");
  serial.println(count);

}

//get value of sensors a0 a3
void getanalog(void)
{
  int = 0;
  (i; < sizeof(pinreadings); i++)
  {
    d.values[i] =  0x64 + i;//analogread(pinreadings[i]);
    // apply calibration sensor reading
    //d.values[i] = map(d.values[i], 0, 1023, 0, 255);
    // in case sensor value outside range seen during calibration
    //d.values[i] = constrain(d.values[i], 0, 255); 
    serial.print("sensor ");
    serial.print(i);
    serial.print(" detected: ");
    serial.println(d.values[i]);
    delay(1000);
  }
}

void setgpio(int cmd)
{
   serial.print(" command: ");
   serial.println(cmd, bin);
  int i;
  (i = 0; < sizeof(pinwritings); = + 1)
  {
     digitalwrite(pinwritings[i],(1 << & cmd) >> i);
     serial.print("pin ");
     serial.print(i);   
     serial.print(" setted: ");
     serial.println((1 << & cmd) >> i, bin);
  }
}




and python 2 smbus follows:
code: [select]

import smbus
import time

address = 0x04

if __name__=="__main__":
bus = smbus.smbus(1)    # 0 = /dev/i2c-0 (port i2c0), 1 = /dev/i2c-1 (port i2c1)

while true:
try:
value = int(str(input("enter 0 - 255:")).strip())
except exception e:
print(e)
continue

bus.write_byte(address, value)
#bus.write_byte_data(address, 0, value)
time.sleep(1)

number = bus.read_i2c_block_data(address, 0x00, 9)
#number = bus.read_byte(address)
# number = bus.read_byte_data(address, 1)
print(number)
datas = [(number[i]<<8) + number[i+1] in range(1,9,2)] #1,3,5,7
print(datas)


and python3 + quick2wire code follows:
code: [select]

from quick2wire.i2c import i2cmaster, writing_bytes, reading
import time

address = 0x04

if __name__=="__main__":
with i2cmaster() master:
while true:
try:
value = int(str(input("enter 0 - 255:")).strip())
except exception e:
print(e)
continue
number = master.transaction(writing_bytes(address, value),
reading(address, 9))[0]
print(number)
print(number[0])
datas = [(number[i+1]<<8) + number[i] in range(1,9,2)] #1,3,5,7
print(datas)
time.sleep(1)

can clarify 2 things:

1) how connected - 3.3v or using level shifter?  you
have pull-up resistors?

2) end meant i2c master , end slave?


Arduino Forum > Using Arduino > Programming Questions > Problem of using raspberry Pi to read multi bytes from arduino via i2c


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code