feat: enhance RFID handling for Bambu tags and update API interactions
This commit is contained in:
1 parent
29e8b09c5a
commit
5f9ad13ea9
7 files changed
+155
-45
No files matched your search
+26
-5
@@ -21,6 +21,7 @@ struct ApiRequest {
|
||||
float val;
|
||||
bool bool1; // success
|
||||
String str3; // error message
|
||||
float remainingWeight; // remaining weight from rfid-result
|
||||
bool active = false;
|
||||
};
|
||||
|
||||
@@ -92,19 +93,28 @@ bool sendHeartbeat() {
|
||||
}
|
||||
|
||||
bool sendWeight(int spoolId, String tagUuid, float measuredWeight) {
|
||||
if (!checkFilamanRegistration() || WiFi.status() != WL_CONNECTED) return false;
|
||||
Serial.printf("sendWeight: sending to API - spoolId=%d, tagUuid=%s, weight=%.1f\n", spoolId, tagUuid.c_str(), measuredWeight);
|
||||
if (!checkFilamanRegistration() || WiFi.status() != WL_CONNECTED) {
|
||||
Serial.println("ERROR: Not registered or WiFi not connected");
|
||||
return false;
|
||||
}
|
||||
HTTPClient http;
|
||||
http.setTimeout(3000);
|
||||
http.begin(filamanUrl + "/api/v1/devices/scale/weight");
|
||||
http.addHeader("Content-Type", "application/json");
|
||||
http.addHeader("Authorization", "Device " + filamanToken);
|
||||
JsonDocument doc;
|
||||
// Only add spool_id if it's > 0 (for NTAG tags with spool ID)
|
||||
// For Bambu tags (spoolId == 0), only send tag_uuid
|
||||
if (spoolId > 0) doc["spool_id"] = spoolId;
|
||||
// Always add tag_uuid if available (this is what we want for Bambu tags)
|
||||
if (tagUuid.length() > 0) doc["tag_uuid"] = tagUuid;
|
||||
doc["measured_weight_g"] = measuredWeight;
|
||||
String payload;
|
||||
serializeJson(doc, payload);
|
||||
Serial.printf("API payload: %s\n", payload.c_str());
|
||||
int httpCode = http.POST(payload);
|
||||
Serial.printf("API response code: %d\n", httpCode);
|
||||
|
||||
if (httpCode == 200) {
|
||||
String response = http.getString();
|
||||
@@ -150,7 +160,7 @@ bool sendLocation(int spoolId, String spoolTagUuid, int locationId, String locat
|
||||
return (httpCode == 200);
|
||||
}
|
||||
|
||||
bool sendRfidResult(String tagUuid, int spoolId, int locationId, bool success, String errorMessage) {
|
||||
bool sendRfidResult(String tagUuid, int spoolId, int locationId, bool success, String errorMessage, float remainingWeight) {
|
||||
if (!checkFilamanRegistration() || WiFi.status() != WL_CONNECTED) return false;
|
||||
HTTPClient http;
|
||||
http.setTimeout(5000);
|
||||
@@ -164,6 +174,7 @@ bool sendRfidResult(String tagUuid, int spoolId, int locationId, bool success, S
|
||||
if (spoolId > 0) doc["spool_id"] = spoolId;
|
||||
if (locationId > 0) doc["location_id"] = locationId;
|
||||
if (errorMessage.length() > 0) doc["error_message"] = errorMessage;
|
||||
if (remainingWeight > 0) doc["remaining_weight_g"] = remainingWeight;
|
||||
|
||||
String payload;
|
||||
serializeJson(doc, payload);
|
||||
@@ -195,7 +206,7 @@ void filamanApiTask(void* pvParameters) {
|
||||
case API_REQUEST_HEARTBEAT: sendHeartbeat(); break;
|
||||
case API_REQUEST_WEIGHT: sendWeight(req.id1, req.str1, req.val); break;
|
||||
case API_REQUEST_LOCATE: sendLocation(req.id1, req.str1, req.id2, req.str2); break;
|
||||
case API_REQUEST_RFID_RESULT: sendRfidResult(req.str1, req.id1, req.id2, req.bool1, req.str3); break;
|
||||
case API_REQUEST_RFID_RESULT: sendRfidResult(req.str1, req.id1, req.id2, req.bool1, req.str3, req.remainingWeight); break;
|
||||
default: break;
|
||||
}
|
||||
filamanApiState = API_IDLE;
|
||||
@@ -227,7 +238,15 @@ void sendHeartbeatAsync() {
|
||||
}
|
||||
|
||||
void sendWeightAsync(int spoolId, String tagUuid, float weight) {
|
||||
if (!checkFilamanRegistration()) return;
|
||||
Serial.printf("sendWeightAsync: spoolId=%d, tagUuid=%s, weight=%.1f\n", spoolId, tagUuid.c_str(), weight);
|
||||
if (!checkFilamanRegistration()) {
|
||||
Serial.println("ERROR: Not registered with FilaMan, cannot send weight");
|
||||
return;
|
||||
}
|
||||
if (weight <= 0) {
|
||||
Serial.println("ERROR: Weight is 0 or negative, cannot send");
|
||||
return;
|
||||
}
|
||||
if (xSemaphoreTake(queueMutex, pdMS_TO_TICKS(50)) == pdTRUE) {
|
||||
for(int i=0; i<MAX_API_QUEUE; i++) if(!apiQueue[i].active) {
|
||||
apiQueue[i].type = API_REQUEST_WEIGHT;
|
||||
@@ -237,6 +256,7 @@ void sendWeightAsync(int spoolId, String tagUuid, float weight) {
|
||||
apiQueue[i].str2 = "";
|
||||
apiQueue[i].val = weight;
|
||||
apiQueue[i].active = true;
|
||||
Serial.printf("Weight queued for API (slot %d)\n", i);
|
||||
break;
|
||||
}
|
||||
xSemaphoreGive(queueMutex);
|
||||
@@ -260,7 +280,7 @@ void sendLocationAsync(int spoolId, String spoolTagUuid, int locationId, String
|
||||
}
|
||||
}
|
||||
|
||||
void sendRfidResultAsync(String tagUuid, int spoolId, int locationId, bool success, String errorMessage) {
|
||||
void sendRfidResultAsync(String tagUuid, int spoolId, int locationId, bool success, String errorMessage, float remainingWeight) {
|
||||
if (!checkFilamanRegistration()) return;
|
||||
if (xSemaphoreTake(queueMutex, pdMS_TO_TICKS(50)) == pdTRUE) {
|
||||
for(int i=0; i<MAX_API_QUEUE; i++) if(!apiQueue[i].active) {
|
||||
@@ -270,6 +290,7 @@ void sendRfidResultAsync(String tagUuid, int spoolId, int locationId, bool succe
|
||||
apiQueue[i].id2 = locationId;
|
||||
apiQueue[i].bool1 = success;
|
||||
apiQueue[i].str3 = errorMessage;
|
||||
apiQueue[i].remainingWeight = remainingWeight;
|
||||
apiQueue[i].active = true;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -29,13 +29,13 @@ bool registerDevice(const String& deviceCode);
|
||||
void sendHeartbeatAsync();
|
||||
void sendWeightAsync(int spoolId, String tagUuid, float weight);
|
||||
void sendLocationAsync(int spoolId, String spoolTagUuid, int locationId, String locationTagUuid);
|
||||
void sendRfidResultAsync(String tagUuid, int spoolId, int locationId, bool success, String errorMessage);
|
||||
void sendRfidResultAsync(String tagUuid, int spoolId, int locationId, bool success, String errorMessage, float remainingWeight = 0);
|
||||
|
||||
// Internal blocking functions (used by async task)
|
||||
bool sendHeartbeat();
|
||||
bool sendWeight(int spoolId, String tagUuid, float weight);
|
||||
bool sendLocation(int spoolId, String spoolTagUuid, int locationId, String locationTagUuid);
|
||||
bool sendRfidResult(String tagUuid, int spoolId, int locationId, bool success, String errorMessage);
|
||||
bool sendRfidResult(String tagUuid, int spoolId, int locationId, bool success, String errorMessage, float remainingWeight = 0);
|
||||
|
||||
// Helper functions
|
||||
void saveFilamanConfig();
|
||||
|
||||
+10
-6
@@ -214,15 +214,19 @@ void loop() {
|
||||
// Wenn ein Tag erkannt wurde und das Gewicht stabil ist, an FilaMan senden
|
||||
if (weightCounterToApi > 3 && weightSend == 0 && nfcReaderState == NFC_READ_SUCCESS && tagProcessed == false)
|
||||
{
|
||||
// set the current tag as processed to prevent it beeing processed again
|
||||
tagProcessed = true;
|
||||
|
||||
// Get UID from nfcJsonData or similar (nfc.cpp should set it)
|
||||
// For now, use activeSpoolId if available
|
||||
int sId = activeSpoolId.toInt();
|
||||
sendWeightAsync(sId, activeTagUuid, weight);
|
||||
// Check if it's a Bambu tag - if so, send only UUID without spoolId
|
||||
if (isBambuTag) {
|
||||
sendWeightAsync(0, activeTagUuid, weight);
|
||||
Serial.println("Bambu weight queued for FilaMan (UUID only)");
|
||||
} else {
|
||||
// Normal NTAG: send spoolId + UUID
|
||||
int sId = activeSpoolId.toInt();
|
||||
sendWeightAsync(sId, activeTagUuid, weight);
|
||||
Serial.println("Weight queued for FilaMan");
|
||||
}
|
||||
weightSend = 1;
|
||||
Serial.println("Weight queued for FilaMan");
|
||||
|
||||
// Feedback to user
|
||||
pauseMainTask = 1;
|
||||
|
||||
+78
-18
@@ -22,6 +22,7 @@ String activeTagUuid = "";
|
||||
String lastSpoolId = "";
|
||||
String nfcJsonData = "";
|
||||
bool tagProcessed = false;
|
||||
bool isBambuTag = false;
|
||||
volatile bool nfcReadingTaskSuspendRequest = false;
|
||||
volatile bool nfcReadingTaskSuspendState = false;
|
||||
volatile bool nfcWriteInProgress = false; // Prevent any tag operations during write
|
||||
@@ -36,6 +37,41 @@ volatile nfcReaderStateType nfcReaderState = NFC_IDLE;
|
||||
// 6 = reading
|
||||
// ***** PN532
|
||||
|
||||
// ##### Bambu Tag Helper Functions #####
|
||||
// Simplified: only read UID, no decryption needed (UID is always visible)
|
||||
bool detectBambuTag(const uint8_t* uid, uint8_t uidLength) {
|
||||
Serial.println("Detected Bambu Lab tag (Mifare Classic) - reading UID only");
|
||||
|
||||
// Create UID string with separators (same format as NTAG)
|
||||
String uidString = "";
|
||||
for (uint8_t i = 0; i < uidLength; i++) {
|
||||
uidString += String(uid[i], HEX);
|
||||
if (i < uidLength - 1) {
|
||||
uidString += ":";
|
||||
}
|
||||
}
|
||||
uidString.toUpperCase();
|
||||
|
||||
Serial.print(" UID: ");
|
||||
Serial.println(uidString);
|
||||
|
||||
isBambuTag = true;
|
||||
activeTagUuid = uidString;
|
||||
activeSpoolId = ""; // Will be resolved by FilaMan API based on tag UID
|
||||
|
||||
// Create minimal JSON for compatibility
|
||||
JsonDocument doc;
|
||||
doc["vendor"] = "Bambu Lab";
|
||||
doc["type"] = "Bambu";
|
||||
nfcJsonData = "";
|
||||
serializeJson(doc, nfcJsonData);
|
||||
|
||||
// Set normal read success state - isBambuTag flag is set for later use
|
||||
nfcReaderState = NFC_READ_SUCCESS;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// ##### Funktionen für RFID #####
|
||||
void payloadToJson(uint8_t *data) {
|
||||
const char* startJson = strchr((char*)data, '{');
|
||||
@@ -1590,11 +1626,11 @@ void writeJsonToTag(void *parameter) {
|
||||
// Wait up to 30 seconds for tag
|
||||
uint8_t success = 0;
|
||||
String uidString = "";
|
||||
uint8_t uidLength = 0;
|
||||
unsigned long startTime = millis();
|
||||
|
||||
while (millis() - startTime < 30000) {
|
||||
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
|
||||
uint8_t uidLength;
|
||||
// yield before potentially waiting for 400ms
|
||||
yield();
|
||||
esp_task_wdt_reset();
|
||||
@@ -1608,7 +1644,7 @@ void writeJsonToTag(void *parameter) {
|
||||
uidString += ":"; // Trennzeichen hinzufügen
|
||||
}
|
||||
}
|
||||
foundNfcTag(nullptr, success);
|
||||
uidString.toUpperCase();
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1619,6 +1655,25 @@ void writeJsonToTag(void *parameter) {
|
||||
|
||||
if (success)
|
||||
{
|
||||
// Check if this is a Bambu tag (Mifare Classic has 4 byte UID, NTAG has 7 byte)
|
||||
// If so, skip writing and just send success with weight to API
|
||||
if (uidLength != 7) {
|
||||
Serial.println("Bambu Lab tag detected during write - skipping write, sending result with weight");
|
||||
oledShowProgressBar(1, 1, "Write Tag", "Done!");
|
||||
|
||||
// Send success to API with tag_uuid and current weight
|
||||
sendRfidResultAsync(uidString, params->spoolId, params->locationId, true, "", weight);
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
|
||||
// Clean up and exit
|
||||
nfcWriteInProgress = false;
|
||||
nfcReaderState = NFC_IDLE;
|
||||
free(params->payload);
|
||||
delete params;
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
oledShowProgressBar(1, 3, "Write Tag", "Writing");
|
||||
|
||||
// Schreibe die NDEF-Message auf den Tag
|
||||
@@ -1710,9 +1765,9 @@ void writeJsonToTag(void *parameter) {
|
||||
|
||||
Serial.println("=========================================");
|
||||
|
||||
// Send success response to API with tag_uuid (with separators)
|
||||
// Send success response to API with tag_uuid and current weight
|
||||
Serial.println("Sending result to API via fire-and-forget...");
|
||||
sendRfidResultAsync(uidString, params->spoolId, params->locationId, true, "");
|
||||
sendRfidResultAsync(uidString, params->spoolId, params->locationId, true, "", weight);
|
||||
|
||||
// vTaskResume(RfidReaderTask); // Don't resume as it was not suspended
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
@@ -1740,9 +1795,6 @@ void writeJsonToTag(void *parameter) {
|
||||
Serial.println("Sending timeout result to API via fire-and-forget...");
|
||||
sendRfidResultAsync("", params->spoolId, params->locationId, false, "Timeout - no tag found");
|
||||
}
|
||||
|
||||
sendWriteResult(nullptr, success);
|
||||
sendNfcData();
|
||||
|
||||
// Reset write protection and cleanup
|
||||
nfcWriteInProgress = false; // Re-enable high-level tag operations
|
||||
@@ -2000,21 +2052,28 @@ void scanRfidTask(void * parameter) {
|
||||
}
|
||||
else
|
||||
{
|
||||
oledShowProgressBar(1, 1, "Failure", "Tag read error");
|
||||
nfcReaderState = NFC_READ_ERROR;
|
||||
// Reset activeSpoolId when tag reading fails to prevent autoSet
|
||||
activeSpoolId = "";
|
||||
Serial.println("Tag read failed - activeSpoolId reset to prevent autoSet");
|
||||
// NTAG reading failed, try reading as Bambu Lab tag
|
||||
Serial.println("NTAG read failed, trying Bambu Lab tag...");
|
||||
if (!detectBambuTag(uid, uidLength)) {
|
||||
oledShowProgressBar(1, 1, "Failure", "Tag read error");
|
||||
nfcReaderState = NFC_READ_ERROR;
|
||||
activeSpoolId = "";
|
||||
Serial.println("Tag read failed - activeSpoolId reset to prevent autoSet");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//TBD: Show error here?!
|
||||
oledShowProgressBar(1, 1, "Failure", "Unkown tag type");
|
||||
Serial.println("This doesn't seem to be an NTAG2xx tag (UUID length != 7 bytes)!");
|
||||
// Reset activeSpoolId when tag type is unknown to prevent autoSet
|
||||
activeSpoolId = "";
|
||||
Serial.println("Unknown tag type - activeSpoolId reset to prevent autoSet");
|
||||
// UID length != 7, might be a Mifare Classic (Bambu tags)
|
||||
Serial.println("Not a standard NTAG (UID length != 7), trying Bambu Lab tag...");
|
||||
if (!detectBambuTag(uid, uidLength)) {
|
||||
//TBD: Show error here?!
|
||||
oledShowProgressBar(1, 1, "Failure", "Unkown tag type");
|
||||
Serial.println("This doesn't seem to be an NTAG2xx tag (UUID length != 7 bytes)!");
|
||||
// Reset activeSpoolId when tag type is unknown to prevent autoSet
|
||||
activeSpoolId = "";
|
||||
Serial.println("Unknown tag type - activeSpoolId reset to prevent autoSet");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2034,6 +2093,7 @@ void scanRfidTask(void * parameter) {
|
||||
{
|
||||
nfcReaderState = NFC_IDLE;
|
||||
tagProcessed = false;
|
||||
isBambuTag = false;
|
||||
Serial.println("Tag nach erfolgreichem Lesen entfernt - bereit für nächsten Tag");
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,9 @@ typedef enum{
|
||||
NFC_READ_ERROR,
|
||||
NFC_WRITING,
|
||||
NFC_WRITE_SUCCESS,
|
||||
NFC_WRITE_ERROR
|
||||
NFC_WRITE_ERROR,
|
||||
NFC_BAMBU_DETECTED,
|
||||
NFC_BAMBU_ERROR
|
||||
} nfcReaderStateType;
|
||||
|
||||
struct NfcWriteParameterType {
|
||||
@@ -35,7 +37,6 @@ extern String lastSpoolId;
|
||||
extern volatile nfcReaderStateType nfcReaderState;
|
||||
extern volatile bool nfcWriteInProgress;
|
||||
extern bool tagProcessed;
|
||||
extern bool isBambuTag;
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
||||
Reference in new issue
Block a user