Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Doubt
#1
Hi , i need some help in python script :
Im receiveing data from USB0 of my Raspberry Pi and for now , with my python script i can print all data . What i need now is only when i receive numerics values of light greater than 50 print "Atencion!!!" and smaller than 50 print "LOW"
if (whatireceive) > 50 print (ATENCION!!!) else print (LOW!!!)

#####################################################
PI incoming from USB0: I already receive this in my USB0 port

pi@lima-raspberrypi:~ $ python receive_seria_data_from_usb0.py
12 lx, level: 1 , quality: toolow
Light: 12 lx
12 lx, level: 1 , quality: toolow
Light: 15 lx
15 lx, level: 1 , quality: toolow
Light: 34 lx
34 lx, level: 2 , quality: low
Light: 28 lx
28 lx, level: 2 , quality: low
Light: 67 lx
67 lx, level: 3 , quality: ideal
Light: 88 lx
88 lx, level: 3 , quality: ideal
Light: 582 lx
582 lx, level: 5 , quality: toohigh
Light: 664 lx
664 lx, level: 5 , quality: toohigh
Light: 28 lx
28 lx, level: 2 , quality: low
Light: 12 lx
12 lx, level: 1 , quality: toolow
^
pi@lima-raspberrypi:~ $

###########################################################
Python Script:

#!/usr/bin/env python3
import serial
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
ser.flush()
while True:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
print(line)

############################################################
Someone can help?
buran write Jan-22-2021, 05:55 PM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Reply
#2
Try putting this after the line,

line = ser.readline().decode('utf-8').rstrip()
number = [int (s) for s in line.split () if s.isdigit ()]
if number [0] > 50 :
	print ('ATTENTION!!!')
else : 
	print ('LOW!!!')
Reply
#3
(Jan-22-2021, 05:39 PM)BashBedlam Wrote: number = [int (s) for s in line.split () if s.isdigit ()]if number [0] > 50 :    print ('ATTENTION!!!')else :     print ('LOW!!!')
Hi , thaks, but it give me an error:
pi@lima-raspberrypi:~ $ python receive_serial_data_from_usb0.py
File "receive_serial_data_from_usb0.py", line 10
number = [int (s) for s in line.split () if s.isdigit ()]
^
IndentationError: unexpected indent
Reply
#4
(Jan-22-2021, 06:00 PM)csrlima Wrote: Hi , thaks, but it give me an error:
pi@lima-raspberrypi:~ $ python receive_serial_data_from_usb0.py
File "receive_serial_data_from_usb0.py", line 10
number = [int (s) for s in line.split () if s.isdigit ()]
^
IndentationError: unexpected indent

The whole thing should look like this. You can copy and paste.

#!/usr/bin/env python3
import serial
ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
ser.flush()
while True:
	if ser.in_waiting > 0:
		line = ser.readline().decode('utf-8').rstrip()
		number = [int (s) for s in line.split () if s.isdigit ()]
		if number [0] > 50 :
			print ('ATTENTION!!!')
		else : 
			print ('LOW!!!')
		print(line)
csrlima likes this post
Reply
#5
(Jan-22-2021, 06:19 PM)BashBedlam Wrote:
(Jan-22-2021, 06:00 PM)csrlima Wrote: Hi , thaks, but it give me an error:
pi@lima-raspberrypi:~ $ python receive_serial_data_from_usb0.py
File "receive_serial_data_from_usb0.py", line 10
number = [int (s) for s in line.split () if s.isdigit ()]
^
IndentationError: unexpected indent

The whole thing should look like this. You can copy and paste.

#!/usr/bin/env python3
import serial
ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
ser.flush()
while True:
	if ser.in_waiting > 0:
		line = ser.readline().decode('utf-8').rstrip()
		number = [int (s) for s in line.split () if s.isdigit ()]
		if number [0] > 50 :
			print ('ATTENTION!!!')
		else : 
			print ('LOW!!!')
		print(line)

Five stars , for now is all i needed . Thank you very much
Reply
#6
(Jan-22-2021, 06:22 PM)csrlima Wrote:
(Jan-22-2021, 06:19 PM)BashBedlam Wrote: The whole thing should look like this. You can copy and paste.

#!/usr/bin/env python3
import serial
ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
ser.flush()
while True:
	if ser.in_waiting > 0:
		line = ser.readline().decode('utf-8').rstrip()
		number = [int (s) for s in line.split () if s.isdigit ()]
		if number [0] > 50 :
			print ('ATTENTION!!!')
		else : 
			print ('LOW!!!')
		print(line)

Five stars , for now is all i needed . Thank you very much

one more doubt : i need to replace print for this command: sudo python smartblinds-diy/control.py -t AA:74:56:99:71:AE -c move_down , but when i execute this command from root it works but inside this python script dont : pi@lima-raspberrypi:~ $ python receive_serial_data_from_usb01.py
File "receive_serial_data_from_usb01.py", line 13
sudo python smartblinds-diy/control.py -t DA:74:56:99:71:DE -c move_down
^
SyntaxError: invalid syntax
pi@lima-raspberrypi:~ $
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Doubt about conditionals in Python. Carmazum 6 1,536 Apr-01-2023, 12:01 AM
Last Post: Carmazum
  A simple python doubt mohamedrabeek 2 692 Mar-26-2023, 07:24 PM
Last Post: deanhystad
  doubt about python tkinter and sqlite3 LONDER 2 2,119 Aug-14-2021, 08:48 AM
Last Post: ibreeden
  Python Exercise Doubt azure 4 2,600 Apr-21-2020, 01:15 PM
Last Post: azure
  Doubt in Regex Lookaround fullstop 3 2,341 Feb-03-2020, 09:53 AM
Last Post: Gribouillis
  A doubt with 'in' and 'not in' operators with strings newbieAuggie2019 7 3,523 Oct-23-2019, 03:11 PM
Last Post: perfringo
  OpenCV - Doubt in a line. ArjunSingh 1 2,462 Jul-14-2019, 03:36 PM
Last Post: ThomasL
  doubt saipython 1 2,785 Aug-21-2017, 06:05 AM
Last Post: Larz60+
  Basic Doubt in code prateek3 6 4,828 Aug-19-2017, 01:05 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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