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 id="temp_container">
<!--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;">
<button style="display:block; height:50px; width:50px;padding:10px;">set</button>
</div-->
<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;">
<button style="display:block; height:50px; width:50px;padding:10px;">set</button-->
</div>
<canvas data-width="100"
data-height="300"
data-type="linear-gauge"
@ -121,15 +121,14 @@ html = """<!DOCTYPE html>
temps = deque(maxlen=10)
def stream(dev):
with serial.Serial(dev, timeout=1) as s:
while True:
temperature = s.readline().decode().strip()
if not temperature:
continue
print("temperature", temperature)
temps.append(temperature)
while True:
temperature = dev.readline().decode().strip().split()[0]
if not temperature:
continue
print("temperature", temperature)
temps.append(temperature)
arduino = None
def application(env, start_response):
@ -159,8 +158,8 @@ def application(env, start_response):
if message.startswith("message="):
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":
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))
exit(1)
import threading
t = threading.Thread(target=stream, args=[ser_dev])
with serial.Serial(ser_dev, timeout=1) as s:
arduino = s
import threading
t = threading.Thread(target=stream, args=[ser_dev])
# classifying as a daemon, so they will die when the main dies
t.daemon = True