fix temp parsingn from arduino

This commit is contained in:
Arti Zirk 2016-10-15 11:22:12 +02:00
parent d701b63914
commit 117ae69e27
1 changed files with 18 additions and 16 deletions

34
main.py
View File

@ -88,10 +88,10 @@ html = """<!DOCTYPE html>
<div style="display: flex;flex-direction: row;flex-wrap: wrap;justify-content: center;align-items: center;"> <div style="display: flex;flex-direction: row;flex-wrap: wrap;justify-content: center;align-items: center;">
<div id="temp_container"> <div id="temp_container">
<!--div style="display: inline-block; position: relative; top: -24px;"> <div style="display: inline-block; position: relative; top: -24px;">
<input type="text" style="display: block; width: 46px; font-size: 20px; padding: 0; margin: 20px 0;"> <!--input type="text" style="display: block; width: 46px; font-size: 20px; padding: 0; margin: 20px 0;">
<button style="display:block; height:50px; width:50px;padding:10px;">set</button> <button style="display:block; height:50px; width:50px;padding:10px;">set</button-->
</div--> </div>
<canvas data-width="100" <canvas data-width="100"
data-height="300" data-height="300"
data-type="linear-gauge" data-type="linear-gauge"
@ -121,15 +121,14 @@ html = """<!DOCTYPE html>
temps = deque(maxlen=10) temps = deque(maxlen=10)
def stream(dev): def stream(dev):
with serial.Serial(dev, timeout=1) as s: while True:
while True: temperature = dev.readline().decode().strip().split()[0]
temperature = s.readline().decode().strip() if not temperature:
if not temperature: continue
continue print("temperature", temperature)
print("temperature", temperature) temps.append(temperature)
temps.append(temperature)
arduino = None
def application(env, start_response): def application(env, start_response):
@ -159,8 +158,8 @@ def application(env, start_response):
if message.startswith("message="): if message.startswith("message="):
message = parse_qs(message)["message"][0] message = parse_qs(message)["message"][0]
messages.appendleft(html_escape(message.strip())) start_response("200 OK", [('Content-Type', 'application/json')])
#start_response("200 OK", [('Content-Type', 'application/json')]) return [json.dumps("OK")]
if env["PATH_INFO"] == "/temps.json": if env["PATH_INFO"] == "/temps.json":
start_response('200 OK', [('Content-Type', 'application/json')]) start_response('200 OK', [('Content-Type', 'application/json')])
@ -204,8 +203,11 @@ if __name__ == "__main__":
print("{} is not a valid tty device".format(ser_dev)) print("{} is not a valid tty device".format(ser_dev))
exit(1) exit(1)
import threading with serial.Serial(ser_dev, timeout=1) as s:
t = threading.Thread(target=stream, args=[ser_dev]) arduino = s
import threading
t = threading.Thread(target=stream, args=[ser_dev])
# classifying as a daemon, so they will die when the main dies # classifying as a daemon, so they will die when the main dies
t.daemon = True t.daemon = True