1

I need to send 4 integers at a time from the Raspberry Pi to the Arduino. At the moment the Arduino doesn't request or send data, but it might need to later. My code sort of works but it crashes after about 5 arrays have been sent.

Raspberry Pi Code (Python)

import smbus
import time

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

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


while True:
    try:   
        writeNumber(12,42,-5,0)
        time.sleep(1)                    #delay one second

    except KeyboardInterrupt:
        quit()

Arduino Code

#include <Wire.h>

int data [4];
int x = 0;

void setup() {                                 

Serial.begin(9600);                        
Wire.begin(0x04);                          
Wire.onReceive(receiveData);               //callback for i2c. Jump to void recieveData() function when pi sends data

}

void loop () {

    delay(100);                            //Delay 0.1 seconds. Something for the arduino to do when it is not inside the reciveData() function. This also might be to prevent data collisions.

}

void receiveData(int byteCount) { 

   while(Wire.available()) {               //Wire.available() returns the number of bytes available for retrieval with Wire.read(). Or it returns TRUE for values >0.
       data[x]=Wire.read();
       x++;
     }
   }

     Serial.println("----");
     Serial.print(data[0]);
     Serial.print("\t");
     Serial.print(data[1]);
     Serial.print("\t");
     Serial.print(data[2]);
     Serial.print("\t");
     Serial.println(data[3]);
     Serial.print("----");

}

It will work for about 5 arrays, i.e it will send a,b,c,d, then one second later it will send it again, then a second later again, for 5 times, and then it crashes and the LXTerminal produces the error:

Traceback (most recent call last):
File "PS3_ctrl_v2.py", line 44, in <module>
writeNumber(12,42,-5,0)
File "PS3_ctrl_v2.py", line 11, in writeNumber
bus.write_i2c_block_data(address, a, [b, c, d])
IOError: [Errno 5] Input/output error 

What am I doing wrong and how can I make my code more robust?

2
  • 3
    Could it be that x keeps on increasing, so that when it is larger than 4, you start overwriting some other things? Try sending different numbers in every loop, in this way you can see if you are receiving new data or not. Now you are just sending the same numbers every time ... Commented Nov 18, 2014 at 14:55
  • Yep. You got it. I've added: if(x==4) { x=0;} and the problem seems to be fixed. Thanks for your comment. Commented Nov 18, 2014 at 15:37

1 Answer 1

1

Just change address to somthing bigger. First addresses are reserved (see this article). I used your code with address 0x20 and it works fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.