Created
January 9, 2019 02:14
-
-
Save SisyamoNeko/f8a4fb9dcc6a170877ea633cf76bf93f to your computer and use it in GitHub Desktop.
DS18B20で水温取得と SQlite3 + Bokeh + Flask でグラフ描画
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# _*_ coding: utf-8 _* | |
import subprocess | |
import time, signal, sys | |
import sqlite3 | |
import datetime | |
from bokeh.plotting import figure, output_file, show | |
import contextlib | |
class Main(): | |
"""実行用クラス""" | |
def __init__(self): | |
temp = Measurement() | |
db_out = DbRead() | |
temp.main() | |
temp.sqlite_insert() | |
db_out.sqlite_select() | |
db_out.graph_draw() | |
class Measurement(): | |
"""計測クラス""" | |
SENSOR_ID = "Your_ID" | |
SENSOR_W1_SLAVE = "/sys/bus/w1/devices/" + SENSOR_ID + "/w1_slave" | |
ERR_VAL = 85000 | |
dbname = 'Your_dbname' | |
def __init__(self,temp_val = None): | |
self.temp_val = temp_val | |
output_time = datetime.datetime.now() | |
self.output_time = "{0:%Y-%m-%dT%H:%M:%SZ}".format(output_time) | |
def main(self): | |
# 取得した値を成型後 temp_val に返す | |
res = self.get_water_temp() | |
if res is not None: | |
self.temp_val = str(res).split("=") | |
self.temp_val = self.temp_val[-1].translate(str.maketrans({'"':'','\\':'','n':'','\'':''})) | |
if self.temp_val == self.ERR_VAL: | |
return self.temp_val | |
self.log_write() | |
self.temp_val = round(float(self.temp_val) / 1000, 1) | |
return self.temp_val | |
self.log_write() | |
else: | |
return self.temp_val | |
self.log_write() | |
def get_water_temp(self): | |
#水温計から取得した値を返す | |
try: | |
res = subprocess.check_output(["cat", self.SENSOR_W1_SLAVE]) | |
return res | |
except: | |
return None | |
def log_write(self): | |
# temp.logに書き込み | |
output_time = time.asctime() | |
if self.temp_val != self.ERR_VAL and self.temp_val != None: | |
with open("Your_logfile","a+", encoding="UTF-8") as log_file: | |
log_file.write(output_time + " : " + str(self.temp_val) + "℃" + "\n") | |
elif self.temp_val == self.ERR_VAL: | |
with open("Your_logfile","a+", encoding="UTF-8") as log_file: | |
log_file.write(output_time + " : Got value:85000. Circuit is ok, but something wrong happens...\n") | |
else : | |
with open("Your_logfile","a+", encoding="UTF-8") as log_file: | |
log_file.write(output_time + " : something wrong with Circuit.cannot read the value.\n") | |
def sqlite_insert(self): | |
# データベースに書き込み | |
data = (self.output_time, self.temp_val) | |
with contextlib.closing(sqlite3.connect(self.dbname)) as con: | |
cur = con.cursor() | |
cur.execute('insert into watertemp (date, temperture) values (?,?)', (data)) | |
con.commit() | |
class DbRead: | |
"""データベース読み込みクラス""" | |
dbname = 'Your_dbname' | |
def __init__(self): | |
self.x = [] | |
self.y = [] | |
def sqlite_select(self): | |
# データベースから取り出し | |
with contextlib.closing(sqlite3.connect(self.dbname)) as con: | |
cur = con.cursor() | |
cur.execute('SELECT date FROM watertemp order by date desc limit 72') | |
self.x = ([(x[0]) for x in cur.fetchall()]) | |
cur.execute('SELECT temperture FROM watertemp order by date desc limit 72') | |
self.y = ([(y[0]) for y in cur.fetchall()]) | |
def graph_draw(self): | |
x = self.x[::-1] | |
y = self.y[::-1] | |
# output to static HTML file | |
output_file("Your_dir/templates/lines.html") | |
# create a new plot with a title and axis labels | |
p = figure(title="メダカちゃんの水温", plot_width=640, plot_height=480, x_axis_label='x', y_axis_label='y', x_range=x) | |
p.vbar(x=x, top=y, width=0.3) | |
p.y_range.start = 0 | |
p.xaxis.major_label_orientation = 1 | |
# add a line renderer with legend and line thickness | |
p.line(x, y, line_width=5,legend="水温:℃", color="limegreen") | |
# show the results | |
show(p) | |
if __name__ == "__main__": | |
Main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment