Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read temperature once
#1
Hi,

Im new to python Rolleyes
I have this code found of youtube, it works fine and print the temperature in a loop (until I do CTRL C)
#!/usr/bin/python

import sys, time, signal, serial, datetime
from socket import *

ser = serial.Serial(
	port='/dev/ttyUSB0',
	baudrate=9600,
	#parity=serial.PARITY_ODD,
	#stopbits=serial.STOPBITS_TWO,
	#bytesize=serial.SEVENBITS
	)
	
ser.isOpen()
	
while True:
		ser.write('T\r')
		time.sleep(1)
		serial_input = ''
		while ser.inWaiting() > 0:
			serial_input += ser.read(1)
		now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
		print now + ", " + serial_input
		
ser.close()
sys.exit()
But If I want to print it only once and optional also sent it by email?
I have tried this code (just print it once, no email yet):
#!/usr/bin/python

import sys, time, signal, serial, datetime
from socket import *

ser = serial.Serial(
	port='/dev/ttyUSB0',
	baudrate=9600,
	#parity=serial.PARITY_ODD,
	#stopbits=serial.STOPBITS_TWO,
	#bytesize=serial.SEVENBITS
	)
	
ser.isOpen()

ser.write('T\r')
serial_input = ''
serial_input = ser.read(1)
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print now + serial_input		

ser.close()

sys.exit()
But it just print the date and time and a " + " no temp - what am I missing?

/Martin
Reply
#2
Hi,
You missed out this line while ser.inWaiting() > 0:

#!/usr/bin/python
 
import sys, time, signal, serial, datetime
from socket import *
 
ser = serial.Serial(
    port='/dev/ttyUSB0',
    baudrate=9600,
    #parity=serial.PARITY_ODD,
    #stopbits=serial.STOPBITS_TWO,
    #bytesize=serial.SEVENBITS
    )
     
ser.isOpen()
     
ser.write('T\r')
time.sleep(1)
serial_input = ''
while ser.inWaiting() > 0:
    serial_input += ser.read(1)
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print now + ", " + serial_input
         
ser.close()
sys.exit()
Reply
#3
Ah ok thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help gathering Temperature from API response road102 5 1,068 Dec-16-2022, 08:30 PM
Last Post: road102
  get data (temperature and humidity) from DHT22 into CSV and sending them over the net apollo 0 3,840 Apr-16-2021, 07:49 PM
Last Post: apollo
  monitoring the temperature of the CPU with Python apollo 2 8,704 Apr-13-2021, 05:39 PM
Last Post: apollo
  print CPU temperature samuelbachorik 12 6,053 Aug-04-2020, 03:28 PM
Last Post: Axel_Erfurt
  Importing a temperature converting module RedSkeleton007 2 8,010 Nov-12-2017, 01:20 PM
Last Post: sparkz_alot
  Temperature Code help? 20AJ0931 2 6,531 Apr-08-2017, 12:35 AM
Last Post: snippsat
  Temperature value code 20AJ0931 10 13,466 Mar-19-2017, 11:05 PM
Last Post: 20AJ0931

Forum Jump:

User Panel Messages

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