728x90
sudo python -m SimpleHTTPServer 80
pip install flask
sudo python app.py host='0.0.0.0'
가상환경 구축
pip install virtualenv
virtualenv "프로젝트폴더명"
cd 프로젝트폴더명
source bin\activate
pip install 시 에러가 난다면 시간 동기화 문제 때문일 수 있다. 그럴땐 아래와 같이 해결한다.
rdate 설치
sudo apt-get install rdate
시간 얻어오기
rdate -p time.bora.net
시간 설정
rdate -s time.bora.net
혹은 수동으로 시간 설정하려면
sudo date -s "2017-06-27 18:25:45"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | from firebase import firebase from flask import Flask, render_template, request import threading from subprocess import Popen, PIPE from time import sleep app = Flask(__name__) thread = None firebase = firebase.FirebaseApplication('https://androidpushservice-17$ #raspberry cpu temperature def get_cpu_temperature(): process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE) output, _error = process.communicate() return float(output[output.index('=') + 1:output.rindex("'")]) def background_thread(): count = 0 while True: cpu_temp = get_cpu_temperature() #database update firebase.patch('/raspberrypi3', {'temperature': cpu_te$ sleep(30) @app.route("/") @app.route("/index") def main(): global thread if thread is None: thread = threading.Thread(target=background_thread) thread.start() return render_template('main.html') if __name__ == '__main__': app.run(host='0.0.0.0', debug=True) | cs |
온습도 (DHT-11)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import Adafruit_DHT import time sensor = Adafruit_DHT.DHT11 pin = 4 while True: humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) if humidity is not None and temperature is not None: print('Temp={0:0.1f}*C humidity={1:0.1f}%'.format(temperature, humidity)) time.sleep(2) else: print('Failed to get reading. Try again!') | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | from firebase import firebase from flask import Flask, render_template, request import threading from subprocess import Popen, PIPE from time import sleep import Adafruit_DHT import time app = Flask(__name__) thread = None sensor = Adafruit_DHT.DHT11 pin = 23 firebase = firebase.FirebaseApplication('https://androidpushservice-172000.firebaseio.com/', None) #raspberry cpu temperature def get_cpu_temperature(): process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE) output, _error = process.communicate() return float(output[output.index('=') + 1:output.rindex("'")]) def background_thread(): count = 0 while True: cpu_temp = get_cpu_temperature() humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) if humidity is not None and temperature is not None: print('Temp={0:0.1f}*C humidity={1:0.1f}%'.format(temperature, humidity)) print('Rpi 3 Cpu_Temp={0:0.1f}*C'.format(cpu_temp)) else: print('Failed to get reading. Try again!') #database update firebase.patch('/raspberrypi3', {'temperature': cpu_temp}) firebase.patch('/raspberrypi3', {'room_temp' : temperature}) firebase.patch('/raspberrypi3', {'room_humidity' : humidity}) sleep(30) @app.route("/") @app.route("/index") def main(): global thread if thread is None: thread = threading.Thread(target=background_thread) thread.start() return render_template('main.html') if __name__ == '__main__': app.run(host='0.0.0.0', debug=True, port=80) | cs |
라이브러리 예제를 통한 실행
pi@raspberrypi78:~/Documents/app/Adafruit_Python_DHT/examples $ sudo ./AdafruitDHT.py 11 23
Temp=28.0* Humidity=35.0%
LED 제어
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | # -*- coding: utf-8 -*- import sys reload(sys) sys.setdefaultencoding('utf-8') from flask import Flask, render_template, request import RPi.GPIO as GPIO app = Flask(__name__) GPIO.setmode(GPIO.BCM) leds = { 21 : {'name' : 'Red LED', 'state' : GPIO.LOW}, 20 : {'name' : 'Green LED', 'state' : GPIO.LOW}, 16 : {'name' : 'Blue LED', 'state' : GPIO.LOW} } for led in leds : GPIO.setup(led, GPIO.OUT) GPIO.output(led, GPIO.LOW) def getGpioState(): for led in leds: leds[led]['state'] = GPIO.input(led) return leds @app.route("/") def main(): #for port in ports: #ports[port]['state'] = GPIO.input(port) ledState = getGpioState() gpioState = { 'leds' : ledState } return render_template('main.html', **gpioState) @app.route("/<led>/<act>") def action(led, act): led = int(led) leds = getGpioState() dev = leds[led]['name'] if act == "on": GPIO.output(led, GPIO.HIGH) msg = dev + "를 켭니다." elif act == "off": GPIO.output(led, GPIO.LOW) msg = dev + "를 끕니다." elif act == "toggle": GPIO.output(led, not GPIO.input(led)) msg = "토글 " + dev + "." else: msg = "예기치 않은 동작입니다!" #for port in ports: #ports[port]['state'] = GPIO.input(port) leds = getGpioState() gpioState = { 'msg' : msg, 'leds' : leds } return render_template('main.html', **gpioState) if __name__ == "__main__": app.run(host='0.0.0.0', port=80, debug=True) | cs |
'Study > Embedded' 카테고리의 다른 글
wizfi 250, mqtt-sqlite 센서값 전송 (0) | 2017.08.03 |
---|---|
라즈베리파이3, mosquitto (0) | 2017.07.31 |
GCM (0) | 2017.06.27 |
라즈베리파이, Pi4J를 통한 GPIO 제어 (0) | 2017.06.20 |
라즈베리 파이3, 기본 설정 (0) | 2017.06.10 |