Go Down

Topic: Two way serial communication with raspberry Pi (radio project) (Read 8172 times) previous topic - next topic


Here is my scenario:

Arduino is connected to my Windows Desktop PC. This is where I write the Arduino code.
If I use the Serial Monitor and run the sketch, everything works fine. In the serial monitor I can fill variable "maxNumb" and the arduino sketch does what I want.

When I connect arduino to my Raspberry pi computer (the target system for my Internet radio):

- Raspberry can receive tuner values between 1 and 26 (according to the pot position)
- I cannot use "echo 40 > /dev/ttyACM0" to change the value of variable "maxNumb".

So the serial communication works from arduino to pi, but not the other way :-(


So the serial communication works from arduino to pi, but not the other way :-(

In the if(Serial.available() > 0) block, blink the onboard LED a few times. This will tell you whether anything is getting from the pi to the Arduino. If something, garbled or not, gets through, you know one thing. If nothing gets through, you know something else.

How are you, on the pi, receiving the serial data from the Arduino?


This is how I read on the pi what arduino is sending:

Code: [Select]
set -x
while true
WERT=$(head -n 1 < /dev/ttyACM0)
WERT=$(echo  $WERT | sed "s:\r::")
NUMBSTATIONS=$(mpc playlist | wc -l)

if [ $ALTWERT -ne $WERT ]; then
mpc play "$WERT"
#sleep 1

#echo  "$NUMBSTATIONS\n"  > /dev/ttyACM0

Variable WERT is filled with what is coming from arduino. So by turning the pot I can "tune" through my playlist.

Variable NUMBSTATIONS hold the number of radio stations in my playlist.  This is the value that I want to echo to the pot.

Echo is commented # in the code above;

But as I leared in this thread in the last two hours "echo" does not work in this case.


Perhaps you need to use cat to send the value to the serial port, rather than echo. I'd be happy to test it for you, if you send me the pi.


Just come over to Europe ... the beer is on me :-)

I found a c Program "ardunio-serial.c"

Code: [Select]
pi@raspberrypi ~/scripts $ ./arduino-serial -b 9600 -p /dev/ttyACM0 -d 1600 -s 30 -r
read: 30

With that I can transfer a value like "30" in this example from the raspberry to arduino.
Unfortunately my script does not catch that value.

Any idea, why?


I found a c Program "ardunio-serial.c"

Come on that is not a C program.

Unfortunately my script does not catch that value.

That would suggest it is not being sent or you have the reset problem describe earlier.


Problem solved:

I skipped Arduino Uno and work with Raspberry's gpio pins only. No serial communication needed any more ...  XD

The following declares  gpio pin 7 to be an input pin:

Code: [Select]
echo "7" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio7/direction

Whenever I press a button, variable BUTTON gets value 1, otherwise 0

Code: [Select]
BUTTON=$(cat /sys/class/gpio/gpio7/value)

So after booting my radio starts playing with station 1; pressing the button produces a switch to the next station.
When the end of the list is reached, we start again with station 1.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131