display temp from ttyACM0

This commit is contained in:
Arti Zirk 2016-10-14 01:34:01 +02:00
parent 8a35a4027b
commit 49c6460303
2 changed files with 46 additions and 6 deletions

25
main.js
View File

@ -4,15 +4,30 @@ var gauge = document.getElementById('temp-gauge');
// Randomly add a data point every 500ms // Randomly add a data point every 500ms
var random = new TimeSeries(); var random = new TimeSeries();
setInterval(function() { setInterval(function() {
var nr = Math.random() * 100; fetch("/temps.json").then(function (resp) {
random.append(new Date().getTime(), nr); return resp.json();
gauge.dataset.value = nr; }).then(function (resp) {
console.log(resp)
var nr = Math.random() * 100;
nr = resp.pop()
random.append(new Date().getTime(), nr);
gauge.dataset.value = nr;
});
}, 500); }, 500);
function createTimeline() { function createTimeline() {
var chart = new SmoothieChart(); var chart = new SmoothieChart({grid:{fillStyle:'transparent'}});
chart.addTimeSeries(random, { strokeStyle: 'rgba(0, 255, 0, 1)', fillStyle: 'rgba(0, 255, 0, 0.2)', lineWidth: 4 }); chart.addTimeSeries(random, { strokeStyle: 'rgba(0, 0, 0, 1)', fillStyle: 'rgba(0, 0, 0, 0.1)', lineWidth: 4 });
chart.streamTo(document.getElementById("chart"), 500); chart.streamTo(document.getElementById("chart"), 500);
} }
fetch("/temps.json").then(function (resp) {
return resp.json();
}).then(function (resp) {
console.log(resp)
for (val of resp) {
random.append(new Date().getTime(), val)
}
});
createTimeline(); createTimeline();

27
main.py
View File

@ -35,6 +35,8 @@ from html import escape as html_escape
from collections import deque from collections import deque
from urllib.parse import parse_qs from urllib.parse import parse_qs
import serial
max_nr_messages = 25 max_nr_messages = 25
html = """<!DOCTYPE html> html = """<!DOCTYPE html>
@ -101,6 +103,18 @@ html = """<!DOCTYPE html>
</html> </html>
""" """
temps = deque(maxlen=10)
def stream(dev):
with serial.Serial(dev, timeout=1) as s:
while True:
temperature = s.readline().decode().strip()
print("temperature", temperature)
temps.append(temperature)
def application(env, start_response): def application(env, start_response):
if env["REQUEST_METHOD"] == "POST": if env["REQUEST_METHOD"] == "POST":
@ -132,7 +146,9 @@ def application(env, start_response):
messages.appendleft(html_escape(message.strip())) messages.appendleft(html_escape(message.strip()))
#start_response("200 OK", [('Content-Type', 'application/json')]) #start_response("200 OK", [('Content-Type', 'application/json')])
if env["PATH_INFO"] == "/temps.json":
start_response('200 OK', [('Content-Type', 'application/json')])
return [json.dumps(list(temps)).encode()]
if env["PATH_INFO"] == "/favicon.ico": if env["PATH_INFO"] == "/favicon.ico":
start_response('200 OK', [('Content-Type', 'image/x-icon')]) start_response('200 OK', [('Content-Type', 'image/x-icon')])
@ -158,6 +174,15 @@ def application(env, start_response):
if __name__ == "__main__": if __name__ == "__main__":
import threading
t = threading.Thread(target=stream, args=["/dev/ttyACM0"])
# classifying as a daemon, so they will die when the main dies
t.daemon = True
# begins, must come after daemon definition
t.start()
from wsgiref.simple_server import make_server from wsgiref.simple_server import make_server
httpd = make_server('0.0.0.0', 8080, application) httpd = make_server('0.0.0.0', 8080, application)