From 89800feba11a689fd31ef3f785d2ebda2d8a796f Mon Sep 17 00:00:00 2001 From: rav4s Date: Thu, 17 Dec 2020 17:30:25 -0600 Subject: [PATCH] More cookie stuff --- garage_door_script.py | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/garage_door_script.py b/garage_door_script.py index 2359cfd..7171655 100644 --- a/garage_door_script.py +++ b/garage_door_script.py @@ -22,23 +22,27 @@ correct_login = 0 # Route for the main page @app.route('/') def index(): - GPIO.setup(PIN_TRIG, GPIO.OUT) #Setup the gpio trigger pin as input - GPIO.setup(PIN_ECHO, GPIO.IN) #Setup the gpio echo pin as output - time.sleep(2) #Wait for 2 seconds for sensor to settle - GPIO.output(PIN_TRIG, GPIO.LOW) #Set trigger to low - GPIO.output(PIN_TRIG, GPIO.HIGH) #Set trigger to high - time.sleep(0.00001) #Wait for 0.1 milliseconds before setting to low again - GPIO.output(PIN_TRIG, GPIO.LOW) #Set trigger to low again - while GPIO.input(PIN_ECHO)==0: - pulse_start_time = time.time() #Set the start time of when the waves are emitted by the sensor - while GPIO.input(PIN_ECHO)==1: - pulse_end_time = time.time() #Record the time the waves traveled back to the sensor - pulse_duration = pulse_end_time - pulse_start_time #Calculate how long it took for the round trip of the waves - distance = round(pulse_duration * 17150, 2) #Convert the time it took to centimeters and round to 2 decimals - if distance >= 80: #Check if the distance is less than 80cm (This will depend on the garage) - return 'The garage is closed.' + logged_in = request.cookies.get('logged_in') + if logged_in == "yes": + GPIO.setup(PIN_TRIG, GPIO.OUT) #Setup the gpio trigger pin as input + GPIO.setup(PIN_ECHO, GPIO.IN) #Setup the gpio echo pin as output + time.sleep(2) #Wait for 2 seconds for sensor to settle + GPIO.output(PIN_TRIG, GPIO.LOW) #Set trigger to low + GPIO.output(PIN_TRIG, GPIO.HIGH) #Set trigger to high + time.sleep(0.00001) #Wait for 0.1 milliseconds before setting to low again + GPIO.output(PIN_TRIG, GPIO.LOW) #Set trigger to low again + while GPIO.input(PIN_ECHO)==0: + pulse_start_time = time.time() #Set the start time of when the waves are emitted by the sensor + while GPIO.input(PIN_ECHO)==1: + pulse_end_time = time.time() #Record the time the waves traveled back to the sensor + pulse_duration = pulse_end_time - pulse_start_time #Calculate how long it took for the round trip of the waves + distance = round(pulse_duration * 17150, 2) #Convert the time it took to centimeters and round to 2 decimals + if distance >= 80: #Check if the distance is less than 80cm (This will depend on the garage) + return 'The garage is closed.' + else: + return 'The garage is open.' else: - return 'The garage is open.' + return 'Please login.' # Route for the login page @app.route('/login', methods=['GET', 'POST'])