Further improvements on NFC writing

Fixes some issues related to tag writing. Allos writing of tags that are already on the scale when pressing the write button, but introduces a confirmation dialog before doing so. Also first test to fix reset issue when trying to write tags.
This commit is contained in:
Jan Philipp Ecker
2025-08-07 21:12:01 +02:00
parent 09f4c43f89
commit b95497aec2
2 changed files with 90 additions and 71 deletions

View File

@@ -7,6 +7,7 @@ let heartbeatTimer = null;
let lastHeartbeatResponse = Date.now();
const HEARTBEAT_TIMEOUT = 20000;
let reconnectTimer = null;
let spoolDetected = false;
// WebSocket Funktionen
function startHeartbeat() {
@@ -508,12 +509,15 @@ function updateNfcStatusIndicator(data) {
if (data.found === 0) {
// Kein NFC Tag gefunden
indicator.className = 'status-circle';
spoolDetected = false;
} else if (data.found === 1) {
// NFC Tag erfolgreich gelesen
indicator.className = 'status-circle success';
spoolDetected = true;
} else {
// Fehler beim Lesen
indicator.className = 'status-circle error';
spoolDetected = true;
}
}
@@ -618,6 +622,7 @@ function updateNfcData(data) {
}
function writeNfcTag() {
if(!spoolDetected || confirm("Are you sure you want to overwrite the Tag?") == true){
const selectedText = document.getElementById("selected-filament").textContent;
if (selectedText === "Please choose...") {
alert('Please select a Spool first.');
@@ -667,8 +672,10 @@ function writeNfcTag() {
alert('Not connected to Server. Please check connection.');
}
}
}
function writeLocationNfcTag() {
if(!spoolDetected || confirm("Are you sure you want to overwrite the Tag?") == true){
const selectedText = document.getElementById("locationSelect").value;
if (selectedText === "Please choose...") {
alert('Please select a location first.');
@@ -679,6 +686,7 @@ function writeLocationNfcTag() {
location: String(selectedText)
};
if (socket?.readyState === WebSocket.OPEN) {
const writeButton = document.getElementById("writeLocationNfcButton");
writeButton.classList.add("writing");
@@ -692,6 +700,7 @@ function writeLocationNfcTag() {
alert('Not connected to Server. Please check connection.');
}
}
}
function handleWriteNfcTagResponse(success) {
const writeButton = document.getElementById("writeNfcButton");

View File

@@ -20,6 +20,7 @@ String lastSpoolId = "";
String nfcJsonData = "";
bool tagProcessed = false;
volatile bool pauseBambuMqttTask = false;
volatile bool suspendNfcReading = false;
struct NfcWriteParameterType {
bool tagType;
@@ -278,8 +279,10 @@ void writeJsonToTag(void *parameter) {
Serial.println(params->payload);
nfcReaderState = NFC_WRITING;
vTaskSuspend(RfidReaderTask);
vTaskDelay(50 / portTICK_PERIOD_MS);
suspendNfcReading = true;
//vTaskSuspend(RfidReaderTask);
// make sure to wait 600ms, after that the reading task should be waiting
vTaskDelay(1000 / portTICK_PERIOD_MS);
//pauseBambuMqttTask = true;
// aktualisieren der Website wenn sich der Status ändert
@@ -372,7 +375,8 @@ void writeJsonToTag(void *parameter) {
sendWriteResult(nullptr, success);
sendNfcData();
vTaskResume(RfidReaderTask);
suspendNfcReading = false;
//vTaskResume(RfidReaderTask);
pauseBambuMqttTask = false;
vTaskDelete(NULL);
@@ -384,7 +388,7 @@ void startWriteJsonToTag(const bool isSpoolTag, const char* payload) {
parameters->payload = strdup(payload);
// Task nicht mehrfach starten
if (nfcReaderState == NFC_IDLE) {
if (nfcReaderState == NFC_IDLE || nfcReaderState == NFC_READ_ERROR || nfcReaderState == NFC_READ_SUCCESS) {
oledShowProgressBar(0, 1, "Write Tag", "Place tag now");
// Erstelle die Task
xTaskCreate(
@@ -405,7 +409,7 @@ void scanRfidTask(void * parameter) {
Serial.println("RFID Task gestartet");
for(;;) {
// Wenn geschrieben wird Schleife aussetzen
if (nfcReaderState != NFC_WRITING)
if (nfcReaderState != NFC_WRITING && !suspendNfcReading)
{
yield();
@@ -413,7 +417,7 @@ void scanRfidTask(void * parameter) {
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength;
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 1000);
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 500);
foundNfcTag(nullptr, success);
@@ -430,7 +434,7 @@ void scanRfidTask(void * parameter) {
oledShowProgressBar(0, octoEnabled?5:4, "Reading", "Detecting tag");
vTaskDelay(500 / portTICK_PERIOD_MS);
//vTaskDelay(500 / portTICK_PERIOD_MS);
if (uidLength == 7)
{
@@ -487,7 +491,7 @@ void scanRfidTask(void * parameter) {
}
}
if (!success && nfcReaderState != NFC_IDLE)
if (!success && nfcReaderState != NFC_IDLE && !suspendNfcReading)
{
nfcReaderState = NFC_IDLE;
//uidString = "";
@@ -500,6 +504,12 @@ void scanRfidTask(void * parameter) {
// aktualisieren der Website wenn sich der Status ändert
sendNfcData();
}
else
{
// TBD: Debug only:
Serial.println("NFC Reading disabled");
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
yield();
}
}