SHARE
TWEET

XMasTimer Raspberry PI

a guest Dec 12th, 2013 720 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Raspberry Pi custom Christmas light timer
  2.  
  3. from RPi import GPIO
  4.  
  5. """
  6. Setup GPIO pins as outputs
  7. This convention is for the "P1" header pin convention
  8. where the pins start with P1 in the upper left and go
  9. to P26 in the lower right, with odds in the left column
  10. and evens in the right column.
  11.  
  12. So, pins P1-11 and P1-12 correspond to GPIO17 and GPIO18
  13. respectively.
  14. """
  15. GPIO.setmode(GPIO.BOARD)
  16. GPIO.setup(11, GPIO.OUT)
  17. GPIO.setup(12, GPIO.OUT)
  18.  
  19.  
  20. from time import sleep
  21. from datetime import time, datetime
  22.  
  23. """
  24. LIST OF DAYS OF WEEK IN STRING FORMAT
  25. """
  26. DOW = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
  27.  
  28. class XMasTimer(object):
  29.     def __init__(self):
  30.         """
  31.        Set a "wait time" in seconds. This ensures that the program pauses
  32.        briefly after it turns the lights on or off. Otherwise, since the
  33.        loop will execute more than once a second, it will try to keep
  34.        turning the lights on when they are already on (or off when they are
  35.        already off.
  36.        """
  37.         self.wait_time = 3
  38.         """
  39.        Enter the times you want the lights to turn on and off for
  40.        each day of the week. Default is for lights to turn on at
  41.        5:30pm and off at 10:30pm on weekdays, on at 5:00pm and off
  42.        at 11:30pm on weekends. Note that this is using a 24-hour clock.
  43.        """
  44.         self.times = {
  45.             'Monday': {
  46.                 'on': time(hour=17, minute=30, second=0),
  47.                 'off': time(hour=22, minute=30, second=0),
  48.             },
  49.             'Tuesday': {
  50.                 'on': time(hour=17, minute=30, second=0),
  51.                 'off': time(hour=22, minute=30, second=0),
  52.             },
  53.             'Wednesday': {
  54.                 'on': time(hour=17, minute=30, second=0),
  55.                 'off': time(hour=22, minute=30, second=0),
  56.             },
  57.             'Thursday': {
  58.                 'on': time(hour=17, minute=30, second=0),
  59.                 'off': time(hour=22, minute=30, second=0),
  60.             },
  61.             'Friday': {
  62.                 'on': time(hour=17, minute=30, second=0),
  63.                 'off': time(hour=22, minute=30, second=0),
  64.             },
  65.             'Saturday': {
  66.                 'on': time(hour=17, minute=0, second=0),
  67.                 'off': time(hour=23, minute=30, second=0),
  68.             },
  69.             'Sunday':{
  70.                 'on': time(hour=17, minute=0, second=0),
  71.                 'off': time(hour=23, minute=30, second=0),
  72.             }
  73.         }
  74.         self.loop()
  75.  
  76.     def loop(self):
  77.         while True:
  78.             action = self.getAction()
  79.             if action == 'ON':
  80.                 self.perform('ON')
  81.             elif action == "OFF":
  82.                 self.perform('OFF')
  83.  
  84.     def currentDay(self):
  85.         return self.times[DOW[datetime.now().weekday()]]
  86.  
  87.     def getAction(self):
  88.         day = self.currentDay()
  89.         now = datetime.now()
  90.  
  91.         if now.hour == day['on'].hour and \
  92.            now.minute == day['on'].minute and \
  93.            now.second == day['on'].second:
  94.             return 'ON'
  95.         elif now.hour == day['off'].hour and \
  96.              now.minute == day['off'].minute and \
  97.              now.second == day['off'].second:
  98.                 return 'OFF'
  99.         return None
  100.  
  101.     def perform(self, action):
  102.         pen = 11 if action is 'ON' else 12
  103.         GPIO.output(pen, GPIO.HIGH)
  104.         sleep(.5)
  105.         GPIO.output(pen, GPIO.LOW)
  106.         sleep(self.wait_time)
  107.  
  108.  
  109. if __name__ == "__main__":
  110.     XMasTimer()
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top