Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. #!/usr/bin/python
  2. ##############################################################################################################################
  3. from time import sleep
  4. ##############################################################################################################################
  5. from datetime import datetime
  6. ##############################################################################################################################
  7. import MySQLdb
  8. import threading
  9. ##############################################################################################################################
  10. class Database():
  11. def __init__(self):
  12. self.mysql_db_name = 'lpg'
  13. self.mysql_db_address = '192.168.1.100'
  14. self.mysql_db_username = 'lpg'
  15. self.mysql_db_password = 'lpg'
  16. self.conn = MySQLdb.Connection(host=self.mysql_db_address, user=self.mysql_db_username, passwd=self.mysql_db_password, db=self.mysql_db_name)
  17. self.conn.autocommit(True)
  18. self.cursor = self.conn.cursor()
  19.  
  20. def close(self):
  21. self.cursor.close()
  22. self.conn.close()
  23.  
  24. def execute(self, query, params):
  25. self.cursor.execute(query, (params))
  26. return self.cursor.fetchall()
  27.  
  28. def execute_dict(self, query, params={}):
  29. self.cursor = self.conn.cursor(MySQLdb.cursors.DictCursor)
  30. try:
  31. self.cursor.execute(query, params)
  32. except MySQLdb.Error, e:
  33. print "[-] Error: " + str(e)
  34. return self.cursor.fetchall()
  35.  
  36. ##############################################################################################################################
  37.  
  38. def readSensor(senor_path):
  39. with open(senor_path, 'r') as sensorfile:
  40. tempdata=sensorfile.read().split("\n")[1].split(" ")[9]
  41. if tempdata:
  42. return float(tempdata[2:]) / 1000
  43. return -1
  44.  
  45. ##############################################################################################################################
  46.  
  47. while True:
  48. now = datetime.now()
  49.  
  50. ##############################################################################################################################
  51.  
  52. temperature_data = {
  53. 'inside': readSensor('/sys/bus/w1/devices/28-000005060735/w1_slave'),
  54. 'outside': readSensor('/sys/bus/w1/devices/28-0115811dc7ff/w1_slave'),
  55. 'date': str(now.year) + "-" + str(now.month) + "-" + str(now.day),
  56. 'time': str(now.hour) + ":" + str(now.minute) + ":" + str(now.second)
  57. }
  58.  
  59. ##############################################################################################################################
  60.  
  61. print "Water Cylinder Temp is: " + str(temperature_data['inside'])
  62. print "Outside Temp is: " + str(temperature_data['outside'])
  63.  
  64. ##############################################################################################################################
  65.  
  66. db = Database()
  67. db.execute_dict("INSERT INTO lpgmonitor (date, time, lpgc, outside) VALUES (%(date)s, %(time)s, %(inside)s, %(outside)s)", temperature_data)
  68. db.close()
  69.  
  70. ##############################################################################################################################
  71. def f():
  72. threading.Timer(320, f).start()
  73.  
  74. alert = float(temperature_data['outside'])
  75. if alert > 45:
  76. #If True run below, otherwise go to Else statment
  77. #cat /home/adydas/alert.txt | ssmtp cliffy.chapman@gmail.com 2>&1
  78. #cat /home/adydas/order.txt | ssmtp TBC@Nova 2>&1
  79. print("Temp Wrong, Sent Email")
  80. f()
  81. else:
  82. print("Temp normal, no need to send email")
  83.  
  84. ##############################################################################################################################
  85. print "Sleeping for 60 seconds."
  86. sleep(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement