Last active
March 18, 2017 21:49
-
-
Save mlutfy/527918fc6216abfb494d1f146b32e8b4 to your computer and use it in GitHub Desktop.
Door status
This file contains 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
import os | |
import RPi.GPIO as GPIO | |
import time | |
import http.client, urllib | |
# Based on: | |
# http://raspberry.io/projects/view/reading-and-writing-from-gpio-ports-from-python/ | |
GPIO.setmode(GPIO.BCM) | |
# LED will be turned on when door is closed. | |
GPIO.setup(18, GPIO.OUT, initial=GPIO.LOW) | |
# Balcony door detector | |
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) | |
GPIO.add_event_detect(6, GPIO.BOTH) | |
# Note that we keep the door status to debounce the input | |
def balcony_callback(pin): | |
val = GPIO.input(pin) | |
if (balcony_callback.status != val): | |
balcony_callback.status = val | |
GPIO.output(18, val) | |
message = "DOOR-" + str(pin) + " is " + ("closed" if val else "open") | |
send_notification(message) | |
balcony_callback.status = GPIO.input(6) | |
print("DOOR-6 is initially:", balcony_callback.status) | |
def send_notification(message): | |
print(message) | |
conn = http.client.HTTPSConnection("api.pushover.net:443") | |
conn.request("POST", "/1/messages.json", | |
urllib.parse.urlencode({ | |
"user": "XXXXXXXXXXXXXXXX", | |
"token": "YYYYYYYYYYYYYYYY", | |
"message": message, | |
}), { "Content-type": "application/x-www-form-urlencoded" }) | |
response = conn.getresponse() | |
print(response.read()) | |
conn.close() | |
GPIO.add_event_callback(6, balcony_callback) | |
try: | |
while True: | |
time.sleep(10) | |
except KeyboardInterrupt: | |
print("Quitting..") | |
print("Bye. On eteint!") | |
GPIO.output(18,GPIO.LOW) | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment