Reputation: 19
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()
Upvotes: 1
Views: 14303
Reputation: 71471
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.
Upvotes: 2