Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- ##############################################################################################################################
- from time import sleep
- ##############################################################################################################################
- from datetime import datetime
- ##############################################################################################################################
- import MySQLdb
- import threading
- ##############################################################################################################################
- class Database():
- def __init__(self):
- self.mysql_db_name = 'lpg'
- self.mysql_db_address = '192.168.1.100'
- self.mysql_db_username = 'lpg'
- self.mysql_db_password = 'lpg'
- self.conn = MySQLdb.Connection(host=self.mysql_db_address, user=self.mysql_db_username, passwd=self.mysql_db_password, db=self.mysql_db_name)
- self.conn.autocommit(True)
- self.cursor = self.conn.cursor()
- def close(self):
- self.cursor.close()
- self.conn.close()
- def execute(self, query, params):
- self.cursor.execute(query, (params))
- return self.cursor.fetchall()
- def execute_dict(self, query, params={}):
- self.cursor = self.conn.cursor(MySQLdb.cursors.DictCursor)
- try:
- self.cursor.execute(query, params)
- except MySQLdb.Error, e:
- print "[-] Error: " + str(e)
- return self.cursor.fetchall()
- ##############################################################################################################################
- def readSensor(senor_path):
- with open(senor_path, 'r') as sensorfile:
- tempdata=sensorfile.read().split("\n")[1].split(" ")[9]
- if tempdata:
- return float(tempdata[2:]) / 1000
- return -1
- ##############################################################################################################################
- while True:
- now = datetime.now()
- ##############################################################################################################################
- temperature_data = {
- 'inside': readSensor('/sys/bus/w1/devices/28-000005060735/w1_slave'),
- 'outside': readSensor('/sys/bus/w1/devices/28-0115811dc7ff/w1_slave'),
- 'date': str(now.year) + "-" + str(now.month) + "-" + str(now.day),
- 'time': str(now.hour) + ":" + str(now.minute) + ":" + str(now.second)
- }
- ##############################################################################################################################
- print "Water Cylinder Temp is: " + str(temperature_data['inside'])
- print "Outside Temp is: " + str(temperature_data['outside'])
- ##############################################################################################################################
- db = Database()
- db.execute_dict("INSERT INTO lpgmonitor (date, time, lpgc, outside) VALUES (%(date)s, %(time)s, %(inside)s, %(outside)s)", temperature_data)
- db.close()
- ##############################################################################################################################
- def f():
- threading.Timer(320, f).start()
- alert = float(temperature_data['outside'])
- if alert > 45:
- #If True run below, otherwise go to Else statment
- #cat /home/adydas/alert.txt | ssmtp cliffy.chapman@gmail.com 2>&1
- #cat /home/adydas/order.txt | ssmtp TBC@Nova 2>&1
- print("Temp Wrong, Sent Email")
- f()
- else:
- print("Temp normal, no need to send email")
- ##############################################################################################################################
- print "Sleeping for 60 seconds."
- sleep(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement