Problem with sending data to arduino

Hello everyone,

I am trying to run an inferencing project where a raspberry pi sends makes a prediction, and sends that prediction to an arduino. The inferencing part is working well, it is the part where it sends data to arduino.

Below is the code for the raspberry pi side:

mport os, sys, time
import cv2
from picamera2 import Picamera2
from edge_impulse_linux.image import ImageImpulseRunner
import RPi.GPIO as GPIO
import serial

filename = “PersonalProject.eim”
reswidth = 96
resheight = 96
rotation = 0
camformat = “RGB888”
LEDPIN = 2

GPIO.setmode(GPIO.BCM)
GPIO.setup(LEDPIN, GPIO.OUT)

dir_path = os.path.dirname(os.path.realpath(file))
model_path = os.path.join(dir_path, filename)

runner = ImageImpulseRunner(model_path)

try:
model_info = runner.init()
print(“Model Name:”, model_info[‘project’][‘name’])
print("Model Owner: ", model_info[‘project’][‘owner’])
except Exception as e:
print(“Error: Could not initialise model”)
print(“Exception:”, e)
if (runner):
runner.stop()
sys.exit(1)

fps = 0

Set up serial connection

ser = serial.Serial(‘/dev/ttyACM0’, 9600, timeout=1)
ser.reset_input_buffer()

with Picamera2() as camera:
config = camera.create_video_configuration(main={“size”: (reswidth, resheight), “format”: camformat})

camera.start()

while True: 
    timestamp = cv2.getTickCount()
    img = camera.capture_array()
    if rotation == 0:
        pass
    elif rotation == 90:
        img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
    elif rotation == 180:
        img = cv2.rotate(img, cv2.ROTATE_180)
    elif rotation == 270:
        img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
    else:
        print("ERROR: rotation not supported. Must be 0, 90, 180, or 270")
        break
    
    features, cropped = runner.get_features_from_image(img)
    res = None

    try: 
        res = runner.classify(features)
    except Exception as e: 
        print("Could not perform inference")
        print("Exception:", e)
        break
        
    print("-----")
    results = res['result']['classification']
    for label in results:
        prob = results[label]
        print(label + ": " + str(round(prob, 3)))
    print("FPS: " + str(round(fps, 3)))
    
    maxlabel = max(results, key=results.get)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    
    towrite = str(maxlabel)
    print("Data sent to arduino: ", towrite)
    ser.write(towrite.encode('utf-8')+ b'\n')
    time.sleep(0.1)  # Add a small delay
    
    cv2.putText(img, maxlabel, (0,12), cv2.FONT_HERSHEY_PLAIN, 1, (255,255,255))
        
    cv2.imshow("Frame", img)
    frame_time = (cv2.getTickCount() - timestamp) / cv2.getTickFrequency()
    fps  =  1 / frame_time
    
    if cv2.waitKey(1) == ord('q'): 
        break

cv2.destroyAllWindows()

and below is the code from the arduino side:
void setup() {
Serial.begin(9600);
}

void loop() {
if (Serial.available() > 0) {
String data = Serial.readStringUntil(‘\n’);
Serial.print("Echo: ");
Serial.println(data);
}
}

Im getting no output in the arduino serial monitor.

Hi @someguy,

It looks like you are having trouble with your serial connection, not so much the machine learning side, so I don’t know how much help we will be here. I might recommend posting on the Raspberry Pi forum or the Arduino forum, as the folks there will be more knowledgeable about such topics.

To get you started, I might recommend the following:

  • Check your TX/RX wiring. It’s super easy to cross these.
  • Get a Serial-to-USB board/cable and check to see if you can successfully send and receive characters (on both the Arduino and Raspberry Pi)
  • Once you verify the above, try running parts of your program to verify that it’s doing what you intend.