Hi all,
I have a sketch that reads the UID of an NFC tags and is supposed to connect this with a directory name. Basically what I want to achieve is a file that shall look like this, when everything works out:
43322634231761291:larsrent
The first part is the UID of the NFC tag, the second part after the : is the directory name.
The function that is supposed to do write this key:value pair to a file on the SD card, is this:
boolean createAlbumDbEntry() {
boolean success = false;
byte bytesWritten;
memset(trackDbEntry, 0, sizeof(trackDbEntry));
Serial.print(F("writing nfc <-> album connection to disk: "));
for (byte i = 0; i <= uidLength ; i++) Serial.print(uid[i]);
Serial.print(F(":"));
Serial.print(plrCurrentFolder);
Serial.println(F("\n"));
memcpy(trackDbEntry, charUid, sizeof(charUid));
strcat(trackDbEntry, ":");
strcat(trackDbEntry, plrCurrentFolder);
strcat(trackDbEntry, "\n");
File file = SD.open(trackDbFile, FILE_WRITE);
bytesWritten = file.write(trackDbEntry, sizeof(trackDbEntry));
file.flush();
if (bytesWritten == sizeof(trackDbEntry)) success = true; else success = false;
Serial.print(F("written "));Serial.print(bytesWritten);Serial.print(F(" bytes to disk"));
return (success);
}
I’m using these 2 global variables to convert the UID as returned from the NFC library to a char array which I want to write to the file:
byte uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
char charUid[18]; // char representation of the UID
I think I need to do this, because I need the UID as a consecutive string as opposed to the comma separated representation that is returned by the call to read the NFC tag:
nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
My problem is that the entry in the file looks like this:
!‚"ÁLÅ:larsrent
I can even track that it doesn’t work, as I get the following serial output from my Serial.print() statements:
SD-Card ready
NFC TAG READER:
Found chip PN532
Firmware ver. 1.6
Scan your NFC tag
___________________________________________________
43322634231761290
Send a character to connect !⸮"⸮L⸮
with directory / album larsrent
writing nfc <-> album connection to disk: 43322634231761290:larsrent
written 27 bytes to disk433
Whereas I would have expected the following output:
SD-Card ready
NFC TAG READER:
Found chip PN532
Firmware ver. 1.6
Scan your NFC tag
___________________________________________________
43322634231761290
Send a character to connect 43322634231761290 with directory / album larsrent
writing nfc <-> album connection to disk: 43322634231761290:larsrent
written 27 bytes to disk
The complete sketch is attached.
Can anyone tell me what I’m doing wrong here???
Kind regards,
Chris
getNfcTagUid_SM.ino (11.7 KB)