라즈베리 파이 수업 기록용
카테고리 없음 2023. 3. 23.1. mysql -u root -p 2. use test; |
db 설치 : sudo apt-get install mariadb-server
접속 : sudo mysql -u root
접근권한설정 : alter user 'root'@'localhost' identified by '비밀번호';
재접속 : mysql -u root -p
db 생성 : create database test;
db 사용 : use test;
table생성
create table sensordb(sensing INT, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
insert : insert into sensordb(sensing) values (1023);
select : select * from sensordb;
파이썬 모듈 설치 : sudo pip3 install PyMySQL
센서의 데이터를 데이터베이스에 저장하기 위한 코드
import pymysql as ps
db= ps.connect(
host = 'localhost',
user = 'root',
passwd = '1234',
db = 'test',
charset = 'utf8'
)
cursor = db.cursor(# ps.cursors.DictCursor ->튜플형태로
)
def insert_sensor(value):
insert_sql = f"insert into sensordb(sensing) values({value})"
cursor.execute(insert_sql)
db.commit()
def select_sensor():
select_sql = "select * from sensordb"
cursor.execute(select_sql)
result = cursor.fetchall()
return result
라즈베리파이 LED Web으로 제어하기
Python WSGI 마이크로프레임워크 (파이썬으로 웹어플리케이션 제작)
WSGI : Web Server Gateway Interface
- 서버가 웹 어플리케이션과 통신하기 위한 interface
- 최소한의 기능만을 제공하여 유연하게 애플리케이션 작성 가능
from flask import Flask, render_template
import ledCon as lc
app = Flask(__name__)
@app.route("/")
def hello():
return render_template('main.html')
# 반드시 templates 폴더 안에 html 문서가 있어야 한다.
@app.route("/led/on")
def led_on():
lc.ledOn()
return "LED ON"
@app.route("/led/off")
def led_off():
lc.ledOff()
return "LED OFF"
if __name__ == '__main__':
app.run(host="172.30.1.82")