mobizt / Firebase-ESP32

[DEPRECATED]🔥 Firebase RTDB Arduino Library for ESP32. The complete, fast, secured and reliable Firebase Arduino client library that supports CRUD (create, read, update, delete) and Stream operations.
MIT License
415 stars 118 forks source link

HELP: firebase.pushJSON with JSONObject created by ArduinoJSON #23

Closed sarkrui closed 5 years ago

sarkrui commented 5 years ago

name: Help and how to firebase.pushJSON with non-hardcoded JSON data?


const int BUFFER_SIZE = JSON_OBJECT_SIZE(3); StaticJsonBuffer<BUFFER_SIZE> jsonBuffer; JsonObject& logData = jsonBuffer.createObject(); logData["dBValue"] = 45.0; logData["time"] = "2019-05-15T15:00:25"; logData["buttonState"] = 0; Firebase.pushJSON(firebaseData, data_log + device_id, logData);

With the above codes, I had such an error.

no matching function for call to 'FirebaseESP32::pushJSON(FirebaseData&, StringSumHelper&, ArduinoJson::JsonObject&)'

I have tried using ArduinoJSON v5/v6, but JSONObject isn't supported by firebase.pushJSON. I wonder how other dudes include variables in the jsonData. From the example, the data is hardcoded though. String jsonData = "{\"parent_001\":\"parent 001 text\", \"parent 002\":{\"child_of_002\":123.456}}";

mobizt commented 5 years ago

You need to print (v5) or serialize (v6) ArduinoJson object (classs) as Arduino string object or C or C++ string. And pass that string to Firebase function.

sarkrui commented 5 years ago

Thanks @mobizt ! It works!

Using ArduinoJson V6

  //generated by arduinojson.org/v6/assistant/
  const size_t capacity = JSON_OBJECT_SIZE(3);
  DynamicJsonDocument objectData(capacity);

  objectData["dBValue"] = 45.0;
  objectData["time"] = "2019-05-15T15:00:25";
  objectData["buttonState"] = 0;

  String logData;
  serializeJson(objectData, logData);
  Firebase.pushJSON(firebaseData, data_log + device_id, logData);
sarkrui commented 5 years ago

I did manage to pass int float variables into a JSONObject, however, only ints and floats were synchronized, why is that?

  //generated by arduinojson.org/v6/assistant/
  const size_t capacity = JSON_OBJECT_SIZE(3);
  DynamicJsonDocument objectData(capacity);

  objectData["dBValue"] = dBValue;  //global variable, float 
  objectData["buttonState"] = buttonState; //global variable, int
  objectData["time"] = formattedDate; //global variable String

  String logData;
  serializeJson(objectData, logData);
  Firebase.pushJSON(firebaseData, data_log + device_id, logData);

Firebase Output:

mobizt commented 5 years ago

What do you mean synchronized?

mobizt commented 5 years ago

Print or serialize your created ArduinoJson object to string and check it on serial print, if something wrong with ArduinoJson please open issue at ArduinoJson Github.

sarkrui commented 5 years ago

Thanks @mobizt ! I switched to ArduinoJSONV5 and problem solved. not synchronized, I meant only int and float were passed, whereas String wasn't.

void baselinePushV5() {

  const size_t capacity = JSON_OBJECT_SIZE(3);
  DynamicJsonBuffer jsonBuffer(capacity);

  JsonObject& root = jsonBuffer.createObject();
  root["dBValue"] = 56;
  root["buttonState"] = 2;
  root["timeStamp"] = formattedDate;
  root.printTo(serialjson);
  Serial.println(serialjson);
  delay(1000);
}

Serial Outputs

{"dBValue":56,"buttonState":2,"timeStamp":"2019-05-21T12:10:53"}

Failed to pass String (variable) to JSONObject https://github.com/bblanchon/ArduinoJson/issues/1002

Proxcentaur commented 4 years ago

Thanks @mobizt ! I switched to ArduinoJSONV5 and problem solved. not synchronized, I meant only int and float were passed, whereas String wasn't.

void baselinePushV5() {

  const size_t capacity = JSON_OBJECT_SIZE(3);
  DynamicJsonBuffer jsonBuffer(capacity);

  JsonObject& root = jsonBuffer.createObject();
  root["dBValue"] = 56;
  root["buttonState"] = 2;
  root["timeStamp"] = formattedDate;
  root.printTo(serialjson);
  Serial.println(serialjson);
  delay(1000);
}

Serial Outputs

{"dBValue":56,"buttonState":2,"timeStamp":"2019-05-21T12:10:53"}

Failed to pass String (variable) to JSONObject bblanchon/ArduinoJson#1002

How did you pass the root data into Firebase.pushJSON? I did the same thing but still can't pass those data.

mobizt commented 4 years ago

You can't use ArduinoJson with this library to handle JSON.

The library has built-in JSON parser and builder called FirebaseJson and you should use it instead.

See the example for the usages.

sarkrui commented 4 years ago

@Proxcentaur Check this out, usage of FirebaseJSON.

mobizt commented 4 years ago

@Proxcentaur Did you read this?

Proxcentaur commented 4 years ago

Thank you so much, I have read this.

Best regards, Septia Dini R Instrumentation Technology Department of Electrical and Informatics Engineering Universitas Gadjah Mada

Pada tanggal Kam, 14 Mei 2020 pukul 00.37 mobizt notifications@github.com menulis:

@Proxcentaur https://github.com/Proxcentaur Did you read this https://github.com/mobizt/Firebase-ESP8266/issues/2#issuecomment-628094708 ?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mobizt/Firebase-ESP32/issues/23#issuecomment-628140347, or unsubscribe https://github.com/notifications/unsubscribe-auth/AILRL6D54DSG5KRQRV6JT2DRRLLEZANCNFSM4HOGA6VQ .