Hi. I’m trying to establish communication between an Arduino and an RPi via GPIO serial, bidirectional.
The ‘read’ Rpi part works fine but the ‘write’ do not. Nothing is received from Arduino.
Here is the python code on RPi:
#!/usr/bin/env python3
import time, serial, subprocess, signal, sys
from picamera2 import Picamera2, Preview
from picamera2.controls import Controls
def sigint_handler(signal, frame):
print('Killed')
sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
ser = serial.Serial("/dev/ttyS0",9600)
# 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")
msg = "RPi started"
ser.write(msg.encode('utf-8'))
time.sleep(3)
while True:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
linelist = line.split(",")
item0 = int(linelist[0])
print(linelist)
if (item0 == 1):
# picam2.start_preview(Preview.QTGL, x=10, y=200, width=800, height=600)
test_string = "item 1 received"
# strBytes = bytes(test_string, 'utf-8')
strBytes = test_string.encode('utf-8')
ser.write(strBytes)
elif (item0 == 2):
picam2.stop_preview()
and here is the Arduino code (part of):
// SoftwareSerial mySerial(RX, TX)
// Bluetooth module's TXD pin to Arduino 8, RXD pin to Arduino 9
SoftwareSerial BT(8, 9);
// RPi's (UART0) TXD GPIO14 to Arduino 10, RXD GPIO15 to Arduino 11
// RPi's (UART5) TXD GPIO12 to Arduino 10, RXD GPIO13 to Arduino 11
SoftwareSerial RPi(10, 11);
void setup() {
Serial.begin(9600);
RPi.begin(9600);
BT.begin(9600);
Serial.println("Hello from PTS's USB hardware serial");
// BT.print(F("^PTS started on Arduino")); // DO NOT CHANGE the string
// RPi.println("Hello from Arduino"); // per ora posso spedire solo int
}
void loop() {
// By default, the last initialized port is listening.
// when you want to listen on a port, explicitly select it:
// BT.listen();
if (BT.available() >0) DataFromBT(); // data from BT
// RPi.listen();
if (RPi.available() >0) DataFromRPi(); // data from RPi
}
void DataFromRPi() {
Serial.println("Data from RPi!");
}