Advertisement
BlakeHepner

Doorbell python

Jul 20th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import subprocess
  3. import time
  4. import pynma
  5. from pynma import PyNMA
  6.  
  7. ledPin = 18
  8. buttonPin = 23
  9. holdTime = 2
  10. tapTime = 0.01
  11. nextInterval = 0.0
  12. lastId = 1
  13. doorapi = pynma.PyNMA("pynma_api_key_goes_here")
  14.  
  15. def tap():
  16.     GPIO.output(ledPin, GPIO.HIGH)
  17.     doorapi.push("Door", "Doorbell", "Ding Dong!", "/home/pinkythepig/HA/Testfile.png")
  18.     time.sleep(3)
  19.     GPIO.output(ledPin, GPIO.LOW)
  20.  
  21. def hold():
  22.     GPIO.output(ledPin, GPIO.HIGH)
  23.     doorapi.push("Door", "Doorbell", "Ding Dong! Button was held", "/home/pinkythepig/HA/Testfile.png")
  24.     time.sleep(3)
  25.     GPIO.output(ledPin, GPIO.LOW)
  26.  
  27. GPIO.setmode(GPIO.BCM)
  28. GPIO.setup(ledPin, GPIO.OUT)
  29. GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  30. GPIO.output(ledPin, GPIO.HIGH)
  31. prevButtonState = GPIO.input(buttonPin)
  32. prevTime        = time.time()
  33. tapEnable       = False
  34. holdEnable      = False
  35.  
  36. while(True):
  37.  
  38.   time.sleep(0.15)
  39.   # Poll current button state and time
  40.   buttonState = GPIO.input(buttonPin)
  41.   t = time.time()
  42.  
  43.   # Has button state changed?
  44.   if buttonState != prevButtonState:
  45.     prevButtonState = buttonState   # Yes, save new state/time
  46.     prevTime = t
  47.   else:                             # Button state unchanged
  48.     if (t - prevTime) >= holdTime:  # Button held more than 'holdTime'?
  49.       # Yes it has.  Is the hold action as-yet untriggered?
  50.       if holdEnable == True:        # Yep!
  51.         hold()                      # Perform hold action (usu. shutdown)
  52.         holdEnable = False          # 1 shot...don't repeat hold action
  53.         tapEnable  = False          # Don't do tap action on release
  54.     elif (t - prevTime) >= tapTime: # Not holdTime.  tapTime elapsed?
  55.       # Yes.  Debounced press or release...
  56.       if buttonState == True:       # Button released?
  57.         if tapEnable == True:       # Ignore if prior hold()
  58.           tap()                     # Tap triggered (button released)
  59.           tapEnable  = False        # Disable tap and hold
  60.           holdEnable = False
  61.       else:                         # Button pressed
  62.         tapEnable  = True           # Enable tap and hold actions
  63.         holdEnable = True
  64.  
  65.   # LED blinks while idle, for a brief interval every 2 seconds.
  66.   # Pin 18 is PWM-capable and a "sleep throb" would be nice, but
  67.   # the PWM-related library is a hassle for average users to install
  68.   # right now.  Might return to this later when it's more accessible.
  69.   if ((int(t) & 1) == 0) and ((t - int(t)) < 0.15):
  70.     GPIO.output(ledPin, GPIO.HIGH)
  71.   else:
  72.     GPIO.output(ledPin, GPIO.LOW)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement