Hi all, I’m new here and a Python newbie also.
My first Python script attempt to control communication between an RPi4 and an Arduino Nano.
import time, serial, subprocess, signal, sys
from picamera2 import Picamera2, Preview
from picamera2.controls import Controls
def sigint_handler(signal, frame):
print('Interrupted')
sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
ser = serial.Serial("/dev/ttyS0",19200)
ser.flush() # Get rid of garbage/incomplete data
picam2 = Picamera2()
config = picam2.create_preview_configuration()
picam2.configure(config)
picam2.start()
time.sleep(1)
picam2.stop_preview()
print("\nWaiting for Arduino commands")
while True:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
linelist = line.split(",")
item0 = int(linelist[0])
print(item0)
if (item0 == 1):
# subprocess.run(["libcamera-hello"])
picam2.start_preview(Preview.QTGL, x=10, y=200, width=800, height=600)
elif (item0 == 2):
picam2.stop_preview()
When I send ‘1,0,0,’, the start_preview command is correctly executed but the ‘2,0,0,’ condition is never catched. Sorry but I’m really new to Python …
Thanks in advance
Gigi