简体   繁体   中英

Is there a way for a Raspberry Pi to read data being sent from the Arduino with I2C?

I am trying to read data from an Arduino to Raspberry Pi with the Python smbus module. This data that I am trying to get the Pi to read is just numbers, such as 200, 100 490, etc. basically similar to status codes to report its status to the Pi. However, I could not find the smbus documentation online and only found one piece of the puzzle, being that bus.read_byte only reads a byte from the received data from the Arduino.

Because of this, the current code can only display a 0 (or some mystery boxes) whenever run:

The writing in blue is the data sent to the Arduino, and the boxes with the 0 are the received data

This the main code for full understanding of what's actually going on:

import time
from smbus import SMBus

clientAddr = 0x08
bus = SMBus(1)

def i2cWrite(msg): # Main Issue
  for c in msg:
    bus.write_byte(clientAddr, ord(c))
  return -1

def main():
  print("Send message to Arduino") 
  while True:
    print(bus.read_byte(clientAddr)) # Part that deals with printing messages from arduino on to python terminal
    msg = input("> ")
    print("...")
    i2cWrite(msg)
    

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print("Program Terminated Manually")

The information that I am trying to send from my Arduino is a output from a potentiometer, it runs normally and outputs its current value, just that it does not appear on the Pi side.

Arduino Code Main Parts:

// Function that executes whenever data is received from master
void receiveEvent(int howMany) {
  while (Wire.available()) { // loop through all but the last
    // receive byte as a character, char converts unicode to character and add these characters into string
    received_str += (char)Wire.read();
  }
}

// Function that sends data to Master (Pi)
void sendData(int msg)
{
  Wire.write(msg);
}

void loop() {
  delay(100);
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  sendData(sensorValue);
  int n = received_str.toInt();
  //Serial.print(n);
  analogWrite(ENA, 255); //for H bridge PWM
  if (n > 0) {
    Serial.print(received_str);
    Serial.println();
    received_str = "";
  } else {
    received_str = "";
  }
}

Serial Monitor Output: Serial Monitor of Potentiometer actually working

So to my understanding, there is no issue with the Arduino side, but it just does not want to work as needed when trying to receive data from the Arduino to the Pi. Am I supposed to use something other than read_byte ? If so, which?

EDIT What actually caused the boxes to appear was this section of the code:

def i2cRead():
    data = ""
    for i in range(0, 5):
            data += chr(bus.read_byte(clientAddr));
    print(data)
    time.sleep(0.5);

This was just added to see if it would fix the error, but unfortunately, it did not

Figured this issue, turns out the Arduino code needed an onRequest(event) in the setup section to basically register a function to be called whenever the master would request data.

The master code on python was also worked on, successfully printing data from the arduino into the Pi

def i2cRead():
    data = bus.read_byte(clientAddr)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM