Files
smart-home/garage/garage.py
T
2020-12-08 17:23:38 +01:00

116 lines
3.3 KiB
Python

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_GARAGE_BUTTON = 23
PIN_GATE_BUTTON = 24
PIN_GARAGE = 21
PIN_GATE = 26
PIN_LED = 11
PIN_DOOR_OPEN = 13
PIN_DOOR_CLOSED = 6
PIN_SENSOR_GATE= 19
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_GARAGE_BUTTON, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(PIN_GATE_BUTTON, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(PIN_GARAGE, GPIO.OUT)
GPIO.setup(PIN_GATE, GPIO.OUT)
GPIO.setup(PIN_LED, GPIO.OUT)
GPIO.setup(PIN_DOOR_OPEN, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(PIN_DOOR_CLOSED, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(PIN_SENSOR_GATE, GPIO.IN, GPIO.PUD_UP)
def garage_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_GARAGE_BUTTON) == False:
logging.info("trigger motor")
GPIO.output(PIN_GARAGE, GPIO.LOW)
GPIO.output(PIN_LED, GPIO.HIGH)
time.sleep(0.4)
GPIO.output(PIN_GARAGE, GPIO.HIGH)
GPIO.output(PIN_LED, GPIO.LOW)
else:
logging.info("flare (motor button)")
def gate_button(channel):
time.sleep(0.01)
if channel == 0 or GPIO.input(PIN_GATE_BUTTON) == False:
logging.info("trigger remote")
GPIO.output(PIN_GATE, GPIO.HIGH)
GPIO.output(PIN_LED, GPIO.HIGH)
time.sleep(0.4)
GPIO.output(PIN_GATE, GPIO.LOW)
GPIO.output(PIN_LED, GPIO.LOW)
else:
logging.info("flare (remote button)")
GPIO.add_event_detect(PIN_GARAGE_BUTTON, GPIO.FALLING, callback=garage_button, bouncetime=250)
GPIO.add_event_detect(PIN_GATE_BUTTON, GPIO.FALLING, callback=gate_button, bouncetime=250)
GPIO.output(PIN_GARAGE, GPIO.HIGH)
GPIO.output(PIN_GATE, GPIO.LOW)
GPIO.output(PIN_LED, GPIO.LOW)
class GarageService(object):
@cherrypy.expose
def index(self):
return "Hello World!"
# deprecated
@cherrypy.expose
def motor(self):
Thread(target=garage_button,args=[0]).start()
return "trigger garage door"
# deprecated
@cherrypy.expose
def remote(self):
Thread(target=gate_button,args=[0]).start()
return "trigger gate"
@cherrypy.expose
def garage(self):
Thread(target=garage_button,args=[0]).start()
return "trigger garage door"
@cherrypy.expose
def gate(self):
Thread(target=gate_button,args=[0]).start()
return "trigger gate"
@cherrypy.expose
def all(self):
Thread(target=garage_button,args=[0]).start()
Thread(target=gate_button,args=[0]).start()
return "trigger all"
@cherrypy.expose
def garage_status(self):
if (GPIO.input(PIN_DOOR_OPEN) and GPIO.input(PIN_DOOR_CLOSED)):
return "moving"
elif (GPIO.input(PIN_DOOR_OPEN) and not GPIO.input(PIN_DOOR_CLOSED)):
return "closed"
return "open"
cherrypy.quickstart(GarageService())
logging.info('Shutdown')
logging.info('Reset GPIO configuration and close')
GPIO.cleanup()