v.4
This commit is contained in:
parent
c10129a5a6
commit
81e4b1de94
33
main.py
33
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)
|
||||
|
Loading…
Reference in New Issue
Block a user