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.
Can anybody advise me on the correct coding to add the temperature string to the email?
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 os.system('clear') #Print start testing and ideal range print print "TURTLE TEMP" print "Version 1.4" print print "Testing the water temperature of the turtle tank. The ideal range is between 24 - 27 degrees celcius." print print "A warning e-mail will be sent to EMAIL ADDRESS HERE should the temperature be outside of the ideal range." print print print #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() tfile.close() tempdata = text.split("\n").split(" ") temp = float(tempdata[2: ]) temp = temp / 1000 #Print on screen information print "Time", datetime.time(datetime.now()) print 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 print "........................." if temp < min: print "WARNING!" print "Water temperature too cold." print "........................." warningcount = warningcount+1 print "Warning count - ", warningcount, "of 6." print print "........................." #check for warnings if warningcount == 6: #Move data file to archive datafile.close() 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()) s.quit() #reset warningcount warningcount = 0 #open datafile datafile = open("tempdata.log", "a") print print "The sensor has made six readings outside of the ideal range" print "Warning e-Mail sent!! Warnings reset." print "....................." print print #sleep for 10 minutes sleep(600) #close data file datafile.close()
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?