From 4f3726d89b6bfcfc2d2781eef3340e8e6e0a5114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D1=82=D0=BE=D0=BD=20=D0=9A=D0=B8=D1=80=D0=BF?= =?UTF-8?q?=D0=B8=D0=BA=D0=BE=D0=B2?= Date: Mon, 20 May 2024 19:48:54 +0300 Subject: [PATCH] json --- ard.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/ard.py b/ard.py index ef5e6ed..35a2e90 100644 --- a/ard.py +++ b/ard.py @@ -2,22 +2,30 @@ import uvicorn from serial import Serial import threading from fastapi import FastAPI - -s="" - +import json ser = Serial('COM6', 9600) -def pot(): - global s - while True: - s = ser.read(1) # read up to one hundred bytes -th=threading.Thread(target=pot) -th.start() +data = {"value": 0} + +def read_data(): + global data + while True: + incoming_data = ser.read(1) # read up to one byte + try: + data["value"] = int(incoming_data) + except ValueError: + pass + app = FastAPI() +def get_data(): + return json.dumps(data) + +th = threading.Thread(target=read_data) +th.start() + @app.get("/") async def root(): - return ("message", s) - + return data if __name__ == "__main__": uvicorn.run(app, port=10000) \ No newline at end of file