2

I have a function that reads an RFID card and returns the RFID string. The function reads the string well but I am using the ArduinoJson library to generate JSON.

This is the function that I am using to read RFID cards:

String rfidOku() {
  String kartid= "";
  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
    for (byte i = 0; i < mfrc522.uid.size; i++) {
      kartid.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
      kartid.concat(String(mfrc522.uid.uidByte[i], HEX));
    }
    kartid.toUpperCase();
    //Serial.println(kartid); 
  }
  kartid.trim();
  return kartid;
}

And in the loop function, if a button is pressed I am getting an RFID and I am creating json using it.

const size_t capacity = JSON_OBJECT_SIZE(4);
DynamicJsonDocument doc(capacity);
const String str_rfid = rfidOku();              
doc["id"] = odeme.id;
doc["fiyat"] = odeme.fiyat;
doc["rfid"] = str_rfid;
serializeJson(doc, Serial);  

When I look at serial monitor the RFID field is null. Why does this happen? I tried to write another string there, and it works well with the other string. Why doesn't it work with str_rfid?

I also printed the value of str_rfid using Serial.print, str_rfid is getting the RFID card id from the function well.

ocrdu
  • 1,795
  • 3
  • 12
  • 24

1 Answers1

0

It could be that your JSON object capacity is too small because of string duplication when you deserialise it to print it.

You should try a capacity like JSON_OBJECT_SIZE(3) + 500 for deserialising, and see what happens.

Also, try: doc["rfid"] = str_rfid.c_str(); to see if that works better (CString instead of String object).

More information on object capacity can be found here; there's a calculator here.

ocrdu
  • 1,795
  • 3
  • 12
  • 24