Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to send array of values 250,5000,400,1,430 from raspberry python but arduino receives

43
0
0
0
0

Anybody help.

my code below

What I have tried:

////Rpi code/////

import smbus
import time

bus = smbus.SMBus(1)
address = 0x20

def writeNumber(a,b,c,d,e):
bus.write_i2c_block_data(address, a, [b, c, d, e])
return -1


while True:
    try:   
        writeNumber(250,5000,400,1,430)
        time.sleep(1)   



////Arduino Code////                 
   

#include <Wire.h>

int data[5];
int x =0;


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
Wire.begin(0x20);
Wire.onReceive(receiveData);
}

void loop() {
  // put your main code here, to run repeatedly:
delay(10);
}

void receiveData(){

  while(Wire.available()){
    data[x]=Wire.read();
  }

  Serial.println("Stand-1");
  Serial.println(data[0]);
  Serial.println(data[1]);
  Serial.println(data[2]);
  Serial.println(data[3]);
  Serial.println(data[4]);
}
Posted
Updated 2-Sep-19 17:53pm
Comments
[no name] 2-Sep-19 23:47pm    
Writing and reading one number (5 times) is usually simpler, to start out with.

1 solution

while(Wire.available()){
    data[x]=Wire.read();
  }


You need to increment x (or something along those lines). Everything is getting stuffed in the first element, with the rest defaulting to int 0.

If the 43 IS supposed to be 430 then there's maybe a "size" issue there too (byte).
 
Share this answer
 
Comments
ramen79 3-Sep-19 6:33am    
Hi!
thanks.
meantime I have done it with "," separated string.

like:
byteData = ConvertStringToBytes("250,5000,400,1,430")
bus.write_i2c_block_data(byteDate)

thanks
ramen79 3-Sep-19 6:34am    
correct is>>

bus.write_i2c_block_data(i2c_address, i2c_cmd,byteDate)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900