Go Down

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

Schwabinger

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 :-(






PaulS

Quote
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?

Schwabinger

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

Code: [Select]
#!/bin/bash
set -x
ALTWERT=1
while true
do
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"
ALTWERT=$WERT
#sleep 1
fi

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


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.


PaulS

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.

Schwabinger

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?



Grumpy_Mike

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

Come on that is not a C program.

Quote
Unfortunately my script does not catch that value.

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

Schwabinger

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