라즈베리파이 파이썬(python) 플라스크(flask) 사용 LED 켜기/끄기
- flask 설치 : 라즈베리파이에 기본으로 설치되어 있음
- 파이썬 2.x
- sudo apt-get install python-pip
- sudo pip install flask
- 파이썬 3.x
- sudo apt-get install python3-pip
- sudo pip3 install flask
- 파이썬 2.x
- 간단한 웹서버 구축 : web_server.py
1234567891011121314151617181920212223from flask import Flaskapp=Flask(__name__)@app.route('/')def index():return 'This is main page.'@app.route('/sub')def sub():return 'This is sub page.'@app.route('/led/<state>')def led(state):if state == 'on' or state == 'off':str = 'Led is now %s.' % stateelse:str = 'Invalid Pages'return strif __name__ == '__main__':print('Webserver start.')app.run(debug=True, port=1234, host='0.0.0.0')- route() 메서드가 Flask에 정의되어 있어 그것을 사용
- debug=True : 디버깅 결과를 출력
- port=1234 : 1234 Port를 사용(기본은 5000번 포트 사용)
- host=0.0.0.0 : public하게 하여 모두 접속 가능
- 실행 : python3 web_server.py
12345678910111213141516171819202122pi@raspberrypi:~/webapp $ python3 web_server.pyWebserver start.* Serving Flask app "web_server" (lazy loading)* Environment: productionWARNING: Do not use the development server in a production environment.Use a production WSGI server instead.* Debug mode: on* Running on http://0.0.0.0:1234/ (Press CTRL+C to quit)* Restarting with statWebserver start.* Debugger is active!* Debugger PIN: 238-007-790192.168.0.10 - - [12/Oct/2019 10:39:12] "GET /led/off HTTP/1.1" 200 -192.168.0.10 - - [12/Oct/2019 10:39:13] "GET /favicon.ico HTTP/1.1" 404 -192.168.0.10 - - [12/Oct/2019 10:39:24] "GET /led/on HTTP/1.1" 200 -192.168.0.10 - - [12/Oct/2019 10:39:28] "GET /sub HTTP/1.1" 200 -192.168.0.10 - - [12/Oct/2019 10:39:33] "GET / HTTP/1.1" 200 -* Detected change in '/home/pi/webapp/web_server.py', reloading* Restarting with statWebserver start.* Debugger is active!* Debugger PIN: 238-007-790 - 웹브라우저에서 접속
- http://192.168.0.14:1234/
- 출력 : This is main page.
- http://192.168.0.14:1234/sub
- 출력 : This is sub page.
- http://192.168.0.14:1234/led/on
- 출력 : Led is now on
- http://192.168.0.14:1234/led/off
- 출력 : Led is now off
- http://192.168.0.14:1234/
- flask를 이용하여 LED를 켜고 끄기 구현
- 서버 실행 : python3 web_led.py
- 클라이언트 접속 결과
- flask 웹서버 구성 : web_led.py
12345678910111213141516171819202122232425262728293031323334import RPi.GPIO as gpiofrom flask import Flask, render_templateimport warningsapp=Flask(__name__)warnings.filterwarnings('ignore')LED = 4def led_turn(st):if st == 'on':gpio.output(LED, gpio.HIGH)else:gpio.output(LED, gpio.LOW)@app.route('/')def index():return render_template('index.html', state='off')@app.route('/led/<st>')def led(st):led_turn(st)return render_template('index.html', state=st)if __name__ == '__main__':gpio.setmode(gpio.BCM)gpio.setup(LED, gpio.OUT)led_turn('off')print('Webserver start.')app.run(debug=True, port=1234, host='0.0.0.0')gpio.cleanup() - 홈페이지 구성:/templates/index.html
1234567891011121314151617<!doctype html><html><head><title> LED Turn On/Off </title><link type="text/css" rel="stylesheet" href="/static/style.css"/></head><body><p></p><input type="button" value="켜기" style="WIDTH:200pt; HEIGHT:80pt; font-size:50pt" onclick="location.href='/led/on'";> <input type="button" value="끄기" style="WIDTH:200pt; HEIGHT:80pt; font-size:50pt" onclick="location.href='/led/off'";><br/><p> LED is now <span id="state">{{state}}</span> state.</p></body></html> - 홈페이지 스타일 지정 : /static/style.css
1234567891011121314body{background-color: black;text-align: center;}p{font-size: 50px;color: yellow;}#state{font-size: 80px;color: red;}