json
This commit is contained in:
parent
b43920bacc
commit
4f3726d89b
28
ard.py
28
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
|
||||
data = {"value": 0}
|
||||
|
||||
def read_data():
|
||||
global data
|
||||
while True:
|
||||
s = ser.read(1) # read up to one hundred bytes
|
||||
th=threading.Thread(target=pot)
|
||||
th.start()
|
||||
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)
|
Loading…
Reference in New Issue
Block a user