pi_gpiozero_examples/webLed.py
2020-11-18 07:48:51 +01:00

35 lines
853 B
Python

from flask import Flask, render_template, Response, request, redirect, url_for
from gpiozero import LED
#This sketch allows you to control an LED from the browser.
#When you run the sketch navigate to yourpi.interaction.tools:8000 to try it
led = LED(17)
app = Flask(__name__)
@app.route("/")
def index():
return render_template('index.html', led_status="Press 'on' or 'off'")
@app.route("/on/", methods=['POST'])
def turn_led_on():
led.on()
led_status = 'LED is on'
#Return the page result
return render_template('index.html', led_status=led_status)
@app.route("/off/", methods=['POST'])
def turn_led_off():
led.off()
led_status= 'LED is off'
#Return the page result
return render_template('index.html', led_status=led_status)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)