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.

80 lines
3.0 KiB

5 years ago
5 years ago
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
  5. import os
  6. from flask import Flask, make_response, request, render_template, redirect, url_for, send_from_directory #Import flask web server and additional components
  7. app = Flask(__name__)
  8. # Set up the GPIO pins
  9. GPIO.setwarnings(False)
  10. GPIO.setmode(GPIO.BOARD)
  11. PIN_TRIG = 40
  12. PIN_ECHO = 38
  13. GPIO.setup(7, GPIO.OUT)
  14. GPIO.setup(11, GPIO.OUT)
  15. GPIO.output(7, True)
  16. GPIO.output(11, True)
  17. #Variables
  18. correct_login = 0
  19. # Route for the main page
  20. @app.route('/')
  21. def index():
  22. logged_in = request.cookies.get('logged_in')
  23. if logged_in == "yes":
  24. GPIO.setup(PIN_TRIG, GPIO.OUT) #Setup the gpio trigger pin as input
  25. GPIO.setup(PIN_ECHO, GPIO.IN) #Setup the gpio echo pin as output
  26. time.sleep(0.5) #Wait for 0.5 seconds for sensor to settle
  27. GPIO.output(PIN_TRIG, GPIO.LOW) #Set trigger to low
  28. GPIO.output(PIN_TRIG, GPIO.HIGH) #Set trigger to high
  29. time.sleep(0.00001) #Wait for 0.1 milliseconds before setting to low again
  30. GPIO.output(PIN_TRIG, GPIO.LOW) #Set trigger to low again
  31. while GPIO.input(PIN_ECHO)==0:
  32. pulse_start_time = time.time() #Set the start time of when the waves are emitted by the sensor
  33. while GPIO.input(PIN_ECHO)==1:
  34. pulse_end_time = time.time() #Record the time the waves traveled back to the sensor
  35. pulse_duration = pulse_end_time - pulse_start_time #Calculate how long it took for the round trip of the waves
  36. distance = round(pulse_duration * 17150, 2) #Convert the time it took to centimeters and round to 2 decimals
  37. if distance >= 80: #Check if the distance is less than 80cm (This will depend on the garage)
  38. message = 'The garage is closed.'
  39. OpenOrClose = 'Open'
  40. else:
  41. message = 'The garage is open.'
  42. OpenOrClose = 'Close'
  43. else:
  44. return redirect(url_for('login'))
  45. return render_template('index.html', message=message, distance=distance, OpenOrClose=OpenOrClose)
  46. # Route for the login page
  47. @app.route('/login', methods=['GET', 'POST'])
  48. def login():
  49. error = None
  50. if request.method == 'POST':
  51. if request.form['username'] != 'admin' or request.form['password'] != 'admin':
  52. error = 'Invalid Credentials. Please try again.'
  53. else:
  54. global correct_login
  55. correct_login = 1
  56. return redirect(url_for('cookie'))
  57. return render_template('login.html', error=error)
  58. # Route for the login page
  59. @app.route('/set-cookie')
  60. def cookie():
  61. if correct_login == 1:
  62. resp = make_response(redirect('/'))
  63. resp.set_cookie('logged_in', 'yes')
  64. return resp
  65. else:
  66. return 'Not logged in.'
  67. @app.route('/favicon.ico')
  68. def favicon():
  69. return send_from_directory(os.path.join(app.root_path, 'static'),
  70. 'favicon.ico',mimetype='image/vnd.microsoft.icon')
  71. if __name__ == '__main__':
  72. app.run(host='0.0.0.0', port=80) #Run the webserver