$ python -m pip config set global.break-system-packages true
$ pip install python-socketio uvicorn fastapi
● 웹 서버를 이용하여 LED를 ON, OFF 한다.
---< flask_WebServer_LED.py >--------------------------------------------
from flask import Flask, render_template_string
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
app = Flask(__name__)
led_state = "off"
def led_onoff(led_state):
print("led_state : ",led_state)
if led_state == 1:
GPIO.output(23, 1)
elif led_state == 0:
GPIO.output(23, 0)
control_page = """
<script>
function btn_click(state)
{
if(state == 0){
state = 1;
} else {
state = 0;
}
window.location.href='/' + state
}
</script>
<input type='button' onClick='btn_click({{gpio23}})' value='led switch'/>
"""
@app.route('/')
@app.route('/<state>')
def led_start(state="n"):
print("state : ",state)
if state != "n" and state != "favicon.ico":
led_onoff(int(state))
return render_template_string(control_page, gpio23=int(state))
else :
return render_template_string(control_page, gpio23=0)
if __name__ == '__main__':
app.run(host='192.168.1.65', debug=True, port=5000)
------------------------------------------------------------------------------
$ python flask_WebServer_LED.py
실행 시키고
웹 브라우저에서
http://192.168.1.65:5000
으로 접속한다.