Hi.
I’m trying to establish communication between an Arduino and an Raspberry Pi via GPIO serial, bidirectional reading and writing. Sorry, I'm not an expert.
The Rpi read correctly the data sent from the Arduino, nevertheless no data is received by the Arduino from the RPi's serial.write.
Here is the python code on the RPi:
#!/usr/bin/env python3
import time, serial, subprocess, signal, sys
ser = serial.Serial("/dev/ttyAMA5",9600)
time.sleep(1)
ser.write(b"Hello from Raspberry Pi!\n")
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(linelist)
if (item0 == 1):
test_string = "item 1 received"
strBytes = test_string.encode('utf-8')
ser.write(strBytes)
elif (item0 == 2):
ser.write(b"due\n")
And this is the relevant part of Arduino code:
#include "defs.h"
#include <SoftwareSerial.h>
// SoftwareSerial mySerial(RX, TX)
// Bluetooth module's TXD pin to Arduino 10, RXD pin to Arduino 11
SoftwareSerial BT(10, 11);
// RPi's (UART5) TXD GPIO12 to Arduino 8, RXD GPIO13 to Arduino 9
SoftwareSerial RPi(8, 9);
void setup() {
Serial.begin(9600);
RPi.begin(9600);
BT.begin(9600);
Serial.println("Hello from PTS's USB hardware serial");
// RPi.println("Hello from Arduino");
}
void loop() {
if (RPi.available() >0) DataFromRPi(); // data from RPi
if (BT.available() >0) DataFromBT(); // data from BT
}
void DataFromRPi() {
String data = Serial.readStringUntil('\n');
Serial.print("You sent me: ");
Serial.println(data);
}
void DataFromBT() {
getSerialBT(); // first value representing the 'caseN' integer
caseN = serialdata;
getSerialBT(); // second value
data1 = serialdata;
getSerialBT(); // third value
data2 = serialdata;
getSerialBT(); // fourth value
data3 = serialdata;
Serial.print("Sent to RPi: ");
Serial.print(caseN); Serial.print(data1); Serial.print(data2); Serial.println(data3);
switch(caseN) {
case 0: {resetFunc(); delay(500); break;}
case 1: { // string for RPi
RPi.print(data1);
RPi.print(",");
RPi.print(data2);
RPi.print(",");
RPi.print(data3);
RPi.println(","); break;
}
case 2: {
// Serial.print(data1); Serial.print(" "); Serial.println(data2); break;
}
default: {
BT.print(F("^Unused command! (case "));
BT.print(caseN);
BT.print(F(" in DataFromBT() switch call"));
}
}
}
long getSerialBT() {
// https://forum.arduino.cc/t/how-do-i-handle-negative-numbers/477387/3
serialdata = 0;
while (inbyte != ',') {
inbyte = BT.read();
if (inbyte >= '0' && inbyte <= '9') {
serialdata = (serialdata * 10) + (inbyte - '0');
} else if (inbyte == '-') {
dataSign = -1;
}
}
serialdata = serialdata * dataSign;
inbyte = 0;
dataSign = 1;
return serialdata;
}
Arduino and RPi are connected with wires using GPIO pins (12,13 on the RPi side) while Arduino is also connected via Bluetooth with an AppInventor app on my phone. Arduino actually receive commands from phone app and then send them to RPi via wired UART5. This is the relevant part of my RPi /boot/config.txt:
[all]
dtparam=i2c_arm=on
dtparam=spi=on
enable_uart=1
dtoverlay=uart5
The Arduino board is USB wired to my laptop, where I write and upload code to the Arduino. The RPi is controlled via RealVNC for coding in Python.
This is the shell output where the Python program is executed:
root@RPi4> python /home/gigi/Documents/arduino_comm2.py
Waiting for Arduino commands
No messages on the Arduino IDE serial console from the RPi, only the message from Arduino code previously running:
Hello from PTS's USB hardware serial
When I send the command "1,1,0,0," from phone app, here is the output on Arduino console:
Sent to RPi: 1100
and here is the RPi shell output:
['1', '0', '0', '']
Again, no messages received by the Arduino from the RPi.
I swapped connecting wires on both Arduino and RPi but no success. Same results.
Any advice? Thanks