Jan 25, 2015

High CPU temperature Raspberry Pi warning by mail

Prevent to bake your Raspberry Pi with this script. It alerts you by mail when CPU temperature is too high. Let's start.... Create your script file:
 cd to /path/you/prefered/   
 sudo nano nameofscript.py #this wiil create blank file and paste code   
CODE:
 #!/usr/bin/env python  
 # coding=utf-8  
 import os  
 import smtplib  
 from email.mime.text import MIMEText  
 # 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 abouve 60°C (you can change this value, but it shouldn't be above 70)  
 if (temp >50):  
  # 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("yourmail@gmail.com" , "yourpassword")  
  # Now comes the Text we want to send:  
  value = "Warning! Too high CPU Temperature is: " + getCPUtemperature()  
  msg = MIMEText(value)  
  # The Subject of your E-Mail  
  msg['Subject'] = "Warning! Too high CPU Temperature:" + getCPUtemperature()  
  # Consigner of your E-Mail  
  msg['From'] = "Raspberry Pi"  
  # recipient of your E-Mail  
  msg['To'] = "receivermail@gmail.com"  
  # Finally send the mail  
  server.sendmail("yourmail@gmail.com", "receivermail@gmail.com", msg.as_string())  
  server.quit()  
Fill your data and save file (CTRL+O-> Enter) and exit (CTRL+X). To test it just make temperature lower than actual. If you receive mail everything is correct. One more thing add script to crontab:
 su - # to become root  
 crontab -e # to edit   
Then add:
 */5 * * * * python /path/to/yourscript/yourscript.py 2>&1 # this will check your temp every 5 minutes  
And again save file (CTRL+O-> Enter) and exit (CTRL+X). That's it! You are done!

You can buy sensors from HERE