1

I'm building a local website in python to press a button, so the door to my room will open from my phone by using a raspberry pi. I already have made a python program, which, if ran, will open the door flawlessly, however, I am trying to make a button in HTML that will return something to execute the file which will open the door.

This is what I already have:

from flask import Flask, render_template
from test import open_door

app = Flask(__name__)


@app.route('/open/door')
def doorOpen():
    return render_template('door.html')

@app.route('/opendoor')
def openDoor():
    open_door()
    return 'the door should be open'


if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')

Here is test.py the file which will open the door if executed, and door.html is the following:

<html>
<body>
<a href="/opendoor" class="openbutton">Open for me please</a>
</body>
</html>

It is not something fancy but it only has to work for now. It looks to me like the return is not doing anything since i also added a return and a print function in the openDoor() function withouth any response. I couln't find any awnsers on the internet so i am curious what the problem is!

p.s. This is my first time with python and i am a beginner with HTML edit: this is test.py:

import RPi.GPIO as GPIO
import time

testPin = 18

GPIO.setmode(GPIO.BCM)
GPIO.setup(testPin, GPIO.OUT)

counter =0

def open_door():
    try:
        while counter < 900000:
            GPIO.output(testPin, GPIO.HIGH)

            counter += 1

    except:
        print ("Everything is oke!")

    finally:
        GPIO.cleanup()

1 Answer 1

2

Instead of an onclick function, you can route the button to another page on your localhost:

from flask import Flask, render_template
from test import open_door
app = Flask(__name__)

@app.route('/')
def doorOpen()
   return render_template('door.html')

@app.route('/opendoor')
def openDoor():
   open_door()

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

In door.html:

<html>
<body>
<a href="/opendoor" class="openbutton">Open Door</a>
</body>
</html>

In the HTML file, class="openbutton" is for pure styling purposes.

Sign up to request clarification or add additional context in comments.

8 Comments

<a href="/opendoor">Open Door</a> should work, and doesn't require knowledge of the hostname.
I get the message that test() is not defined. Can't find anywhere on how to make it run another file
@Kuunnn test() is a function call for demonstration in this code. To run the code in test.py, you have to import the file and call whatever main function exists in the file. What functions are written in test.py?
@Ajax1234 I made use of a "try:" function with a while loop in it. Followed by "except:" function with a print in it to test if it worked. After that, a "finally: GPIO.cleanup()"
@Kuunnn Encapsulate the code you have above in a function called open_door, which can then be imported to the flask app. Please see my recent edit for more info.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.