switch to flask app

This commit is contained in:
Michael Clemens 2019-07-06 23:03:20 +01:00
parent b26b99c5de
commit 03b34d6fa8
2 changed files with 51 additions and 15 deletions

View File

@ -1,27 +1,48 @@
from flask import Flask, redirect, url_for, request, render_template
import RPi.GPIO as GPIO
import sys
import time
app = Flask(__name__)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
RELAIS_1_GPIO = 0
RELAIS_2_GPIO = 1
GPIO.setup(RELAIS_1_GPIO, GPIO.OUT)
GPIO.setup(RELAIS_2_GPIO, GPIO.OUT)
antenna = sys.argv[1]
GPIO.output(RELAIS_1_GPIO, GPIO.HIGH)
GPIO.output(RELAIS_2_GPIO, GPIO.HIGH)
if antenna == "dipol":
GPIO.output(RELAIS_2_GPIO, GPIO.HIGH)
time.sleep(1)
GPIO.output(RELAIS_1_GPIO, GPIO.LOW)
elif antenna == "diamond":
GPIO.output(RELAIS_1_GPIO, GPIO.HIGH)
time.sleep(1)
GPIO.output(RELAIS_2_GPIO, GPIO.LOW)
elif antenna == "off":
GPIO.output(RELAIS_1_GPIO, GPIO.HIGH)
GPIO.output(RELAIS_2_GPIO, GPIO.HIGH)
else:
print("nope")
@app.route('/')
def start():
return render_template('index.html')
@app.route('/index',methods = ['POST', 'GET'])
def index():
if request.method == 'POST':
if request.form['ant_button'] == 'Antenna 1':
GPIO.output(RELAIS_2_GPIO, GPIO.HIGH)
time.sleep(1)
GPIO.output(RELAIS_1_GPIO, GPIO.LOW)
print('Antenna 1 selected')
return redirect(url_for('start'))
if request.form['ant_button'] == 'Antenna 2':
GPIO.output(RELAIS_1_GPIO, GPIO.HIGH)
time.sleep(1)
GPIO.output(RELAIS_2_GPIO, GPIO.LOW)
print('Antenna 2 selected')
return redirect(url_for('start'))
if request.form['ant_button'] == 'Antenna Off':
GPIO.output(RELAIS_1_GPIO, GPIO.HIGH)
GPIO.output(RELAIS_2_GPIO, GPIO.HIGH)
print('Off: No antenna attached')
return redirect(url_for('start'))
else:
print(request.form.action)
return redirect(url_for('start'))
if __name__ == '__main__':
app.run(host='0.0.0.0', port='5000')

15
templates/index.html Normal file
View File

@ -0,0 +1,15 @@
<html>
<body>
<form action = "/index" method = "post">
<div>
<input type ="submit" name="ant_button" value="Antenna 1">
</div>
<div>
<input type ="submit" name="ant_button" value="Antenna 2">
</div>
<div>
<input type ="submit" name="ant_button" value="Antenna Off">
</div>
</form>
</body>
</html>