import RPi.GPIO as GPIO import logging import time from threading import Thread import cherrypy # https://docs.cherrypy.org/en/latest/tutorials.html#tutorial-1-a-basic-web-application cherrypy.config.update({ 'server.socket_host' : '192.168.178.49', 'server.socket_port' : 8081, }) logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s - %(message)s') PIN_MOTOR_BUTTON = 23 PIN_REMOTE_BUTTON = 24 PIN_MOTOR = 21 PIN_REMOTE = 26 PIN_LED = 11 GPIO.setmode(GPIO.BCM) GPIO.setup(PIN_MOTOR_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(PIN_REMOTE_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(PIN_MOTOR, GPIO.OUT) GPIO.setup(PIN_REMOTE, GPIO.OUT) GPIO.setup(PIN_LED, GPIO.OUT) def motor_button(channel): time.sleep(0.01) # el cheapo relay module creates lots of interference, so we check again after a waiting period (see https://electronics.stackexchange.com/a/361793/253533) if channel == 0 or GPIO.input(PIN_MOTOR_BUTTON) == False: logging.info("trigger motor") GPIO.output(PIN_MOTOR, GPIO.LOW) GPIO.output(PIN_LED, GPIO.HIGH) time.sleep(0.4) GPIO.output(PIN_MOTOR, GPIO.HIGH) GPIO.output(PIN_LED, GPIO.LOW) else: logging.info("flare (motor button)") def remote_button(channel): time.sleep(0.01) if channel == 0 or GPIO.input(PIN_REMOTE_BUTTON) == False: logging.info("trigger remote 0.8") GPIO.output(PIN_REMOTE, GPIO.HIGH) GPIO.output(PIN_LED, GPIO.HIGH) time.sleep(0.8) GPIO.output(PIN_REMOTE, GPIO.LOW) GPIO.output(PIN_LED, GPIO.LOW) else: logging.info("flare (remote button)") GPIO.add_event_detect(PIN_MOTOR_BUTTON, GPIO.FALLING, callback=motor_button, bouncetime=250) GPIO.add_event_detect(PIN_REMOTE_BUTTON, GPIO.FALLING, callback=remote_button, bouncetime=250) GPIO.output(PIN_MOTOR, GPIO.HIGH) GPIO.output(PIN_REMOTE, GPIO.LOW) GPIO.output(PIN_LED, GPIO.LOW) class GarageService(object): @cherrypy.expose def index(self): return "Hello World!" @cherrypy.expose def motor(self): Thread(target=motor_button,args=[0]).start() return "trigger motor" @cherrypy.expose def remote(self): Thread(target=remote_button,args=[0]).start() return "trigger remote" @cherrypy.expose def all(self): Thread(target=motor_button,args=[0]).start() Thread(target=remote_button,args=[0]).start() return "trigger all" cherrypy.quickstart(GarageService()) logging.info('Shutdown') logging.info('Reset GPIO configuration and close') GPIO.cleanup()