From 81e4b1de948bf4b5ddb8a324f812ce0b30427af7 Mon Sep 17 00:00:00 2001 From: Dima Date: Mon, 22 Apr 2024 19:57:35 +0300 Subject: [PATCH] v.4 --- main.py | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index 718d0b5..f6616dc 100644 --- a/main.py +++ b/main.py @@ -1,18 +1,37 @@ from fastapi import FastAPI +import serial +import threading +from queue import Queue + + +serial_port = "COM4" +baud_rate = 9600 +serial_conn = serial.Serial(serial_port, baud_rate) + +line = "0" + + +def read_from_arduino(): + global line + while True: + if serial_conn.in_waiting > 0: + line = serial_conn.read().decode() + print(line) + + +threading.Thread(target=read_from_arduino, daemon=True).start() app = FastAPI() -count = 0 - -@app.get("/count") -async def get_count(): - global count - count += 1 - return {"count": count} +@app.get("/pin_state") +async def get_pin_state(): + return {"pin_state": line} + @app.get("/") async def root(): return {"message": "Hello World"} + if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=12345)