Python Forum
Problems displaying data sent from Arduino to Pi
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems displaying data sent from Arduino to Pi
#1
Hi,

I'm following the book Beginning Robotics with Raspberry Pi & Arduino.

I'm familiar with the Arduino and C++, but learning Python along with the book.

I'm at the part where I'm sending Distance Measurements from an Arduino hooked up to an HC-SR04 sensor over the USB serial connection to be displayed by the Python program.

This is the code that I have

# Import serial and time libraries
import serial
import time

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

while 1:
        recSer = ser.readline().decode('utf-8')
        recSer.rstrip()
        distance = int(recSer + '0')/10
        print("Distance: " + str(distance) + "cm", end = '\r')
        time.sleep(0.5)
Here is the error:
Error:
Distance: 0.0cm Traceback (most recent call last): File "/home/bkmypie/Documents/Robotics-Pie-Arduino/serial_test.py", line 10, in <module> distance = int(recSer + '0')/10 ValueError: invalid literal for int() with base 10: '342\r\n0
I have googled around the net and I understand it is to with trying to display a float as an int in a string, but I'm at a loss on how to fix it.

I've tried int(float(recSer + '0') but still no joy.

I am using Python 3.5.3 on the Raspberry Pi,

Any help appreciated,
Reply
#2
(May-18-2019, 06:30 PM)VostokRising Wrote: recSer.rstrip()
distance = int(recSer + '0')/10
rstrip() does not work in-place
so it should be
recSer = recSer.rstrip()
then what is the point to conacatenate 0 and then devide by 10? convert to float? then simply use float()
if it is just for string formatting
Python 3.7.3 (default, Mar 26 2019, 01:59:45) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> recSer = '342\r\n'  # to replicate what you get
>>> distance = recSer.rstrip()
>>> float(distance)
342.0
>>> distance = int(distance) # convert from str to int
>>> print(f'Distance is {distance:.1f}cm')
Distance is 342.0cm
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hi buran,

Thanks for that. It works perfectly. Like I said I'm working through the book and wrote down his code as he has it in the chapter.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to Get Arduino sensor data over to excel using Python. eh5713 1 1,717 Dec-01-2022, 01:52 PM
Last Post: deanhystad
  Problems Sorting Data in an External File (.txt) Superlegend21 1 4,331 Dec-27-2020, 10:06 PM
Last Post: Superlegend21
  Save Arduino data in mysql server via raspberrypi rithikvg 1 3,410 Jun-24-2020, 10:59 PM
Last Post: Larz60+
  Help Graphing Arduino Data Real Time in Python nschulz 0 2,539 Mar-12-2020, 06:15 PM
Last Post: nschulz
  Matplotlib Hovering Event displaying (x, y) data RawlinsCross 0 6,220 Oct-18-2019, 06:13 PM
Last Post: RawlinsCross
  Problems with displaying a 15x15 Complex Matrix JoelFooCJ 5 3,275 Sep-20-2019, 07:43 AM
Last Post: newbieAuggie2019
  show and storage real temperature data Arduino in Python3 linkxxx 0 1,856 Aug-21-2019, 04:07 PM
Last Post: linkxxx
  Pygal: Displaying information for each data point KirkmanJ 0 1,846 Jul-29-2019, 01:10 PM
Last Post: KirkmanJ
  Arduino Uno connected to PI 0 W - Sensor Data MQTT runboy 4 3,070 Nov-07-2018, 03:55 AM
Last Post: runboy
  Displaying image on PC using Arduino and Python GallowMilk 1 2,328 Sep-19-2018, 06:38 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020