Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2.7
- # -*- coding: utf-8 -*-
- import os
- import smtplib
- import time, datetime
- from time import strftime
- import MySQLdb
- import RPi.GPIO as GPIO
- import sys
- import random
- alerted = 0
- higalerted = 0
- os.system('modprobe w1-gpio')
- os.system('modprobe w1-therm')
- def temp_raw():
- f = open('/sys/bus/w1/devices/28-0000059dfde7/w1_slave','r')
- lines = f.readlines()
- f.close()
- return lines
- def read_temp():
- lines = temp_raw()
- while lines[0].strip()[-3:] != 'YES':
- time.sleep(0.2)
- lines = temp_raw()
- temp_output = lines[1].find('t=')
- if temp_output != -1:
- temp_string = lines[1].strip()[temp_output+2:]
- temp_c = float(temp_string) / 1000.0
- temp_f = temp_c * 9.0 / 5.0 + 32.0
- return temp_c, temp_f
- def mysql_datetime():
- t = datetime.datetime.now()
- day = t.day
- month = t.month
- year = t.year
- hour = t.hour+1
- minute = t.minute
- dtime = "%d-%02d-%02d %02d:%02d:00" % (year,month,day,hour,minute)
- return dtime
- def plaintime():
- t = datetime.datetime.now()
- minute = t.minute
- if t.hour > 11:
- if t.hour > 12:
- hour = t.hour - 12
- else:
- hour = t.hour
- return "%d:%02dpm" % (hour, minute)
- else:
- if t.hour == 0:
- hour = 12
- else:
- hour = t.hour
- return "%d:%02dam" % (hour, minute)
- def temp_post():
- temps = read_temp()
- tempc = temps[0] #temps[0] reads Celcius value, temps[1] reads fahrenheit
- temp = "%3.1f" % tempc
- ptime = plaintime()
- datetime = mysql_datetime()
- conn = MySQLdb.connect(host="localhost",user="pytemp",passwd="celcius",db="roomtemp")
- cur = conn.cursor()
- sql = ("""INSERT INTO roomtemp.temperatures (datetime,plaintime,temp) VALUES (%s,%s,%s)""", (datetime,ptime,temp))
- try:
- cur.execute(*sql)
- conn.commit()
- except:
- conn.rollback()
- def sendalert(emailbody):
- SMTP_SERVER = ''
- SMTP_PORT = 587
- EMAIL_USERNAME = ''
- EMAIL_PASSWORD = '' #CAUTION: This is stored in plain text!
- recipient = ''
- subject = 'SERVER ROOM TEMPERATURE ALERT'
- emailText = emailbody
- headers = ["From: " + EMAIL_USERNAME, "Subject: " + subject, "To: " + recipient, "MIME-Version: 1.0", "Content-Type: text/html"]
- headers = "\r\n".join(headers)
- session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
- session.ehlo()
- session.starttls()
- session.ehlo
- session.login(EMAIL_USERNAME, EMAIL_PASSWORD)
- session.sendmail(EMAIL_USERNAME, recipient, headers + "\r\n\r\n" + emailText)
- session.quit()
- def alerttest():
- global alerted
- global highalerted
- alerted()
- temps = read_temp()
- temp = temps[0]
- nowtemp="Current temperature is %2.2f degrees C" % temp
- if highalerted == 0:
- if temp > 25:
- sethighalerted(1)
- message = "Temperature in server room is over 25 degrees! "+temptext
- sendalert(message)
- else:
- sethighalerted(0)
- else:
- if temp > 25:
- sethighalerted(1)
- else:
- sethighalerted(0)
- rows = []
- db = MySQLdb.connect(host="localhost", user="pytemp", passwd="celcius", db="roomtemp")
- cur = db.cursor()
- cur.execute("SELECT temp FROM roomtemp.temperatures order by id desc limit 30")
- for row in cur.fetchall():
- newrow = row[0]
- rows.append( newrow )
- if temp > 18:
- if alerted == 0:
- if (rows[0] - 2.99) > rows[29]:
- message = "Temperature in the server room has risen 3 degrees in 1 hour".nowtemp
- sendalert(message)
- setalerted(1)
- elif (rows[0] - 2.49) > rows[24]:
- message = "Temperature in the server room has risen 2.5 degrees in 50 minutes".nowtemp
- sendalert(message)
- setalerted(1)
- elif (rows[0] - 1.99) > rows[19]:
- message = "Temperature in the server room has risen 2 degrees in 40 minutes".nowtemp
- sendalert(message)
- setalerted(1)
- elif (rows[0] - 1.49) > rows[14]:
- message = "Temperature in the server room has risen 1.5 degrees in 30 minutes".nowtemp
- sendalert(message)
- setalerted(1)
- elif (rows[0] - 0.99) > rows[9]:
- message = "Temperature in the server room has risen 1 degree in 20 minutes".nowtemp
- sendalert(message)
- setalerted(1)
- elif (rows[0] - 0.49) > rows[4]:
- message = "Temperature in the server room has risen 0.5 degrees in 10 minutes".nowtemp
- sendalert(message)
- setalerted(1)
- else:
- setalerted(0)
- else:
- if (rows[0] - 2.99) > rows[29]:
- setalerted(1)
- elif (rows[0] - 2.49) > rows[24]:
- setalerted(1)
- elif (rows[0] - 1.99) > rows[19]:
- setalerted(1)
- elif (rows[0] - 1.49) > rows[14]:
- setalerted(1)
- elif (rows[0] - 0.99) > rows[9]:
- setalerted(1)
- elif (rows[0] - 0.49) > rows[4]:
- setalerted(1)
- else:
- setalerted(0)
- db.close()
- def alerted():
- global alerted
- global highalerted
- db = MySQLdb.connect(host="localhost", user="pytemp", passwd="celcius", db="roomtemp")
- cur = db.cursor()
- cur.execute("select yes_no from alerted order by ID desc limit 1")
- alerted = cur.fetchone()[0]
- cur.execute("select yes_no from highalerted order by ID desc limit 1")
- highalerted = cur.fetchone()[0]
- db.close()
- def setalerted(value):
- conn = MySQLdb.connect(host="localhost",user="pytemp",passwd="celcius",db="roomtemp")
- cur = conn.cursor()
- sql = ("""INSERT INTO roomtemp.alerted (yes_no) VALUES (%s)""", (value))
- try:
- cur.execute(*sql)
- conn.commit()
- except:
- conn.rollback()
- def sethighalerted(value):
- conn = MySQLdb.connect(host="localhost",user="pytemp",passwd="celcius",db="roomtemp")
- cur = conn.cursor()
- sql = ("""INSERT INTO roomtemp.highalerted (yes_no) VALUES (%s)""", (value))
- try:
- cur.execute(*sql)
- conn.commit()
- except:
- conn.rollback()
- #temp_post()
- #alerttest()
- testmessage = "This is a test message, nothing to worry about!"
- #sendalert(testmessage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement