Created
June 16, 2016 15:25
-
-
Save nickbalch/f926ad707828c0b73b7aa222c43a67e4 to your computer and use it in GitHub Desktop.
Raspberry Pi GPIO Interrupt to MQTT
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 RPi.GPIO as GPIO | |
import time | |
import paho.mqtt.client as paho | |
GPIO.setmode(GPIO.BCM) | |
#GPIO.setwarnings(False) | |
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # push button | |
print("set up pin") | |
def alert_action(channel): | |
print(time.strftime('%d/%m/%Y %X') + ' Edge detected on channel %s'%channel) | |
time.sleep(0.01) # need to filter out the false positive of some power fluctuation | |
if GPIO.input(19) != GPIO.HIGH: | |
print('Quitting event handler because this was probably a false positive') | |
return | |
mosq.publish("home/alert/doorbell",time.strftime('%x %X')) | |
mosq = paho.Client() | |
mosq.connect("raspberrypi.local") | |
mosq.loop_start() | |
GPIO.add_event_detect(19, GPIO.RISING, callback=alert_action, bouncetime=200) | |
while True: | |
time.sleep(1) | |
GPIO.cleanup() | |
mosq.discconect() | |
mosq.loop_stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With thanks to the tutorials on http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio