Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input/output error using Python module SMBus, a Raspberry Pi and an Arduino

I have connected a Raspberry Pi and Rainbowduino together with a homemade I²C level shifter, and installed the Python module SMBus, the Raspberry Pi can communicate with the Rainbowduino, but every so often I get an input/output error message when trying the command bus.write_i2c_block_data(address, signal, data).

It says:

IOError: [Errno 5] Input/output error

Why does it happen and how do I fix or ignore these errors?

like image 930
holmeswatson Avatar asked Mar 06 '13 10:03

holmeswatson


1 Answers

Long story short alot of people are plagued by this, I found a very simple work around is the following.

It will let you ignore the error and keep tx/rx-ing, calling i2cdetect seems to reinitialize the bus somehow instead of the arduino disappearing from it.

i posted an explanation of how i found this solution here (waiting mod approval right now) http://www.raspberrypi.org/phpBB3/viewtopic.php?f=41&t=52517

try:
    bus.write_i2c_block_data(address, signal, data)
except IOError:
    subprocess.call(['i2cdetect', '-y', '1'])
    flag = 1     #optional flag to signal your code to resend or something

Even though this allows the Pi to keep transmitting bad data is still being sent to the arduino. The simplest way I found to get around this was to add an extra check-sum byte to the end of my data blocks.

I added up each byte of the message inside a byte variable allowing the value to rollover, then assign the check-sum byte whatever value necessary to sum the whole message out to zero.

The arduino can then check each incoming transmission by summing all the bytes. If the message does not sum out to zero, it is ignored as an erroneous transmission.

I also assigned my messages a one byte message id which increments after each successful transmission, eliminating the possibility of accidental double sends. But that may not really be necessary.

like image 152
Jon I Avatar answered Oct 06 '22 07:10

Jon I