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:
@@ -7,6 +7,7 @@ let heartbeatTimer = null;
|
|||||||
let lastHeartbeatResponse = Date.now();
|
let lastHeartbeatResponse = Date.now();
|
||||||
const HEARTBEAT_TIMEOUT = 20000;
|
const HEARTBEAT_TIMEOUT = 20000;
|
||||||
let reconnectTimer = null;
|
let reconnectTimer = null;
|
||||||
|
let spoolDetected = false;
|
||||||
|
|
||||||
// WebSocket Funktionen
|
// WebSocket Funktionen
|
||||||
function startHeartbeat() {
|
function startHeartbeat() {
|
||||||
@@ -508,12 +509,15 @@ function updateNfcStatusIndicator(data) {
|
|||||||
if (data.found === 0) {
|
if (data.found === 0) {
|
||||||
// Kein NFC Tag gefunden
|
// Kein NFC Tag gefunden
|
||||||
indicator.className = 'status-circle';
|
indicator.className = 'status-circle';
|
||||||
|
spoolDetected = false;
|
||||||
} else if (data.found === 1) {
|
} else if (data.found === 1) {
|
||||||
// NFC Tag erfolgreich gelesen
|
// NFC Tag erfolgreich gelesen
|
||||||
indicator.className = 'status-circle success';
|
indicator.className = 'status-circle success';
|
||||||
|
spoolDetected = true;
|
||||||
} else {
|
} else {
|
||||||
// Fehler beim Lesen
|
// Fehler beim Lesen
|
||||||
indicator.className = 'status-circle error';
|
indicator.className = 'status-circle error';
|
||||||
|
spoolDetected = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -618,6 +622,7 @@ function updateNfcData(data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function writeNfcTag() {
|
function writeNfcTag() {
|
||||||
|
if(!spoolDetected || confirm("Are you sure you want to overwrite the Tag?") == true){
|
||||||
const selectedText = document.getElementById("selected-filament").textContent;
|
const selectedText = document.getElementById("selected-filament").textContent;
|
||||||
if (selectedText === "Please choose...") {
|
if (selectedText === "Please choose...") {
|
||||||
alert('Please select a Spool first.');
|
alert('Please select a Spool first.');
|
||||||
@@ -666,9 +671,11 @@ function writeNfcTag() {
|
|||||||
} else {
|
} else {
|
||||||
alert('Not connected to Server. Please check connection.');
|
alert('Not connected to Server. Please check connection.');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeLocationNfcTag() {
|
function writeLocationNfcTag() {
|
||||||
|
if(!spoolDetected || confirm("Are you sure you want to overwrite the Tag?") == true){
|
||||||
const selectedText = document.getElementById("locationSelect").value;
|
const selectedText = document.getElementById("locationSelect").value;
|
||||||
if (selectedText === "Please choose...") {
|
if (selectedText === "Please choose...") {
|
||||||
alert('Please select a location first.');
|
alert('Please select a location first.');
|
||||||
@@ -679,6 +686,7 @@ function writeLocationNfcTag() {
|
|||||||
location: String(selectedText)
|
location: String(selectedText)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
if (socket?.readyState === WebSocket.OPEN) {
|
if (socket?.readyState === WebSocket.OPEN) {
|
||||||
const writeButton = document.getElementById("writeLocationNfcButton");
|
const writeButton = document.getElementById("writeLocationNfcButton");
|
||||||
writeButton.classList.add("writing");
|
writeButton.classList.add("writing");
|
||||||
@@ -691,6 +699,7 @@ function writeLocationNfcTag() {
|
|||||||
} else {
|
} else {
|
||||||
alert('Not connected to Server. Please check connection.');
|
alert('Not connected to Server. Please check connection.');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleWriteNfcTagResponse(success) {
|
function handleWriteNfcTagResponse(success) {
|
||||||
|
26
src/nfc.cpp
26
src/nfc.cpp
@@ -20,6 +20,7 @@ String lastSpoolId = "";
|
|||||||
String nfcJsonData = "";
|
String nfcJsonData = "";
|
||||||
bool tagProcessed = false;
|
bool tagProcessed = false;
|
||||||
volatile bool pauseBambuMqttTask = false;
|
volatile bool pauseBambuMqttTask = false;
|
||||||
|
volatile bool suspendNfcReading = false;
|
||||||
|
|
||||||
struct NfcWriteParameterType {
|
struct NfcWriteParameterType {
|
||||||
bool tagType;
|
bool tagType;
|
||||||
@@ -278,8 +279,10 @@ void writeJsonToTag(void *parameter) {
|
|||||||
Serial.println(params->payload);
|
Serial.println(params->payload);
|
||||||
|
|
||||||
nfcReaderState = NFC_WRITING;
|
nfcReaderState = NFC_WRITING;
|
||||||
vTaskSuspend(RfidReaderTask);
|
suspendNfcReading = true;
|
||||||
vTaskDelay(50 / portTICK_PERIOD_MS);
|
//vTaskSuspend(RfidReaderTask);
|
||||||
|
// make sure to wait 600ms, after that the reading task should be waiting
|
||||||
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||||
|
|
||||||
//pauseBambuMqttTask = true;
|
//pauseBambuMqttTask = true;
|
||||||
// aktualisieren der Website wenn sich der Status ändert
|
// aktualisieren der Website wenn sich der Status ändert
|
||||||
@@ -372,7 +375,8 @@ void writeJsonToTag(void *parameter) {
|
|||||||
sendWriteResult(nullptr, success);
|
sendWriteResult(nullptr, success);
|
||||||
sendNfcData();
|
sendNfcData();
|
||||||
|
|
||||||
vTaskResume(RfidReaderTask);
|
suspendNfcReading = false;
|
||||||
|
//vTaskResume(RfidReaderTask);
|
||||||
pauseBambuMqttTask = false;
|
pauseBambuMqttTask = false;
|
||||||
|
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
@@ -384,7 +388,7 @@ void startWriteJsonToTag(const bool isSpoolTag, const char* payload) {
|
|||||||
parameters->payload = strdup(payload);
|
parameters->payload = strdup(payload);
|
||||||
|
|
||||||
// Task nicht mehrfach starten
|
// 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");
|
oledShowProgressBar(0, 1, "Write Tag", "Place tag now");
|
||||||
// Erstelle die Task
|
// Erstelle die Task
|
||||||
xTaskCreate(
|
xTaskCreate(
|
||||||
@@ -405,7 +409,7 @@ void scanRfidTask(void * parameter) {
|
|||||||
Serial.println("RFID Task gestartet");
|
Serial.println("RFID Task gestartet");
|
||||||
for(;;) {
|
for(;;) {
|
||||||
// Wenn geschrieben wird Schleife aussetzen
|
// Wenn geschrieben wird Schleife aussetzen
|
||||||
if (nfcReaderState != NFC_WRITING)
|
if (nfcReaderState != NFC_WRITING && !suspendNfcReading)
|
||||||
{
|
{
|
||||||
yield();
|
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 uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
|
||||||
uint8_t uidLength;
|
uint8_t uidLength;
|
||||||
|
|
||||||
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 1000);
|
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 500);
|
||||||
|
|
||||||
foundNfcTag(nullptr, success);
|
foundNfcTag(nullptr, success);
|
||||||
|
|
||||||
@@ -430,7 +434,7 @@ void scanRfidTask(void * parameter) {
|
|||||||
|
|
||||||
oledShowProgressBar(0, octoEnabled?5:4, "Reading", "Detecting tag");
|
oledShowProgressBar(0, octoEnabled?5:4, "Reading", "Detecting tag");
|
||||||
|
|
||||||
vTaskDelay(500 / portTICK_PERIOD_MS);
|
//vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||||
|
|
||||||
if (uidLength == 7)
|
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;
|
nfcReaderState = NFC_IDLE;
|
||||||
//uidString = "";
|
//uidString = "";
|
||||||
@@ -500,6 +504,12 @@ void scanRfidTask(void * parameter) {
|
|||||||
// aktualisieren der Website wenn sich der Status ändert
|
// aktualisieren der Website wenn sich der Status ändert
|
||||||
sendNfcData();
|
sendNfcData();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// TBD: Debug only:
|
||||||
|
Serial.println("NFC Reading disabled");
|
||||||
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||||
|
}
|
||||||
yield();
|
yield();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user