Skip to content

Instantly share code, notes, and snippets.

@nickbalch
Created June 16, 2016 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickbalch/f926ad707828c0b73b7aa222c43a67e4 to your computer and use it in GitHub Desktop.
Save nickbalch/f926ad707828c0b73b7aa222c43a67e4 to your computer and use it in GitHub Desktop.
Raspberry Pi GPIO Interrupt to MQTT
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()
@nickbalch
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment