# coding=utf-8
#import time
import os
import smtplib
from email.mime.text import MIMEText
critical = False
high = 50
too_high = 80
# At First we have to get the current CPU-Temperature with this defined function
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
return(res.replace("temp=","").replace("'C\n",""))
# Now we convert our value into a float number
temp = float(getCPUtemperature())
# Check if the temperature is above 60°C (you can change this value, but it shouldn't be above 70)
if (temp > high):
if (temp > too_high):
critical = True
subject = "Critical warning! The temperature is: {} shutting down!!".format(temp)
body = "Critical warning! The actual temperature is: {} \n\n Shutting down the pi!".format(temp)
else:
subject = "Warning! The temperature is: {} ".format(temp)
body = "Warning! The actual temperature is: {} ".format(temp)
else:
subject = "LousReef Controller at Normal Temp"
body = "LousReef running at {}".format(temp)
# Enter your smtp Server-Connection
server = smtplib.SMTP('smtp.gmail.com', 587) # if your using gmail: smtp.gmail.com
server.ehlo()
server.starttls()
server.ehlo
# Login
server.login("your email", "your password")
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = "you"
msg['To'] = "whoever"
# Finally send the mail
server.sendmail("from", "to", msg.as_string())
server.quit()
# Critical, shut down the pi
if critical:
os.popen('sudo halt')