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;
}
Serial.printlnbecause it could just be the Arduino can't keep up since it's serial-ing while the host is still blasting I2C data. (Only do one change at a time, obviously).