Why isn't my ESP32 sending the full payload to my python server [closed]

2 days ago 3
ARTICLE AD BOX

I'm recently working on a project that has an ESP32 as the client and Python FastAPI as the server.

After the connection, there's an authentication process, which requires them to communicate using WebSocket (messages are in JSON format).

The problem is that the ESP32 said it sent out the message:

{"type":"auth_response","id":"CHY","data":{"hash":"f6bb6993e5a44bcde621d6bdd4a3dcb7fcfa5ac0b9a8137d88bed81510849fed","time":19726}}

but the server only received {. The server didn't receive the whole message sent from the ESP32.

I was expecting the server to receive the whole message, but it didn't.

I've used Postman to test the server, and it's working fine, so I suspect the problem lies in the ESP32.

This is the code for ESP32.

void webSocketEvent(WStype_t type, uint8_t *payload, size_t length) { switch (type) { ... case WStype_TEXT: String text = String((char *)payload); doc.clear(); DeserializationError error = deserializeJson(doc, text); if (error) { break; } const char *type = doc["type"]; if (strcmp(type, "challenge") == 0) { challengeNonce = doc["data"].as<String>(); challengeSignal = true; } break; } } while (true) { webSocket.loop(); if (challengeSignal) { challengeSignal = false; ... webSocket.sendTXT(responseJson); } vTaskDelay(pdMS_TO_TICKS(1)); }

and this is the server code.

@router.websocket("/ws/data/{client_id}") async def websocket_data_endpoint(websocket: WebSocket, client_id: str, background: BackgroundTasks): await websocket.accept() nonce = secrets.token_hex(128) payload = { "type": "challenge", "data": nonce } await websocket.send_json(payload) try: data = await websocket.receive_text() print(data) ...

I searched for relative problems but I found none and asking AI still didn't solve the problem.

I would like to know if anyone knows how to fix this. Thanks.

Read Entire Article