Below is a little code that I have written in python that is scheduled on a crontab to run every 15 minutes. It is supposed to check the temperature of the CPU on my raspberry pi, and text me asking what to do if the temperature is above 45 degrees Celsius. I know I am receiving the correct temperature value because it prints to the screen as a number. However, I seem to get a text message every 15 minutes stating its temperature even thought it is below 45 degrees. I know the error must be in my conditional somewhere, but I am new to python syntax and can't figure it out. Ive tried comparing to both 45 and 45.0 using both > and >=.
import os
import smtplib
import sys
# Return CPU temperature as a character string
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
return(res.replace("temp=","").replace("'C\n",""))
# CPU informatiom
CPU_temp = getCPUtemperature()
if CPU_temp > 45.0:
fromaddr = '[email protected]'
toaddrs = '[email protected]'
msg = 'My current CPU temperature is %s degrees. Should I shutdown?' % (CPU_temp)
# Credentials (if needed)
username = 'xxxxxxxxxx'
password = 'xxxxxxxxxx'
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
sys.exit()
else:
sys.exit()