Contains python control script, Android app code, schematics, and an installer script to automate installation on the Pi.
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.

108 lines
4.1 KiB

  1. # Python Script To Control Garage Door
  2. # Load libraries
  3. import RPi.GPIO as GPIO
  4. import time
  5. from bottle import route, run, post, request
  6. # Set up the GPIO pins
  7. GPIO.setwarnings(False)
  8. GPIO.setmode(GPIO.BOARD)
  9. PIN_TRIG = 40
  10. PIN_ECHO = 38
  11. GPIO.setup(7, GPIO.OUT)
  12. GPIO.setup(11, GPIO.OUT)
  13. GPIO.output(7, True)
  14. GPIO.output(11, True)
  15. #define variables
  16. fresh_login = True
  17. pswd = 'xxxx'
  18. #Define check_login (function that checks if password is correct by comparing it to the correct one)
  19. def check_login(password):
  20. if password == pswd:
  21. return True
  22. else:
  23. return False
  24. #Create the login form (html)
  25. @route('/login') #http://servername/login
  26. def login():
  27. #Making fresh_login true when new form is created
  28. global fresh_login #make fresh_login a global variable
  29. fresh_login = True
  30. #Create the html form
  31. return '''
  32. <form action="/login" method="post">
  33. Password: <input name="password" type="password" />
  34. <input value="Open" type="submit" />
  35. </form>
  36. '''
  37. #Receive and check login info
  38. @route('/login', method='POST')
  39. def do_login():
  40. global fresh_login #make fresh_login global from inside function do_login
  41. password = request.forms.get('password') #request password from form created in login
  42. #Making sure the login is fresh
  43. if fresh_login == True:
  44. fresh_login = False #make fresh login false so page reload doesn't cause garage to re-open (a variable that takes place of a cookie, but clears on webpage closing)
  45. if check_login(password): #cycle the gpio to open garage if passwords match
  46. GPIO.output(7, False)
  47. time.sleep(.8)
  48. GPIO.output(7, True)
  49. return "<p>Opening Garage Door One.</p>" #confirm that garage is opening
  50. else:
  51. return "<p>Incorrect Password.</p>" #tell them to go away if password is incorrect
  52. else:
  53. return "<p>Please restart your browser</p>" #this triggers if fresh_login was false when the function ran, telling you to restart your browser/webpage to clear the fresh_login variable
  54. #Create the login form (html) for the sensor page
  55. @route('/sensor') #http://servername/sensor
  56. def sensor():
  57. #Create the html form
  58. return '''
  59. <form action="/sensor" method="post">
  60. Password: <input name="password" type="password" />
  61. <input value="Authorize" type="submit" />
  62. </form>
  63. '''
  64. #Create the sensor page (html)
  65. @route('/sensor', method='POST') #http://servername/sensor
  66. def do_sensor():
  67. password = request.forms.get('password') #request password from form created in login
  68. if check_login(password): #Continue to the sensor reading if the password is correct
  69. GPIO.setup(PIN_TRIG, GPIO.OUT) #Setup the gpio trigger pin as input
  70. GPIO.setup(PIN_ECHO, GPIO.IN) #Setup the gpio echo pin as output
  71. time.sleep(2) #Wait for 2 seconds for sensor to settle
  72. GPIO.output(PIN_TRIG, GPIO.LOW) #Set trigger to low
  73. GPIO.output(PIN_TRIG, GPIO.HIGH) #Set trigger to high
  74. time.sleep(0.00001) #Wait for 0.1 milliseconds before setting to low again
  75. GPIO.output(PIN_TRIG, GPIO.LOW) #Set trigger to low again
  76. while GPIO.input(PIN_ECHO)==0:
  77. pulse_start_time = time.time() #Set the start time of when the waves are emitted by the sensor
  78. while GPIO.input(PIN_ECHO)==1:
  79. pulse_end_time = time.time() #Record the time the waves traveled back to the sensor
  80. pulse_duration = pulse_end_time - pulse_start_time #Calculate how long it took for the round trip of the waves
  81. distance = round(pulse_duration * 17150, 2) #Convert the time it took to centimeters and round to 2 decimals
  82. if distance >= 80: #Check if the distance is less than 80cm (This will depend on the garage)
  83. return '<p>The garage is closed.</p>'
  84. else:
  85. return '<p>The garage is open.</p>'
  86. else:
  87. return '<p>Sorry, the password entered was incorrect</p>' #Don't allow access if password isn't correct
  88. #Run the bottle.py server
  89. run(host='0.0.0.0', port=1234)