From 338490e88732c32b2eeea47449e96a27572cb874 Mon Sep 17 00:00:00 2001 From: Arti Zirk Date: Sat, 15 Oct 2016 12:21:32 +0200 Subject: [PATCH] makeit heat --- main.js | 4 ++++ main.py | 26 +++++++++++++------------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/main.js b/main.js index c6694d5..8bdaa40 100644 --- a/main.js +++ b/main.js @@ -30,4 +30,8 @@ fetch("/temps.json").then(function (resp) { } }); +function heat(h) { + fetch("/heat",{ method: 'POST', body: JSON.stringify({message:h})}) +} + createTimeline(); diff --git a/main.py b/main.py index df1580b..9112141 100644 --- a/main.py +++ b/main.py @@ -89,8 +89,9 @@ html = """
- + + +
temps = deque(maxlen=10) arduino = None def stream(dev): - with serial.Serial(ser_dev, timeout=1) as s: - global arduino - arduino = s - while True: - temperature = s.readline().decode().strip().split() - if not temperature: - continue - print("temperature", temperature) - temps.append(temperature[0]) + while True: + temperature = dev.readline().decode().strip().split() + if not temperature: + continue + print("temperature", temperature) + temps.append(temperature[0]) @@ -162,7 +160,8 @@ def application(env, start_response): message = parse_qs(message)["message"][0] start_response("200 OK", [('Content-Type', 'application/json')]) - return [json.dumps("OK")] + print(arduino.write(message.encode())) + return [json.dumps(message).encode()] if env["PATH_INFO"] == "/temps.json": start_response('200 OK', [('Content-Type', 'application/json')]) @@ -206,8 +205,9 @@ if __name__ == "__main__": print("{} is not a valid tty device".format(ser_dev)) exit(1) + arduino = serial.Serial(ser_dev, timeout=2) import threading - t = threading.Thread(target=stream, args=[ser_dev]) + t = threading.Thread(target=stream, args=[arduino]) # classifying as a daemon, so they will die when the main dies t.daemon = True