Guest User

Untitled

a guest
Aug 11th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. import datetime as dt
  2. import RPi.GPIO as GPIO
  3. import time
  4.  
  5. from elapse import ElapseSeconds
  6.  
  7. class WaterSchedule:
  8.  
  9. def __init__(self):
  10.  
  11. self.status = False
  12. self.cycle = 0
  13. self.cycle_duration = 0
  14.  
  15. self.log_file = "logs/log_water.txt"
  16.  
  17. def setup(self):
  18. # Water
  19. GPIO.setup(21, GPIO.OUT)
  20.  
  21. # initialize to 0#
  22. GPIO.output(21, GPIO.HIGH)
  23.  
  24. # initialize to first cycle
  25. self.cycle = 1
  26.  
  27. # log system startup
  28. self.log("SYSTEM START: WATER")
  29.  
  30. def log(self, msg):
  31. status = self.status
  32. cycle = self.cycle
  33. duration = self.cycle_duration
  34.  
  35. cur_time = dt.datetime.now()
  36. cur_time = cur_time.strftime('%Y/%m/%d %H:%M:%S')
  37.  
  38. # log
  39. pin_21 = "0" if GPIO.input(21) else "1"
  40.  
  41. log_msg = "[ {:>22} ] : [ {} ] Status: {:1}, Cycle: {}, Duration: {:2}\n".format(msg, cur_time, status, cycle, duration)
  42. log_msg += "[ {:>22} ] : [ {:2} {:1} ]\n".format("PIN", 21, pin_21)
  43.  
  44. log_file = open(self.log_file, "a")
  45. log_file.write(log_msg)
  46. log_file.close()
  47.  
  48. # use these for debugging
  49. #print "[ {:>22} ] : [ {} ] Status: {:1}, Cycle: {}, Duration: {:2}".format(msg, cur_time, status, cycle, duration)
  50. #print "[ {:>22} ] : [ {:2} {:1} ]".format("PIN", 21, pin_21)
  51.  
  52. def start(self, cycle, duration):
  53.  
  54. # set status
  55. self.status = True
  56. self.cycle = cycle
  57. self.cycle_duration = duration
  58.  
  59. # set relay on
  60. GPIO.output(21, GPIO.LOW)
  61.  
  62. # call log
  63. self.log("WATER ON")
  64.  
  65. def stop(self):
  66.  
  67. # set status
  68. self.status = False
  69.  
  70. # set relay off
  71. GPIO.output(21, GPIO.HIGH)
  72.  
  73. # call log
  74. self.log("WATER OFF")
  75.  
  76. def update(self):
  77. # get time
  78. cur_time = dt.datetime.now()
  79. cur_hour = cur_time.hour
  80. cur_minute = cur_time.minute
  81.  
  82. # check hour
  83. if cur_hour == 14:
  84.  
  85. # check cycle and time
  86. if self.cycle == 1 and cur_minute == 05:
  87.  
  88. # turn on
  89. self.start(1, 2)
  90. # sleep
  91. time.sleep(2)
  92. # turn off
  93. self.stop()
  94. # next cycle: 2
  95. self.cycle = 2
  96.  
  97. elif self.cycle == 2 and cur_minute == 15:
  98.  
  99. # turn on
  100. self.start(2, 2)
  101. # sleep
  102. time.sleep(2)
  103. # turn off
  104. self.stop()
  105. # next cycle: 3
  106. self.cycle = 3
  107.  
  108. elif self.cycle == 3 and cur_minute == 30:
  109.  
  110. # turn on
  111. self.start(3, 2)
  112. # sleep
  113. time.sleep(2)
  114. # turn off
  115. self.stop()
  116. # next cycle: 4
  117. self.cycle = 4
  118.  
  119. elif self.cycle == 4 and cur_minute == 45:
  120.  
  121. # turn on
  122. self.start(4, 2)
  123. # sleep
  124. time.sleep(2)
  125. # turn off
  126. self.stop()
  127. # next cycle: 1
  128. self.cycle = 1
  129.  
  130. else:
  131.  
  132. # log
  133. self.log("WATER LOG")
  134.  
  135. else:
  136.  
  137. # log
  138. self.log("WATER LOG")
  139.  
  140.  
  141. def display(self):
  142. if self.status:
  143. display = "WATER: ON\n"
  144. else:
  145. display = "WATER: OFF\n"
  146.  
  147. display += "CYCLE: {}".format(self.cycle)
  148.  
  149. return display
Add Comment
Please, Sign In to add comment