2

I have a function that reads an RFID card and returns the RFID string. The function reads thr 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 to serial monitor the RFID field is null. Why does this happen? I tried to write another string there, it works well with the other string but why doesn't it with str_rfid?

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

ocrdu
  • 1,673
  • 2
  • 9
  • 22
  • 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 09 '20 at 23:52
  • @hcheung sir how can fix this, what should i do to fix this problem. – Enver Pasha Dec 10 '20 at 08: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
  • @Enver-Pasha: It could be your JSON object capacity is too small because of string duplication when you deserialise to print it. You could try a capacity like JSON_OBJECT_SIZE(3) + 500 for deserialising, just for testing. Have a look at https://arduinojson.org/v5/assistant/ for more. Also see https://arduinojson.org/v6/how-to/determine-the-capacity-of-the-jsondocument/ . Not sure it is the problem, but worth a try. Also: you could try: doc["rfid"] = str_rfid.c_str(); to see if that works better (CString instead of String object). – ocrdu Dec 10 '20 at 19:45
  • @ocrdu Sir tank you so much it worked. – Enver Pasha Dec 10 '20 at 19:52
  • Which one worked? – ocrdu Dec 10 '20 at 19:53
  • @ocrdu The second one , doc["rfid"] = str_rfid.c_str(); this worked. – Enver Pasha Dec 10 '20 at 19:53
  • 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

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,673
  • 2
  • 9
  • 22