Python Forum
Python Doubt - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python Doubt (/thread-32134.html)



Python Doubt - csrlima - Jan-22-2021

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?


RE: Python Doubt - BashBedlam - Jan-22-2021

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!!!')



RE: Python Doubt - csrlima - Jan-22-2021

(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


RE: Python Doubt - BashBedlam - Jan-22-2021

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



RE: Python Doubt - csrlima - Jan-22-2021

(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


RE: Python Doubt - csrlima - Jan-23-2021

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