Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import time
  3. import random
  4. import MySQLdb
  5. import Adafruit_DHT
  6.  
  7. temp = 0
  8. log = open('/home/pi/Desktop/Python Programming/plant-monitor/logs/PMLog.txt', 'a')
  9.  
  10. class PMTemp:
  11.  
  12. def __init__(self, pmMessage, TEMP_High, TEMP_Low):
  13. print("Loading temp class")
  14. self.pmMessage = pmMessage
  15. self.tmpHigh = TEMP_High
  16. self.tmpLow = TEMP_Low
  17.  
  18. def getTemp(self):
  19. print(time.strftime("%m/%d/%Y %I:%M:%S %p") + " - PMTemp: Reading temperature")
  20. log.write("\n" + time.strftime("%m/%d/%Y %I:%M:%S %p") + " - PMTemp: Reading temperature")
  21. log.flush()
  22. #GET TEMP HERE
  23. humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11,4)
  24. temperature = temperature * 9/5.0 + 32
  25. if humidity is not None and temperature is not None:
  26. temp = "{0:0.1f}".format(temperature) #random.uniform(68.0, 93.0) #91
  27. humid = "{0:0.1f}".format(humidity)
  28. db = MySQLdb.connect("localhost", "root", "gm1RPw", "pm")
  29. curs = db.cursor()
  30. try:
  31. curDate = time.strftime('%Y-%m-%d %H:%M:%S')
  32. sql = "INSERT INTO PMTemp VALUES(NULL, '%s', '%s', '%s')" % (curDate, temp, humid)
  33. #print(sql)
  34. curs.execute(sql)
  35. db.commit()
  36. except:
  37. print("PMTemp: DB comit error, transaction being rolled back")
  38. db.rollback()
  39. #print(str(temperature) + " >= " + str(self.tmpHigh))
  40. if temperature >= self.tmpHigh:
  41. print(time.strftime("%m/%d/%Y %I:%M:%S %p") + " - PMTemp: WARNING HIGH Temp Alert! ")
  42. log.write("\n" + "Temp HIGH warning: The temprature is currently " + str(temp) + " degrees! " + time.strftime("%m/%d/%Y %I:%M:%S %p"))
  43. log.flush()
  44. self.pmMessage.sendMessage("Temp HIGH warning", "The temprature is currently " + str(temp) + " degrees! \r\n " + time.strftime("%m/%d/%Y %I:%M:%S %p"))
  45. if temperature <= self.tmpLow:
  46. print(time.strftime("%m/%d/%Y %I:%M:%S %p") + " - PMTemp: WARNING LOW Temp Alert! ")
  47. log.write("\n" + "Temp LOW warning: The temprature is currently " + str(temp) + " degrees! " + time.strftime("%m/%d/%Y %I:%M:%S %p"))
  48. log.flush()
  49. self.pmMessage.sendMessage("Temp LOW warning", "The temprature is currently " + str(temp) + " degrees! \r\n " + time.strftime("%m/%d/%Y %I:%M:%S %p"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement