mobizt / Firebase-ESP-Client

[DEPRECATED]🔥Firebase Arduino Client Library for ESP8266, ESP32 and RP2040 Pico. The complete, fast, secured and reliable Firebase Arduino client library that supports RTDB, Cloud Firestore, Firebase and Google Cloud Storage, Cloud Messaging and Cloud Functions for Firebase.
MIT License
471 stars 100 forks source link

serverTimestamp in Firestore #632

Closed lougira closed 7 months ago

lougira commented 7 months ago

Hello,

I try to add a timestamp field in Firestore. I would like to get the server timestamp. I notice in the repo main page the following :

The server's Timestamp can be stored in the database through Firebase.RTDB.setTimestamp.

I wrote the following code:

void write_on_firebase(float temp, float hum, int dis)
{
    dataMillis = millis();

    Serial.printf("Set timestamp... %s\n", Firebase.setTimestamp(fbdo, "/test/timestamp") ? "ok" : fbdo.errorReason().c_str());

    if (fbdo.httpCode() == FIREBASE_ERROR_HTTP_CODE_OK)
    {
      // In setTimestampAsync, the following timestamp will be 0 because the response payload was ignored for all async functions.

      // Timestamp saved in millisecond, get its seconds from int value
      Serial.print("TIMESTAMP (Seconds): ");
      Serial.println(fbdo.to<int>());

      // Or print the total milliseconds from double value
      // Due to bugs in Serial.print in Arduino library, use printf to print double instead.
      printf("TIMESTAMP (milliSeconds): %lld\n", fbdo.to<uint64_t>());
    }

    Serial.printf("Get timestamp... %s\n", Firebase.getDouble(fbdo, "/test/timestamp") ? "ok" : fbdo.errorReason().c_str());
    if (fbdo.httpCode() == FIREBASE_ERROR_HTTP_CODE_OK)
      printf("TIMESTAMP: %lld\n", fbdo.to<uint64_t>());

    String documentPath = "Cuve/mesure";
    documentPath += String(count);

    // Add values in the document
    content.set("fields/hauteur/doubleValue", dis/10.0);
    content.set("fields/temperature/doubleValue", String(temp));
    content.set("fields/humidite/doubleValue", String(hum));
    content.set("Ts/.sv", "timestamp"); //  timestamp

    String doc_path = "projects/";
    doc_path += FIREBASE_PROJECT_ID;
    doc_path += "/databases/(default)/documents/coll_id/doc_id"; // coll_id and doc_id are your collection id and document id

    count++; // incrémentation du nb de mesure pour la création du nouveau doc dans firestore

    // Display in the console if Firestore writing is succesfull
    if (Firebase.Firestore.createDocument(&fbdo, FIREBASE_PROJECT_ID, "" /* databaseId can be (default) or empty */, documentPath.c_str(), content.raw()))
       Serial.printf("ok\n%s\n\n", fbdo.payload().c_str());
    else
        Serial.println(fbdo.errorReason());
}

When I build this code, the error is :

Compiling .pio\build\dfrobot_firebeetle2_esp32e\src\main.cpp.o
src/fonctions.cpp: In function 'void write_on_firebase(float, float, int)':
src/fonctions.cpp:188:53: error: 'class Firebase_ESP_Client' has no member named 'setTimestamp'
     Serial.printf("Set timestamp... %s\n", Firebase.setTimestamp(fbdo, "/test/timestamp") ? "ok" : fbdo.errorReason().c_str());
                                                     ^~~~~~~~~~~~
src/fonctions.cpp:203:53: error: 'class Firebase_ESP_Client' has no member named 'getDouble'
     Serial.printf("Get timestamp... %s\n", Firebase.getDouble(fbdo, "/test/timestamp") ? "ok" : fbdo.errorReason().c_str());
                                                     ^~~~~~~~~
*** [.pio\build\dfrobot_firebeetle2_esp32e\src\fonctions.cpp.o] Error 1

I can't figure out where is the problem and I don't understand why the setTimestamp() method is not a member function of the class Firebase. By the way, can the setTimestamp() be used for Firestore?

mobizt commented 7 months ago

For RTDB, you have to follow this example. https://github.com/mobizt/Firebase-ESP-Client/blob/main/examples/RTDB/Timestamp/Timestamp.ino

Firestore and Realtime database are totally different, and nothing is compatible.

cinqlair commented 7 months ago

So it means that with this library, we can't use serverTimestamp with Firestore? Can you confirm this?

mobizt commented 7 months ago

You should consult the Firestore documentation.