From ad145c7d7ae87ca1369e9be2b3d355715c54928e Mon Sep 17 00:00:00 2001 From: Manuel Weiser Date: Thu, 19 Feb 2026 17:16:07 +0100 Subject: [PATCH] Refactor WebSocket handling: add NFC data sending to clients and improve connection logging; adjust task priority for better scheduling --- src/api.cpp | 5 +++-- src/main.cpp | 2 ++ src/website.cpp | 24 ++++++++++++++++++++---- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/api.cpp b/src/api.cpp index ccee5dc..acf7a88 100644 --- a/src/api.cpp +++ b/src/api.cpp @@ -216,8 +216,9 @@ void sendLocationAsync(int spoolId, String spoolTagUuid, int locationId, String bool initFilaman() { loadFilamanConfig(); queueMutex = xSemaphoreCreateMutex(); - // Reduce stack size to 6k and set priority to 0 (lowest) to prevent starving WiFi stack - xTaskCreatePinnedToCore(filamanApiTask, "FilaManApi", 6144, NULL, 0, NULL, 0); + // Move to Core 1 (Hardware Core) to free up Core 0 for WiFi/Webserver + // Set priority to 1 (same as Scale/NFC) to ensure fair scheduling + xTaskCreatePinnedToCore(filamanApiTask, "FilaManApi", 6144, NULL, 1, NULL, 1); if (checkFilamanRegistration()) sendHeartbeatAsync(); return true; } diff --git a/src/main.cpp b/src/main.cpp index 8cc780f..8e9a87c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -119,6 +119,8 @@ void loop() { if (intervalElapsed(currentMillis, lastTopRowUpdateTime, DISPLAY_UPDATE_INTERVAL)) { oledShowTopRow(); + // Clean up dead websocket clients periodically instead of on connect + if(currentMillis % 10000 < 50) ws.cleanupClients(); } // Periodic FilaMan heartbeat diff --git a/src/website.cpp b/src/website.cpp index bdefba0..4d80bea 100644 --- a/src/website.cpp +++ b/src/website.cpp @@ -24,12 +24,28 @@ AsyncWebSocket ws("/ws"); uint8_t lastSuccess = 0; nfcReaderStateType lastnfcReaderState = NFC_IDLE; +void sendNfcDataToClient(AsyncWebSocketClient *client) { + if(!client) return; + switch(nfcReaderState){ + case NFC_IDLE: client->text("{\"type\":\"nfcData\", \"payload\":{}}"); break; + case NFC_READ_SUCCESS: client->text("{\"type\":\"nfcData\", \"payload\":" + nfcJsonData + "}"); break; + case NFC_READ_ERROR: client->text("{\"type\":\"nfcData\", \"payload\":{\"error\":\"Read Error\"}}"); break; + case NFC_WRITING: client->text("{\"type\":\"nfcData\", \"payload\":{\"info\":\"Writing...\"}}"); break; + case NFC_WRITE_SUCCESS: client->text("{\"type\":\"nfcData\", \"payload\":{\"info\":\"Success\"}}"); break; + case NFC_WRITE_ERROR: client->text("{\"type\":\"nfcData\", \"payload\":{\"error\":\"Write Error\"}}"); break; + default: break; + } +} + void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) { if (type == WS_EVT_CONNECT) { - Serial.printf("WS Client #%u connected\n", client->id()); - sendNfcData(); - ws.text(client->id(), "{\"type\":\"nfcTag\", \"payload\":{\"found\": " + String(lastSuccess) + "}}"); - server->cleanupClients(); + Serial.printf("WS Client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str()); + sendNfcDataToClient(client); + client->text("{\"type\":\"nfcTag\", \"payload\":{\"found\": " + String(lastSuccess) + "}}"); + } else if (type == WS_EVT_DISCONNECT) { + Serial.printf("WS Client #%u disconnected\n", client->id()); + } else if (type == WS_EVT_ERROR) { + Serial.printf("WS Client #%u error: %u\n", client->id(), *((uint16_t*)arg)); } else if (type == WS_EVT_DATA) { JsonDocument doc; DeserializationError error = deserializeJson(doc, (char*)data, len);