Guest User

python gpio sensor

a guest
Dec 22nd, 2021
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. import sys
  4. import time
  5. import pigpio
  6. import logging
  7. import datetime
  8. import subprocess
  9.  
  10. logging.basicConfig(level=logging.INFO, format='[%(levelname)s]  %(asctime)s - %(message)s',)
  11.  
  12. def do_watcher():
  13.     pi = pigpio.pi('pigpiod', 8888)
  14.     if not pi.connected:
  15.            exit()
  16.  
  17.     debounce = 1000
  18.  
  19.     pi.set_mode(18, pigpio.INPUT)
  20.     pi.set_glitch_filter(18, debounce)
  21.     cb1 = pi.callback(18, pigpio.RISING_EDGE, button_main)
  22.  
  23.     logging.info("PiGPIO Conected");
  24.  
  25.     try:
  26.         while True:
  27.             time.sleep(1)
  28.     except KeyboardInterrupt:
  29.         cb1.cancel()
  30.  
  31. def button_main(gpio, level, tick):
  32.     """CALLBACK:  Respond when the main button is pressed """
  33.  
  34.     logging.info("button main pressed")
  35.     subprocess.call("/webhook.sh ", shell=True)
  36.    
  37. if __name__ == "__main__":
  38.     logging.info ("Starting button watcher");
  39.     do_watcher()
Advertisement
Add Comment
Please, Sign In to add comment