diff --git a/main.py b/main.py index 4dbd878..df1580b 100644 --- a/main.py +++ b/main.py @@ -119,16 +119,19 @@ html = """ temps = deque(maxlen=10) - -def stream(dev): - while True: - temperature = dev.readline().decode().strip().split()[0] - if not temperature: - continue - print("temperature", temperature) - temps.append(temperature) - 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]) + + def application(env, start_response): @@ -203,11 +206,8 @@ if __name__ == "__main__": print("{} is not a valid tty device".format(ser_dev)) exit(1) - with serial.Serial(ser_dev, timeout=1) as s: - arduino = s - import threading - t = threading.Thread(target=stream, args=[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