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.

57 lines
1.6 KiB

5 years ago
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. return render_template('index.html')
  23. # Route for the login page
  24. @app.route('/login', methods=['GET', 'POST'])
  25. def login():
  26. error = None
  27. if request.method == 'POST':
  28. if request.form['username'] != 'admin' or request.form['password'] != 'admin':
  29. error = 'Invalid Credentials. Please try again.'
  30. else:
  31. global correct_login
  32. correct_login = 1
  33. return redirect(url_for('cookie'))
  34. return render_template('login.html', error=error)
  35. # Route for the login page
  36. @app.route('/set-cookie')
  37. def cookie():
  38. if correct_login == 1:
  39. resp = make_response(redirect('/'))
  40. resp.set_cookie('logged_in', 'yes')
  41. return resp
  42. else:
  43. return 'Not logged in.'
  44. @app.route('/favicon.ico')
  45. def favicon():
  46. return send_from_directory(os.path.join(app.root_path, 'static'),
  47. 'favicon.ico',mimetype='image/vnd.microsoft.icon')
  48. if __name__ == '__main__':
  49. app.run(host='0.0.0.0') #Run the webserver