Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime as dt
- import RPi.GPIO as GPIO
- import time
- from elapse import ElapseSeconds
- class WaterSchedule:
- def __init__(self):
- self.status = False
- self.cycle = 0
- self.cycle_duration = 0
- self.log_file = "logs/log_water.txt"
- def setup(self):
- # Water
- GPIO.setup(21, GPIO.OUT)
- # initialize to 0#
- GPIO.output(21, GPIO.HIGH)
- # initialize to first cycle
- self.cycle = 1
- # log system startup
- self.log("SYSTEM START: WATER")
- def log(self, msg):
- status = self.status
- cycle = self.cycle
- duration = self.cycle_duration
- cur_time = dt.datetime.now()
- cur_time = cur_time.strftime('%Y/%m/%d %H:%M:%S')
- # log
- pin_21 = "0" if GPIO.input(21) else "1"
- log_msg = "[ {:>22} ] : [ {} ] Status: {:1}, Cycle: {}, Duration: {:2}\n".format(msg, cur_time, status, cycle, duration)
- log_msg += "[ {:>22} ] : [ {:2} {:1} ]\n".format("PIN", 21, pin_21)
- log_file = open(self.log_file, "a")
- log_file.write(log_msg)
- log_file.close()
- # use these for debugging
- #print "[ {:>22} ] : [ {} ] Status: {:1}, Cycle: {}, Duration: {:2}".format(msg, cur_time, status, cycle, duration)
- #print "[ {:>22} ] : [ {:2} {:1} ]".format("PIN", 21, pin_21)
- def start(self, cycle, duration):
- # set status
- self.status = True
- self.cycle = cycle
- self.cycle_duration = duration
- # set relay on
- GPIO.output(21, GPIO.LOW)
- # call log
- self.log("WATER ON")
- def stop(self):
- # set status
- self.status = False
- # set relay off
- GPIO.output(21, GPIO.HIGH)
- # call log
- self.log("WATER OFF")
- def update(self):
- # get time
- cur_time = dt.datetime.now()
- cur_hour = cur_time.hour
- cur_minute = cur_time.minute
- # check hour
- if cur_hour == 14:
- # check cycle and time
- if self.cycle == 1 and cur_minute == 05:
- # turn on
- self.start(1, 2)
- # sleep
- time.sleep(2)
- # turn off
- self.stop()
- # next cycle: 2
- self.cycle = 2
- elif self.cycle == 2 and cur_minute == 15:
- # turn on
- self.start(2, 2)
- # sleep
- time.sleep(2)
- # turn off
- self.stop()
- # next cycle: 3
- self.cycle = 3
- elif self.cycle == 3 and cur_minute == 30:
- # turn on
- self.start(3, 2)
- # sleep
- time.sleep(2)
- # turn off
- self.stop()
- # next cycle: 4
- self.cycle = 4
- elif self.cycle == 4 and cur_minute == 45:
- # turn on
- self.start(4, 2)
- # sleep
- time.sleep(2)
- # turn off
- self.stop()
- # next cycle: 1
- self.cycle = 1
- else:
- # log
- self.log("WATER LOG")
- else:
- # log
- self.log("WATER LOG")
- def display(self):
- if self.status:
- display = "WATER: ON\n"
- else:
- display = "WATER: OFF\n"
- display += "CYCLE: {}".format(self.cycle)
- return display
Add Comment
Please, Sign In to add comment