A rewrite of the Raspberry Pi Garage Door Opener using Flask
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. # Python Script To Control Garage Door
  2. # Load libraries
  3. import RPi.GPIO as GPIO #Import RPi GPIO library
  4. import time #Import time
  5. from flask import Flask #Import flask web server
  6. app = Flask(__name__)
  7. # Set up the GPIO pins
  8. GPIO.setwarnings(False)
  9. GPIO.setmode(GPIO.BOARD)
  10. PIN_TRIG = 40
  11. PIN_ECHO = 38
  12. GPIO.setup(7, GPIO.OUT)
  13. GPIO.setup(11, GPIO.OUT)
  14. GPIO.output(7, True)
  15. GPIO.output(11, True)
  16. @app.route('/') #root directory of webserver
  17. def index():
  18. GPIO.setup(PIN_TRIG, GPIO.OUT) #Setup the gpio trigger pin as input
  19. GPIO.setup(PIN_ECHO, GPIO.IN) #Setup the gpio echo pin as output
  20. time.sleep(2) #Wait for 2 seconds for sensor to settle
  21. GPIO.output(PIN_TRIG, GPIO.LOW) #Set trigger to low
  22. GPIO.output(PIN_TRIG, GPIO.HIGH) #Set trigger to high
  23. time.sleep(0.00001) #Wait for 0.1 milliseconds before setting to low again
  24. GPIO.output(PIN_TRIG, GPIO.LOW) #Set trigger to low again
  25. while GPIO.input(PIN_ECHO)==0:
  26. pulse_start_time = time.time() #Set the start time of when the waves are emitted by the sensor
  27. while GPIO.input(PIN_ECHO)==1:
  28. pulse_end_time = time.time() #Record the time the waves traveled back to the sensor
  29. pulse_duration = pulse_end_time - pulse_start_time #Calculate how long it took for the round trip of the waves
  30. distance = round(pulse_duration * 17150, 2) #Convert the time it took to centimeters and round to 2 decimals
  31. if distance >= 80: #Check if the distance is less than 80cm (This will depend on the garage)
  32. return 'The garage is closed.'
  33. else:
  34. return 'The garage is open.'
  35. if __name__ == '__main__':
  36. app.run(host='0.0.0.0') #Run the webserver