Skip to content

Commit

Permalink
⚗️ #1 Now could able to get the feed with async calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Balaji-Ganesh committed Jun 15, 2023
1 parent 0cda32e commit bc066f5
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions System/ESP32_communicator/fastapi_as_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

app = FastAPI()

esp32_ip = '192.168.135.165' # set it to the assigned IP address to ESP32 when connected to WiFi.
esp32_ip = '192.168.134.165' # set it to the assigned IP address to ESP32 when connected to WiFi.
camera_port, data_port = 81, 82 # configured ports for camera and data-transfer in ESP32.
camera_ws_url = "ws://"+esp32_ip+":"+str(camera_port) # url of camera websockets
data_txrx_url = "ws://"+esp32_ip+":"+str(data_port) # url of data transfer websockets
Expand All @@ -26,6 +26,9 @@ async def establish_line():
async def camera_client():
print("------------------------------------------- Getting camera feed")
try:
cam_ws = await websockets.connect(camera_ws_url)
print("Connection established")
print("About to fetch frames")
while True:
msg = await cam_ws.recv()
# print("Received message", msg)
Expand All @@ -35,40 +38,37 @@ async def camera_client():
cv2.imshow("img", img)
if cv2.waitKey(1) == 27:
print('EXITING')
cv2.destroyAllWindows()
return '--- by from camerafeed'
except Exception as e:
print("[EXCEPTION] Camera streaming interrupted")
return 'ERROR in fetching feed'

async def collision_dist_client():
print("------------------------------------------- Getting ultra-sonic feed")
data_ws = await websockets.connect(data_txrx_url)
while True:
msg = await data_ws.recv()
print("collsion distance: ", msg)

@app.get('/establish-line')
async def establish():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
loop.run_until_complete(establish_line())
except KeyboardInterrupt:
return 'Line establishment interrupted'
task = asyncio.create_task(establish_line())
return await task

@app.get('/camera-feed')
@app.get('/camera-feed/')
async def get_camera_feed():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
loop.run_until_complete(camera_client())
task = asyncio.create_task(camera_client())
return await task
except KeyboardInterrupt:
return 'Camera data fetching interrupted'

@app.get('/collision-distance')
async def get_collision_distance():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
loop.run_until_complete(collision_dist_client())
print("-----------------check: ", data_ws)
task = asyncio.create_task(collision_dist_client())
return await task
except KeyboardInterrupt:
return 'Collsion data fetching interrupted'

0 comments on commit bc066f5

Please sign in to comment.