RPI3 and Arduino. Error when calling a function to print a received data via I2C

Hello guys,

I’m getting the error “[Errno 121] Remote I/O error” when I try to access a received data from Raspberry PI 3 via I2C.

Here is my raspberry pi code (in Python):

from flask import Flask
import smbus
import time
import sys

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

app = Flask('Robotica')

@app.route("/")
def index():
    return "use /request/<query> to proccess a query"

@app.route("/request/<query>")
def request(query):
    sendQuery(query)
    return "Received query: " + query

def sendQuery(query):
    data = query.split('|')
    for i in data:
        bus.write_byte(address, int(i))
        time.sleep(0.050)
    return

try:
    app.run(host='192.168.15.11', threaded=True)
except KeyboardInterrupt:
    print("Server closed!")

Here is my Arduino code:

#include <Wire.h>
 
#define SLAVE_ADDRESS 0x04

int number = 0;
int data[700];
int pos = 0;
 
void setup() {
  Serial.begin(9600);
  Wire.begin(SLAVE_ADDRESS);
  Wire.onReceive(receiveData);
  Wire.onRequest(sendData);
}
 
void loop() {
  delay(50);
}

void printData(){
  for (int i = 0; i < pos; i++){;
    Serial.print(data[i] + " ");
  }
}

void receiveData(int byteCount) {

  while (Wire.available()) {
    data[pos] = Wire.read();
    if (data[pos] != 255){
      Serial.println(data[pos]);
      pos++;
    }
    else{
      Serial.println("Trying to print the vector...");
      printData();
      pos = 0;
    }
  }
}
 
void sendData() {
  Wire.write(number);
}

When I send the data for the first time the Arduino prints the numbers in the Serial.println inside the if but not print on else.
When I send another data for the second time, I got the error :frowning:

The input data that I’m using for test is: “10|20|30|40|50|60|1|255” without quotes.

What is happening?

I'm getting the error "[Errno 121] Remote I/O error" when I try to access a received data from Raspberry PI 3 via I2C.

On the Pi? On the back side of the moon?

void loop() {
  delay(50);
}

Stupid. You are NOT charged for the number of time that loop() iterates. It is pointless to put a call to delay() in loop().

You should NOT be doing Serial printing in the ISR. Set a flag showing that data needs to be printed, and make loop() do the printing.

    Serial.print(data[i] + " ");

Grab your calculator. What do you get when you ADD a value, say 4354 and a string?

void receiveData(int byteCount) {

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

You KNOW how many bytes to read. Sticking around reading all the data, whether that is too much, or too little, is wrong.

Storing data in the array without confirming that there is room in the array is wrong.