-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensor_module.py
More file actions
executable file
·49 lines (40 loc) · 1.54 KB
/
sensor_module.py
File metadata and controls
executable file
·49 lines (40 loc) · 1.54 KB
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
#!/usr/bin/python3
from flask import Flask, send_file, make_response, render_template
import datetime
import io
from tinydb import TinyDB, Query
from matplotlib import pyplot as plt
from matplotlib.dates import DayLocator, HourLocator, DateFormatter
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import time
app = Flask(__name__)
@app.route('/')
def main():
temp = str(float(open("/sys/bus/i2c/devices/1-005c/temp1_input", "r").read())/10)
hum = str(float(open("/sys/bus/i2c/devices/1-005c/humidity1_input", "r").read())/10)
return render_template("sensors.html", title="Sensors", temp_data=temp, hum_data=hum)
@app.route('/images/<datatype>')
def images(datatype):
db = TinyDB("sensor_log.json")
Data = Query()
data = db.search(Data.type == datatype)
times = [datetime.datetime.strptime(i[1]['time'], "%Y-%m-%d %H:%M:%S") for i in enumerate(data)]
datas = [i[1]['value'] for i in enumerate(data)]
fig = Figure()
ax = fig.add_subplot(111)
ax.plot_date(times, datas, '-')
ax.xaxis.set_major_locator(DayLocator())
ax.xaxis.set_major_formatter(DateFormatter("%m-%d %H:%M"))
ax.xaxis.set_minor_locator(HourLocator())
ax.autoscale_view()
ax.grid(True)
fig.autofmt_xdate()
canvas = FigureCanvas(fig)
png_output = io.BytesIO()
fig.savefig(png_output, format='png')
response = make_response(png_output.getvalue())
response.headers['Content-Type'] = 'image/png'
return response
if __name__ == "__main__":
app.run()