Guest User

Untitled

a guest
Aug 11th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. import datetime as dt
  2. import RPi.GPIO as GPIO
  3. import Adafruit_DHT as Adafruit_DHT
  4.  
  5. class CoolingSchedule:
  6.  
  7. def __init__(self):
  8.  
  9. self.temp_s = None
  10. self.temp_p = None
  11.  
  12. self.humid_s = None
  13. self.humid_p = None
  14.  
  15. self.status = False
  16.  
  17. self.log_file = "logs/log_cooling.txt"
  18.  
  19. def setup(self):
  20.  
  21. # cooling
  22. GPIO.setup(26, GPIO.OUT)
  23.  
  24. # initialize to 0
  25. GPIO.output(26, GPIO.HIGH)
  26.  
  27. # log system startup
  28. self.log("SYSTEM START: COOLING")
  29.  
  30. def log(self, msg):
  31.  
  32. # get time
  33. cur_time = dt.datetime.now()
  34. cur_time = cur_time.strftime("%Y/%m/%d %H:%M:%S")
  35.  
  36. # get variables
  37. status = self.status
  38. temp = self.temp_s
  39. humid = self.humid_s
  40.  
  41. # log
  42. pin_26 = "0" if GPIO.input(26) else "1"
  43.  
  44. log_msg = "[ {:>22} ] : [ {} ] Status: {:1}, Temp: {}, Humid: {}%".format(msg, cur_time, status, temp, humid)
  45. log_msg += "[ {:>22} ] : [ {:2} {:1} ]\n".format("Pin", 26, pin_26)
  46.  
  47. log_file = open(self.log_file, "a")
  48. log_file.write(log_msg)
  49. log_file.close()
  50.  
  51. # use these for debugging
  52. #print "[ {:>22} ] : [ {} ] Status: {:1}, Temp: {}, Humid: {}%".format(msg, cur_time, status, temp, humid)
  53. #print "[ {:>22} ] : [ {:2} {:1} ]".format("PIN", 26, pin_26)
  54.  
  55. def start(self):
  56.  
  57. # set status
  58. self.status = True
  59.  
  60. # set relay on
  61. GPIO.output(26, GPIO.LOW)
  62.  
  63. # call log
  64. self.log("COOLING ON")
  65.  
  66. def stop(self):
  67.  
  68. # set status
  69. self.status = False
  70.  
  71. # set relay off
  72. GPIO.output(26, GPIO.HIGH)
  73.  
  74. # call log
  75. self.log("COOLING OFF")
  76.  
  77. def update(self):
  78.  
  79. # get temperature and humidity
  80. humid, temp = Adafruit_DHT.read_retry(Adafruit_DHT.AM2302, 17)
  81.  
  82. # check value exists
  83. if temp is not None and humid is not None:
  84.  
  85. # calculate farenheit
  86. temp = temp * 9 / 5.0 + 32
  87.  
  88. # check if temp has been saved
  89. if self.temp_p == None or self.humid_p == None:
  90.  
  91. # set initial value
  92. self.temp_s = "{:.2f}".format(temp)
  93. self.temp_p = temp
  94.  
  95. self.humid_s = "{:.2f}".format(humid)
  96. self.humid_p = humid
  97.  
  98. # saved initial value, skip the rest
  99. return
  100.  
  101. # at this point, temp has been checked more than once
  102. # check margin (faulty reading), within 3 degrees
  103. margin = abs(self.temp_p - temp)
  104. if margin < 5:
  105.  
  106. # check status
  107. if not self.status and temp > 77:
  108.  
  109. # start
  110. self.start()
  111.  
  112. elif self.status and temp < 73:
  113.  
  114. # stop
  115. self.stop()
  116.  
  117. else:
  118.  
  119. # log
  120. self.log("COOLING LOG")
  121.  
  122. self.temp_s = "{:.2f}".format(temp)
  123. self.temp_p = temp
  124.  
  125. self.humid_s = "{:.2f}".format(humid)
  126. self.humid_p = humid
  127.  
  128.  
  129. def display(self):
  130. display = "TEMP: {:5}\n".format(self.temp_p)
  131.  
  132. return display
Add Comment
Please, Sign In to add comment