62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
from flask import Flask, redirect, render_template_string, request, url_for
|
||
from gpiozero import LED
|
||
|
||
app = Flask(__name__)
|
||
led = LED(17) # GPIO17 = physischer Pin 11
|
||
|
||
HTML = """
|
||
<!doctype html>
|
||
<html lang="de">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title>Raspberry-Pi-Labor – LED</title>
|
||
<style>
|
||
body { font-family: sans-serif; max-width: 600px; margin: 3rem auto;
|
||
padding: 0 1rem; text-align: center; background: #f3f5f7; color: #222; }
|
||
main { background: white; padding: 2rem; border-radius: 1rem; }
|
||
.status { font-size: 1.4rem; margin: 1.5rem 0; }
|
||
button { font-size: 1.2rem; padding: .8rem 1.5rem; margin: .4rem;
|
||
border: 0; border-radius: .5rem; cursor: pointer; }
|
||
.on { background: #b8e6c1; }
|
||
.off { background: #f2c4c4; }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<main>
|
||
<h1>Raspberry-Pi-Labor</h1>
|
||
<h2>LED-Steuerung</h2>
|
||
<p class="status">Status: <strong>{{ "AN" if is_on else "AUS" }}</strong></p>
|
||
<form method="post" action="{{ url_for('switch_led') }}">
|
||
<button class="on" type="submit" name="state" value="on">LED einschalten</button>
|
||
<button class="off" type="submit" name="state" value="off">LED ausschalten</button>
|
||
</form>
|
||
</main>
|
||
</body>
|
||
</html>
|
||
"""
|
||
|
||
|
||
@app.get("/")
|
||
def index():
|
||
return render_template_string(HTML, is_on=led.is_lit)
|
||
|
||
|
||
@app.post("/led")
|
||
def switch_led():
|
||
state = request.form.get("state")
|
||
if state == "on":
|
||
led.on()
|
||
elif state == "off":
|
||
led.off()
|
||
return redirect(url_for("index"))
|
||
|
||
|
||
if __name__ == "__main__":
|
||
try:
|
||
# Im lokalen Netzwerk erreichbar; Debugmodus bleibt ausgeschaltet.
|
||
app.run(host="0.0.0.0", port=8080, debug=False)
|
||
finally:
|
||
led.off()
|
||
led.close()
|