SHARE
TWEET

Untitled

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