Created
November 8, 2018 17:30
-
-
Save aderbas/22de3d9d8a5e3b39bbbc51e76918dbf3 to your computer and use it in GitHub Desktop.
Listener sensor reed switch with Raspberry Pi GPIO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import RPi.GPIO as GPIO | |
import time | |
import sys | |
import signal | |
GPIO.setmode(GPIO.BCM) | |
DOOR_SENSOR_PIN = 18 | |
isOpen = None | |
oldIsOpen = None | |
GPIO.setup(DOOR_SENSOR_PIN, GPIO.IN, pull_up_down = GPIO.PUD_UP) | |
def cleanupLights(signal, frame): | |
GPIO.cleanup() | |
sys.exit(0) | |
def expiredTime(sig, frame): | |
print('Door opened timeout') | |
signal.alarm(0) | |
signal.signal(signal.SIGALRM, expiredTime) | |
signal.signal(signal.SIGINT, cleanupLights) | |
while True: | |
oldIsOpen = isOpen | |
isOpen = GPIO.input(DOOR_SENSOR_PIN) | |
if (isOpen and (isOpen != oldIsOpen)): | |
print('Door open') | |
signal.alarm(10) | |
elif (isOpen != oldIsOpen): | |
print('Door close') | |
signal.alarm(0) | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment