FW 심화 과정/[2] STM32심화실습
0715 웹 서버 기반 하드웨어 제어
천숭이
2022. 7. 17. 16:54
- 라즈베리파이 보드 빵판 21번 핀에 led를 연결해놓고, 웹서버에서 ON/OFF 버튼으로 LED를 껐다 켰다 하는 프로그램
<app.py>
import RPi.GPIO as GPIO
from flask import Flask, render_template
app=Flask(__name__)
GPIO.setmode(GPIO.BCM)
GPIO.setup(21,GPIO.OUT)
def led_on():
GPIO.output(21,0)
return
def led_off():
GPIO.output(21,1)
return
@app.route('/')
def index():
led_off()
return render_template('index.html',state='OFF')
@app.route('/on/')
def on():
led_on()
print('switch on')
return render_template('index.html',state='ON')
@app.route('/off/')
def off():
led_off()
print('switch off')
return render_template('index.html',state='OFF')
if __name__ == '__main__':
print('web Server Starts')
app.run(debug=True, host='0.0.0.0')
print('web server ends')
GPIO.cleanup()
print("program ends")
<index.html>
<!doctype html>
<html>
<head>
<title> GPIO INPUT/OUTPUT DEMO </title>
<link type="text/css" rel="stylesheet" href="/static/style.css"/>
</head>
<body>
<div calss="onoff"><a href="/on">ON</a></div>
<div calss="onoff"><a href="/off">OFF</a></div>
<br/>
<p> LED is now <span id="state">{{state}}</span> state.</p>
</body>
</html>
<style.css>
body {
background-color:black;
text-align:center;
}
div.onoff a{
color:white;
font-size=200px;
text-decoration=none;
}
p{
font-size:50px;
color:yellow;
}
#state{
font-size:80px;
color:red;
}