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 How to send JSON string to firebase #29

Closed mixaobo closed 5 years ago

mixaobo commented 5 years ago

here is my code, i can set each value but need totaly 4 times, i know there is a setJSON but i cant get it work. here is my code

JsonObject ID = object.createNestedObject(String(header.from_node));
        ID["Temperature"] = pack0.temperature;
        ID["Humidity"] = pack0.humidity;
        ID["Soil"] = pack0.soil;
        ID[ "Battery"] = pack0.battery;
        serializeJson(object, Serial);
        Firebase.setJSON(firebaseData, path1, object);
        //aFirebase.setJSON(firebaseData,path1, object);
        /*Firebase.setFloat(firebaseData, path1_h, ID["Humidity"]);
        Firebase.setFloat(firebaseData, path1_t, ID["Temperature"]);
        Firebase.setInt(firebaseData, path1_s, ID["Soil"]);
        Firebase.setFloat(firebaseData, path1_b, ID["Battery"]);*/

which i get from serial monitor: {"1":{"Temperature":29.8,"Humidity":41,"Soil":2,"Battery":3292.593}} error is no match function code setJSON My code and error

mobizt commented 5 years ago

Function setJSON requires json serialized string.

Please remember that JSON is only key-value paired notation which can be handle or manage differently depends on system and parser.

The ArduinoJson handle and manage it by its own class (object), form create, parse, to serialize, and it has its own functions that return to string directly as stansard c++ string or C chars array for you to use.

ArduinoJson v5 provides printTo function to return string from its JSON object.

String myString;
object.printTo(myString);

//Now use myString in setJSON function

Firebase.setJSON(firebaseData, path1, myString);

https://arduinojson.org/v5/api/jsonobject/printto/

mixaobo commented 5 years ago

Function setJSON require json serialized string.

Please remember that JSON is only key-value paired notation which can be handle or manage differently depends on system and parser.

The ArduinoJson handle and manage it by its own class (object), form create, parse, to serialize, and it has its own functions that return to string directly as stansard c++ string or C chars array for you to use.

ArduinoJson v5 provide printTo function to return string from its JSON object.

String myString;
object.printTo(myString);

//Now use myString in setJSON function

Firebase.setJSON(firebaseData, path1, myString);

https://arduinojson.org/v5/api/jsonobject/printto/

Function setJSON requires json serialized string.

Please remember that JSON is only key-value paired notation which can be handle or manage differently depends on system and parser.

The ArduinoJson handle and manage it by its own class (object), form create, parse, to serialize, and it has its own functions that return to string directly as stansard c++ string or C chars array for you to use.

ArduinoJson v5 provides printTo function to return string from its JSON object.

String myString;
object.printTo(myString);

//Now use myString in setJSON function

Firebase.setJSON(firebaseData, path1, myString);

https://arduinojson.org/v5/api/jsonobject/printto/

sorry for noob question, since i use arduinojson v6. are there anyway to use printTo. they just use serializeJson

mobizt commented 5 years ago

Read this for how to print as string. https://arduinojson.org/v6/example/string/

For print to string variable, do the same way as you print to Serial.

serializeJson(object, myString);