Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Raspberry Pi custom Christmas light timer
- from RPi import GPIO
- """
- Setup GPIO pins as outputs
- This convention is for the "P1" header pin convention
- where the pins start with P1 in the upper left and go
- to P26 in the lower right, with odds in the left column
- and evens in the right column.
- So, pins P1-11 and P1-12 correspond to GPIO17 and GPIO18
- respectively.
- """
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(11, GPIO.OUT)
- GPIO.setup(12, GPIO.OUT)
- from time import sleep
- from datetime import time, datetime
- """
- LIST OF DAYS OF WEEK IN STRING FORMAT
- """
- DOW = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
- class XMasTimer(object):
- def __init__(self):
- """
- Set a "wait time" in seconds. This ensures that the program pauses
- briefly after it turns the lights on or off. Otherwise, since the
- loop will execute more than once a second, it will try to keep
- turning the lights on when they are already on (or off when they are
- already off.
- """
- self.wait_time = 3
- """
- Enter the times you want the lights to turn on and off for
- each day of the week. Default is for lights to turn on at
- 5:30pm and off at 10:30pm on weekdays, on at 5:00pm and off
- at 11:30pm on weekends. Note that this is using a 24-hour clock.
- """
- self.times = {
- 'Monday': {
- 'on': time(hour=17, minute=30, second=0),
- 'off': time(hour=22, minute=30, second=0),
- },
- 'Tuesday': {
- 'on': time(hour=17, minute=30, second=0),
- 'off': time(hour=22, minute=30, second=0),
- },
- 'Wednesday': {
- 'on': time(hour=17, minute=30, second=0),
- 'off': time(hour=22, minute=30, second=0),
- },
- 'Thursday': {
- 'on': time(hour=17, minute=30, second=0),
- 'off': time(hour=22, minute=30, second=0),
- },
- 'Friday': {
- 'on': time(hour=17, minute=30, second=0),
- 'off': time(hour=22, minute=30, second=0),
- },
- 'Saturday': {
- 'on': time(hour=17, minute=0, second=0),
- 'off': time(hour=23, minute=30, second=0),
- },
- 'Sunday':{
- 'on': time(hour=17, minute=0, second=0),
- 'off': time(hour=23, minute=30, second=0),
- }
- }
- self.loop()
- def loop(self):
- while True:
- action = self.getAction()
- if action == 'ON':
- self.perform('ON')
- elif action == "OFF":
- self.perform('OFF')
- def currentDay(self):
- return self.times[DOW[datetime.now().weekday()]]
- def getAction(self):
- day = self.currentDay()
- now = datetime.now()
- if now.hour == day['on'].hour and \
- now.minute == day['on'].minute and \
- now.second == day['on'].second:
- return 'ON'
- elif now.hour == day['off'].hour and \
- now.minute == day['off'].minute and \
- now.second == day['off'].second:
- return 'OFF'
- return None
- def perform(self, action):
- pen = 11 if action is 'ON' else 12
- GPIO.output(pen, GPIO.HIGH)
- sleep(.5)
- GPIO.output(pen, GPIO.LOW)
- sleep(self.wait_time)
- if __name__ == "__main__":
- XMasTimer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement