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.
-
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– hcheung2020年12月09日 23:52:26 +00:00Commented Dec 9, 2020 at 23:52
-
@hcheung sir how can fix this, what should i do to fix this problem.Enver Pasha– Enver Pasha2020年12月10日 08:37:34 +00:00Commented Dec 10, 2020 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– ocrdu2020年12月10日 19:33:47 +00:00Commented Dec 10, 2020 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– Enver Pasha2020年12月10日 19:42:38 +00:00Commented Dec 10, 2020 at 19:42
-
1So 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– ocrdu2020年12月10日 20:09:55 +00:00Commented Dec 10, 2020 at 20:09
1 Answer 1
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.