user77055: The code below sends data to Raspberry Pi every 20 millisecond. The code works fine but sometimes python throws an error. Why this is happening? Should I give more time between writeData?
Error:
> Traceback (most recent call last):   File "rpi-I2C.py", line 33, in
> <module>
> writeData("360")   File "rpi-I2C.py", line 12, in writeData
> bus.write_i2c_block_data(address,0x00,byteValue) OSError: [Errno 121] Remote I/O error

Python code:
import smbus
import time
# for RPI version 1, use bus = smbus.SMBus(0)
bus = smbus.SMBus(1)

# This is the address we setup in the Arduino Program
address = 0x04

#http://www.raspberry-projects.com/pi/programming-in-python/i2c-programming-in-python/using-the-i2c-interface-2
def writeData(value):
    byteValue = StringToBytes(value)    
    bus.write_i2c_block_data(address,0x00,byteValue)
    #first byte is 0=command byte.. just is.
    return -1


def StringToBytes(val):
        retVal = []
        for c in val:
                retVal.append(ord(c))
        return retVal

while True:
    print("sending test")
    writeData("left")   
    time.sleep(0.02)

    #print('Sending OPEN-00-00');
    writeData("right")
    time.sleep(0.02)

    #print('Sending WIN-12-200');
    writeData("360")
    time.sleep(0.02)

Arduino code:
#include <Wire.h>
#define SLAVE_ADDRESS 0x04

volatile boolean receiveFlag = false;
char temp[32];
String command;

void setup() {
  Wire.begin(SLAVE_ADDRESS);
  // define callbacks for i2c communication    
  Wire.onReceive(receiveEvent);    
  Serial.begin(115200);    
  Serial.println("Ready!");    
}

void loop() {    
  if (receiveFlag == true) {    
    Serial.println(temp);    
    receiveFlag = false;    
  }    
}

void receiveEvent(int howMany) {    
  for (int i = 0; i < howMany; i++) {          
    temp[i] = Wire.read();    
    temp[i + 1] = '\0';  //add null after ea. char    
  }
    
  //RPi first byte is cmd byte so shift everything to the left 1 pos so temp contains our string    
  for (int i = 0; i < howMany; ++i)    
    temp[i] = temp[i + 1];    
    receiveFlag = true;    
  }


user68012: A RPI uses 3.3v logic while an arduino uses 5v logic you can use a logic converter but by now it's probly to late and maybe your arduino or RPI is broken (Sorry!)

77055: <p>The code below sends data to Raspberry Pi every 20 millisecond. The code works fine but sometimes python throws an error. Why this is happening? Should I give more time between <code>writeData</code>?</p>
<p><strong>Error:</strong></p>
<pre><code>&gt; Traceback (most recent call last):   File &quot;rpi-I2C.py&quot;, line 33, in
&gt; &lt;module&gt;
&gt; writeData(&quot;360&quot;)   File &quot;rpi-I2C.py&quot;, line 12, in writeData
&gt; bus.write_i2c_block_data(address,0x00,byteValue) OSError: [Errno 121] Remote I/O error
</code></pre>
<p><strong>Python code:</strong></p>
<pre><code>import smbus
import time
# for RPI version 1, use bus = smbus.SMBus(0)
bus = smbus.SMBus(1)

# This is the address we setup in the Arduino Program
address = 0x04

#http://www.raspberry-projects.com/pi/programming-in-python/i2c-programming-in-python/using-the-i2c-interface-2
def writeData(value):
    byteValue = StringToBytes(value)    
    bus.write_i2c_block_data(address,0x00,byteValue)
    #first byte is 0=command byte.. just is.
    return -1


def StringToBytes(val):
        retVal = []
        for c in val:
                retVal.append(ord(c))
        return retVal

while True:
    print(&quot;sending test&quot;)
    writeData(&quot;left&quot;)   
    time.sleep(0.02)

    #print('Sending OPEN-00-00');
    writeData(&quot;right&quot;)
    time.sleep(0.02)

    #print('Sending WIN-12-200');
    writeData(&quot;360&quot;)
    time.sleep(0.02)
</code></pre>
<p><strong>Arduino code</strong>:</p>
<pre><code>#include &lt;Wire.h&gt;
#define SLAVE_ADDRESS 0x04

volatile boolean receiveFlag = false;
char temp[32];
String command;

void setup() {
  Wire.begin(SLAVE_ADDRESS);
  // define callbacks for i2c communication    
  Wire.onReceive(receiveEvent);    
  Serial.begin(115200);    
  Serial.println(&quot;Ready!&quot;);    
}

void loop() {    
  if (receiveFlag == true) {    
    Serial.println(temp);    
    receiveFlag = false;    
  }    
}

void receiveEvent(int howMany) {    
  for (int i = 0; i &lt; howMany; i++) {          
    temp[i] = Wire.read();    
    temp[i + 1] = '\0';  //add null after ea. char    
  }
    
  //RPi first byte is cmd byte so shift everything to the left 1 pos so temp contains our string    
  for (int i = 0; i &lt; howMany; ++i)    
    temp[i] = temp[i + 1];    
    receiveFlag = true;    
  }
</code></pre>
 68012: <p>A RPI uses 3.3v logic while an arduino uses 5v logic you can use a logic converter but by now it's probly to late and maybe your arduino or RPI is broken (<em><strong>Sorry!</strong></em>)</p>
