38 lines
707 B
Python
38 lines
707 B
Python
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()
|
|
|
|
@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)
|