RFID scan missing last two letter

Hi, I have this code working almost perfect, I took parts from other various projects to make this. The RFID scan however only capture the first 8 characters of the RFID and I am not sure why. I think it may have something to do with the buffer and size commands. I know it should be 10 characters because I scan the same RFID with a raspberry pi setup and I get exactly the same thing except an additional 2 letters at the end.
Any thoughts
thanks

Here is the code
*/

#include
#include
#include "MFRC522.h"
#include

/* wiring the MFRC522 to ESP8266 (ESP-12)
RST = GPIO5
SDA(SS) = GPIO4
MOSI = GPIO13
MISO = GPIO12
SCK = GPIO14
GND = GND
3.3V = 3.3V
*/

#define RST_PIN 5 // RST-PIN für RC522 - RFID - SPI - Modul GPIO5
#define SS_PIN 4 // SDA-PIN für RC522 - RFID - SPI - Modul GPIO4

char message_buff[200];

const char *ssid = "xxx"; // change according to your Network - cannot be longer than 32 characters!
const char pass = "xxxxx"; // change according to your Network
const char
mqtt_server = "xxxx";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance

void setup() {
Serial.begin(115200); // Initialize serial communications
delay(250);
Serial.println(F("Booting...."));
client.setServer(mqtt_server, 1883);

SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522

WiFi.begin(ssid, pass);

int retries = 0;
while ((WiFi.status() != WL_CONNECTED) && (retries < 10)) {
retries++;
delay(500);
Serial.print(".");
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println(F("WiFi connected"));
}

Serial.println(F("Ready!"));
Serial.println(F("======================================================"));
Serial.println(F("Scan for Card and print UID:"));

}

byte ActualUID[6];

void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
delay(100);
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
delay(100);
return;
}
// Show some details of the PICC (that is: the tag/card)
Serial.print(F("Card UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
//Serial.println();
}

// Helper routine to dump a byte array as hex values to Serial
void dump_byte_array(byte buffer, byte bufferSize) {
String uid;
String rfidUid = "";
for (byte i = 0; i < bufferSize; i++) {
// Serial.print(buffer < 0x10 ? " 0" : " ");
_ // Serial.print(buffer
, HEX);_
_
// Serial.println(msg);_
_// Serial.print(buffer, HEX);
uid = uid + String(buffer, HEX);
ActualUID=mfrc522.uid.uidByte;
//Serial.println(uid);
rfidUid += String(mfrc522.uid.uidByte < 0x10 ? "0" : "");
rfidUid += String(mfrc522.uid.uidByte, HEX);
}
if (!client.connected()) {
}
client.loop();
// long now = millis();
// if (now - lastMsg > 2000) {
// lastMsg = now;
// ++value;
// snprintf (msg, 75, "This is msg store #%ld", value);
// Serial.print("Publish message: ");
// Serial.println(msg);
Serial.print("Attempting MQTT connection...");
if (client.connect("arduinoClient", "xxx", "xxxxx"))
//client.publish("outTopic", "test" );_

// rfidUid.toCharArray(message_buff, rfidUid.length() + 0);
uid.toCharArray(message_buff, 20);
client.publish("outTopic", message_buff);
_ Serial.println("UID is:");
Serial.println(uid);
Serial.println(rfidUid);
delay(500);
} *_

Guessing, I expect the ID has a CRC check digit or two. You may be displaying those with your Pi program but not on the Arduino.

Possible I guess. I also noticed that in any of the other examples with people doing similar, their UID codes from the scans are also 8 digits. So maybe it really is only 8 digits.

thanks

dump_byte_array does not write anything from the buffer to serial at all, so you are missing everything?

This is how you are supposted to dump an array to serial:

void dump_byte_array(byte *buffer, int bufferSize)
{
  for (int i = 0; i < bufferSize; i++) Serial.print(buffer[i], HEX);
  Serial.println();
}

EDIT: Do not use the "String" object (search the forum for reason).
EDIT2: Clarification.

Not sure I follow. It works, I get all 8 characters printed to the screen and stored in my variables.

mpovolo:
Not sure I follow. It works, I get all 8 characters printed to the screen and stored in my variables.

Not with the code you posted, the following snippet is IMO defunct:

for (byte i = 0; i < bufferSize; i++) {
  //  Serial.print(buffer < 0x10 ? " 0" : " ");
   // Serial.print(buffer, HEX);
 //   Serial.println(msg);
 //   Serial.print(buffer, HEX);
    uid = uid + String(buffer, HEX); //1
    ActualUID=mfrc522.uid.uidByte; //2
    //Serial.println(uid);
    rfidUid += String(mfrc522.uid.uidByte < 0x10 ? "0" : ""); //3
    rfidUid += String(mfrc522.uid.uidByte, HEX); //3
 }
  1. Converting a pointer address to hex, not the data it points to.
  2. Re-assigning an array, not moving its contents.
  3. Again, wrong usage of arrays / pointers; No elements are accessed.

Try the method I posted above and see if it produces the same output as yours. If not, your code is wrong :slight_smile:

Thanks a bunch, will try it tonight

And by the way.. According to the RFID library, the UID is either 4, 7 or 10 bytes long - since you are getting 8 bytes, something MUST be wrong with your code. Good luck!