I have a function that reads rfid card and return rfid string. Function reads string well but. I am using ArduinoJson libray 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));
  return kartid;

and in the loop function , if button pressed i am getting an rfid and ı 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 to serial monitor rfid field is null. Why it happens. I tried to write other string there it works well with other string but why it does not with str_rfid. I also print value of str_rfid using Serial.print str_rfid getting rfid card id from function well.

  • Your function create a local variable and return it value at end of the function, a local variable will out of Scope upon exiting the function and no longer available. Furthermore, String object uses dynamic memory allocation in the heap, and get free up when it is out of scope. Read a book about C++ programming and try not to use String object if you are new to Arduino/C++ programming. – hcheung Dec 9 '20 at 23:52
  • @hcheung sir how can fix this, what should i do to fix this problem. – Enver Pasha Dec 10 '20 at 8:37
  • @hcheung: The function returns a String, and that return value is used and present, according to the OP. Variable scope has nothing to do with it. – ocrdu Dec 10 '20 at 19:33
  • @ocrdu the function works and returns string, i output it using serial print. It is correct. But when i assign it to doc["rfid"], the in the json output the field is null. I created a string to test String a = "example" i assign it to the doc["rfid"] , in the json i show this value it works well with other strings. – Enver Pasha Dec 10 '20 at 19:42
  • 1
    So maybe ArduinoJson doesn't play nice with String objects, or there is some other reason I don't know about. I'll write an answer based on my comment; please accept it (if you accept it 8-). – ocrdu Dec 10 '20 at 20:09

It could be 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.