Pages: 1 [2]   Go Down
Author Topic: Two way serial communication with raspberry Pi (radio project)  (Read 4452 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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





Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 642
Posts: 50431
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#!/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.

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 642
Posts: 50431
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


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

Code:
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?


Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 637
Posts: 34580
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Problem solved:

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

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

Code:
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:
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.


Logged

Pages: 1 [2]   Go Up
Jump to: