jcarnaggio
Posts: 38
Joined: Thu Feb 26, 2015 2:54 am

Sending data from Arduino to Raspberry Pi

Fri May 29, 2015 10:42 pm

I have an Arduino with multiple sensors attached to it to collect GPS coordinates; however, I have only been able to get that information printed to the Serial console. I would like to be able to get the data being printed to the serial console on to a Raspberry Pi as easily and as quickly as possible so I'm just wondering what the best way to do that would be.

All info appreciated.


User avatar
DougieLawson
Posts: 32339
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Sending data from Arduino to Raspberry Pi

Fri May 29, 2015 11:13 pm

Use the USB connection, open /dev/ttyACM0 on your RPi and read the data.

Code: Select all

#!/usr/bin/python

import serial, string, time

output = " "
ser = serial.Serial('/dev/ttyACM0', 9600, 8, 'N', 1, timeout=1)


#while True:
#  print "----"
#  while output != "":
#    output = ser.readline()
#    print output
#  output = " "

for i in range(0,256):
  print unichr(i)
  ser.write(chr(i))
  time.sleep(1)
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3B*3, 3B+

Please post ALL technical questions on the forum. Do not send private messages.

jcarnaggio
Posts: 38
Joined: Thu Feb 26, 2015 2:54 am

Re: Sending data from Arduino to Raspberry Pi

Fri May 29, 2015 11:14 pm

DougieLawson wrote:Use the USB connection, open /dev/ttyACM0 on your RPi and read the data.

Code: Select all

#!/usr/bin/python

import serial, string, time

output = " "
ser = serial.Serial('/dev/ttyACM0', 9600, 8, 'N', 1, timeout=1)


#while True:
#  print "----"
#  while output != "":
#    output = ser.readline()
#    print output
#  output = " "

for i in range(0,256):
  print unichr(i)
  ser.write(chr(i))
  time.sleep(1)
Is this possible with C++?

User avatar
AndyD
Posts: 2321
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: Sending data from Arduino to Raspberry Pi

Fri May 29, 2015 11:31 pm

jcarnaggio wrote:Is this possible with C++?
Yes, of course!

jcarnaggio
Posts: 38
Joined: Thu Feb 26, 2015 2:54 am

Re: Sending data from Arduino to Raspberry Pi

Fri May 29, 2015 11:37 pm

AndyD wrote:
jcarnaggio wrote:Is this possible with C++?
Yes, of course!
Would an explanation be possible? This Serial business is kind of foreign to me.

jcarnaggio
Posts: 38
Joined: Thu Feb 26, 2015 2:54 am

Re: Sending data from Arduino to Raspberry Pi

Fri May 29, 2015 11:41 pm

DougieLawson wrote:Use the USB connection, open /dev/ttyACM0 on your RPi and read the data.

Code: Select all

#!/usr/bin/python

import serial, string, time

output = " "
ser = serial.Serial('/dev/ttyACM0', 9600, 8, 'N', 1, timeout=1)


#while True:
#  print "----"
#  while output != "":
#    output = ser.readline()
#    print output
#  output = " "

for i in range(0,256):
  print unichr(i)
  ser.write(chr(i))
  time.sleep(1)
I tried to test this code with a program on the Uno that just printed "Hello, World!" over and over again and the output I got from the Python program was really odd. I got little squares with four characters in each one at a really inconsistent rate.

User avatar
AndyD
Posts: 2321
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: Sending data from Arduino to Raspberry Pi

Fri May 29, 2015 11:51 pm

jcarnaggio wrote:Would an explanation be possible? This Serial business is kind of foreign to me.
Sorry, I wasn't intending to be so short, but I was interrupted in posting. There is a huge amount of serial code on the Internet for C/C++ Linux. A quick search found this on stackoverflow. Unfortunately, I don't have a clean example that I can post right now.

User avatar
AndyD
Posts: 2321
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: Sending data from Arduino to Raspberry Pi

Fri May 29, 2015 11:56 pm

Dougie's code had the actual loop that reads from the serial port commented out. Try this

Code: Select all

#!/usr/bin/python

import serial, string, time

output = " "
ser = serial.Serial('/dev/ttyACM0', 9600, 8, 'N', 1, timeout=1)

while True:
    print "----"
    while output != "":
    output = ser.readline()
    print output
    output = " "
I haven't tested it, but it appears to be reading a line of data from the serial port and printing it out.

User avatar
joan
Posts: 13294
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Sending data from Arduino to Raspberry Pi

Sat May 30, 2015 12:18 am

...or just.

Code: Select all

#!/usr/bin/python

import serial

ser = serial.Serial('/dev/ttyACM0', 9600, 8, 'N', 1, timeout=5)

while True:
    print(ser.readline())

jcarnaggio
Posts: 38
Joined: Thu Feb 26, 2015 2:54 am

Re: Sending data from Arduino to Raspberry Pi

Sat May 30, 2015 8:15 pm

joan wrote:...or just.

Code: Select all

#!/usr/bin/python

import serial

ser = serial.Serial('/dev/ttyACM0', 9600, 8, 'N', 1, timeout=5)

while True:
    print(ser.readline())
This works, but the first couple of lines are a bit wonky. Is there any way to perfectly time it so if I wanted to print out unique lines and get them as they were printed to the serial exactly, could I do that?

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: Sending data from Arduino to Raspberry Pi

Sun May 31, 2015 12:00 am

jcarnaggio wrote:
joan wrote:...or just.

Code: Select all

#!/usr/bin/python

import serial

ser = serial.Serial('/dev/ttyACM0', 9600, 8, 'N', 1, timeout=5)

while True:
    print(ser.readline())
This works, but the first couple of lines are a bit wonky. Is there any way to perfectly time it so if I wanted to print out unique lines and get them as they were printed to the serial exactly, could I do that?
Yes, of course!

Your Arduino will be sending stuff all the time, so if you start the Pi program at a random time you might start receiving half-way through a transmission.

If it's just GPS NMEA strings you are dealing with then just discard any string that does not begin with "$". If you are making your own protocol between the Arduino and the Pi then make sure you insert some obvious way to detect the start of a packet, then you can use that.

will2055
Posts: 1
Joined: Thu Jul 20, 2017 10:05 pm

Re: Sending data from Arduino to Raspberry Pi

Thu Jul 20, 2017 10:15 pm

joan wrote:...or just.

Code: Select all

#!/usr/bin/python

import serial

ser = serial.Serial('/dev/ttyACM0', 9600, 8, 'N', 1, timeout=5)

while True:
    print(ser.readline())
What type does the data become? I'm assuming they are placed as string variables. How could I do integers?

eripy
Posts: 3
Joined: Sun Jan 14, 2018 9:03 pm

Re: Sending data from Arduino to Raspberry Pi

Sun Jan 14, 2018 9:13 pm

Code not stable, after 15- 20 sec I get this:
Traceback (most recent call last):
File "/home/pi/myprograms/ser.py", line 6, in <module>
print(ser.readline())
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 490, in read
'device reports readiness to read but returned no data '
SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

Arduino sends data continously

asandford
Posts: 1988
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Sending data from Arduino to Raspberry Pi

Mon Jan 15, 2018 1:37 am

eripy wrote:
Sun Jan 14, 2018 9:13 pm
Code not stable, after 15- 20 sec I get this:
Traceback (most recent call last):
File "/home/pi/myprograms/ser.py", line 6, in <module>
print(ser.readline())
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 490, in read
'device reports readiness to read but returned no data '
SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

Arduino sends data continously
Thread over 2 years old, things have probably moved on.

scotty101
Posts: 2777
Joined: Fri Jun 08, 2012 6:03 pm

Re: Sending data from Arduino to Raspberry Pi

Mon Jan 15, 2018 10:01 am

Try this instead

Code: Select all

#!/usr/bin/python

import serial

ser = serial.Serial('/dev/ttyACM0', 9600, 8, 'N', 1, timeout=5)

while True:
    if ser.in_waiting > 0:
        print(ser.readline())
Often the python program will read data from the serial port quicker than it can be received. The line I have added checks to see if there is and data first before trying to read it.

Note: in_waiting works with PySerial version 3.0 and greater. For older versions use ser.inWaiting()
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

eripy
Posts: 3
Joined: Sun Jan 14, 2018 9:03 pm

Re: Sending data from Arduino to Raspberry Pi

Mon Jan 15, 2018 8:56 pm

OK tried to add inWaiting but its the same. However I see that Arduino serial monitor is disturbed by the python program on Raspberry

Analog: 293 , 285 ( 966 )
Analog: 293 , 285 ( 969 )
Anal Ana95An8 ) A A4Anal ) Ana)
Analog: 301 , 291 ( 993 )
Analog: 302 , 293 ( 996 )

User avatar
DougieLawson
Posts: 32339
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Sending data from Arduino to Raspberry Pi

Mon Jan 15, 2018 9:10 pm

Can you post your Arduino sketch?

You can't run the IDE serial monitor while you're running the python program.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3B*3, 3B+

Please post ALL technical questions on the forum. Do not send private messages.

eripy
Posts: 3
Joined: Sun Jan 14, 2018 9:03 pm

Re: Sending data from Arduino to Raspberry Pi

Mon Jan 15, 2018 10:29 pm

Thanks, closing the Serial monitor solved the problem

the Arduino code just uses Serial.print

Next I will try to get it out on Ethernet :-)

Return to “Interfacing (DSI, CSI, I2C, etc.)”

Who is online

Users browsing this forum: No registered users and 8 guests