Temperature sensor emailing from python.

3 posts
by benmason » Mon May 20, 2013 10:28 pm
Good evening fellow Pi eaters,

I have been working on a project for a few months (lots of testing time) which sees my pi acting as a temperature monitor for an aquarium.

My two baby turtles are happily swimming around whilst my pi watches over the temperature of their tank. At the moment I am using a single 1-wire thermometer to monitor the water however in the future I will be adding additional sensors for air and outside temperatures.

Currently the program works well in its basic form, if the water temperature varies outside of the set range I receive an email warning me that the could be a problem. This is great but I wish to take it a step further, I am trying to get the email subject line or the body of the message to contain the current temperature string 'temp'.

I have tried a few things but each time I edit the email section of the code it gives errors which i just cant work out, the code is posted below.

Code: Select all
#Import modules
from datetime import datetime
from time import sleep
import smtplib
from email.mime.text import MIMEText
import os
#Clear the screen
#Print start testing and ideal range
print "Version 1.4"
print "Testing the  water temperature of the turtle tank. The ideal range is between  24 - 27 degrees celcius."
print "A warning e-mail will be sent to EMAIL ADDRESS HERE should the temperature be outside of the ideal range."

#set values
warningcount = 0
max = 27.5
min = 23.5

#Open datafile for storing temperature data
datafile = open("/tempdata.log", "a")
#open sensor data and make into a useable string
while True:
   tfile = open("/sys/bus/w1/devices/28-00000481ed30/w1_slave")
   text = tfile.read()
   tempdata = text.split("\n")[1].split(" ")[9]
   temp = float(tempdata[2: ])
   temp = temp / 1000
   #Print on screen information
   print "Time", datetime.time(datetime.now())
   print temp, "degrees celcius."
   print "................"
   #write temperature to log file
   time = datetime.time(datetime.now())
   datafile.write (str(time) + "\n")
   datafile.write(str(temp) + "\n")

   #Check temperature ranges
   if temp > max:
      print "WARNING!"
      print "Water temperature too hot."
      print "........................."
      warningcount = warningcount+1
      print "Warning count - ",  warningcount, "of 6."
      print "........................."
   if temp < min:
      print "WARNING!"
      print "Water temperature too cold."
      print "........................."
      warningcount = warningcount+1
      print "Warning count - ",  warningcount, "of 6."
      print "........................."

   #check for warnings
   if warningcount == 6:
   #Move data file to archive
      os.system('mv /tempdata.log "/logs/`date +%Y%m%d_%H.%M`_temp.log"')
   #create email
      message = """Water temperature outside of ideal range."""
      msg = MIMEText (message)
      msg['Subject'] = 'TURTLE TEMPERATURE WARNING!'   
      msg['From'] = 'Email address>'
      msg['To'] = 'Email address'
      # send the email
      s = smtplib.SMTP('smtp.o2.co.uk')
      s.login('username', 'password')
      s.sendmail(msg['From'], msg['To'], msg.as_string())
      #reset warningcount
      warningcount = 0
      #open datafile
      datafile = open("tempdata.log", "a")      
      print "The sensor has made six readings outside of the ideal range"
      print "Warning e-Mail sent!! Warnings reset."
      print "....................."
#sleep for 10  minutes
#close data file

Can anybody advise me on the correct coding to add the temperature string to the email?

Also when I view the data file "tempdata.log" it is always empty, however it must contain some data as the program works. it would be good to be able to copy the data into a spreadsheet for graphing and analysis. Any suggestions of why this may not be working correctly?

Many thanks

Posts: 23
Joined: Sat Jan 19, 2013 7:40 pm
by benmason » Mon May 20, 2013 10:41 pm
I should probably mention I am working on a model B Pi with Rasbian "Wheezy"

The script auto runs on startup and the unit is headless except for testing work.
Posts: 23
Joined: Sat Jan 19, 2013 7:40 pm
by saltydog » Tue May 21, 2013 12:00 am
See if this works.

Code: Select all
   #create email
      message = """Water temperature outside of ideal range. {} """.format(temp)

Posts: 23
Joined: Mon Dec 24, 2012 10:40 am