Skip to content

Commit

Permalink
Add Cloud Firestore document commit function and examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed May 11, 2021
1 parent 23f8d06 commit 7274102
Show file tree
Hide file tree
Showing 14 changed files with 797 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Firebase Arduino Client Library for ESP8266 and ESP32


Google's Firebase Arduino Client Library for ESP8266 and ESP32 v2.1.5
Google's Firebase Arduino Client Library for ESP8266 and ESP32 v2.1.6


This library supports ESP8266 and ESP32 MCU from Espressif. The following are platforms in which the libraries are also available (RTDB only).
Expand Down Expand Up @@ -250,7 +250,7 @@ The session was close when the host or ip changes or server closed or the sessio
When the new session need to be opened, the SSL handshake will be processed again and used the time approx 1 - 2 seconds to be done.


For post (push) or put (set) the request in RTDB, to speed up the data transfer, use pushAsync or setAsync instead.
For post (push) or put (set) request in RTDB, to speed up the data transfer, use pushAsync or setAsync instead.


With pushAsync and setAsync, the payload response will be ignored and the next data will be processed immediately.
Expand Down
159 changes: 159 additions & 0 deletions examples/Firestore/Commit_Document/Append_Array/Append_Array.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@

/**
* Created by K. Suwatchai (Mobizt)
*
* Email: [email protected]
*
* Github: https://github.com/mobizt
*
* Copyright (c) 2021 mobizt
*
*/

//This example shows how to app the value to array field. This operation required Email/password, custom or OAUth2.0 authentication.

#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <Firebase_ESP_Client.h>

//Provide the token generation process info.
#include "addons/TokenHelper.h"

/* 1. Define the WiFi credentials */
#define WIFI_SSID "WIFI_AP"
#define WIFI_PASSWORD "WIFI_PASSWORD"

/* 2. Define the API Key */
#define API_KEY "API_KEY"

/* 3. Define the project ID */
#define FIREBASE_PROJECT_ID "PROJECT_ID"

/* 4. Define the user Email and password that alreadey registerd or added in your project */
#define USER_EMAIL "USER_EMAIL"
#define USER_PASSWORD "USER_PASSWORD"

//Define Firebase Data object
FirebaseData fbdo;

FirebaseAuth auth;
FirebaseConfig config;

unsigned long dataMillis = 0;
int count = 0;

void setup()
{

Serial.begin(115200);

WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();

/* Assign the api key (required) */
config.api_key = API_KEY;

/* Assign the user sign in credentials */
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;

/* Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h

Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);

#if defined(ESP8266)
//Set the size of WiFi rx/tx buffers in the case where we want to work with large data.
fbdo.setBSSLBufferSize(1024, 1024);
#endif
}

void loop()
{

if (Firebase.ready() && (millis() - dataMillis > 60000 || dataMillis == 0))
{
dataMillis = millis();
count++;

Serial.println("------------------------------------");
Serial.println("Commit a document (append array)...");

//The dyamic array of write object fb_esp_firestore_document_write_t.
std::vector<struct fb_esp_firestore_document_write_t> writes;

//A write object that will be written to the document.
struct fb_esp_firestore_document_write_t transform_write;

//Set the write object write operation type.
//fb_esp_firestore_document_write_type_update,
//fb_esp_firestore_document_write_type_delete,
//fb_esp_firestore_document_write_type_transform
transform_write.type = fb_esp_firestore_document_write_type_transform;

//Set the document path of document to write (transform)
transform_write.document_transform.transform_document_path = "test_collection/test_document";

//Set a transformation of a field of the document.
struct fb_esp_firestore_document_write_field_transforms_t field_transforms;

//Set field path to write.
field_transforms.fieldPath = "appended_data";

//Set the transformation type.
//fb_esp_firestore_transform_type_set_to_server_value,
//fb_esp_firestore_transform_type_increment,
//fb_esp_firestore_transform_type_maaximum,
//fb_esp_firestore_transform_type_minimum,
//fb_esp_firestore_transform_type_append_missing_elements,
//fb_esp_firestore_transform_type_remove_all_from_array
field_transforms.transform_type = fb_esp_firestore_transform_type_append_missing_elements;

String content;
FirebaseJson js;

String txt = "Hello World! " + String(count);
js.set("values/[0]/integerValue", String(rand()).c_str());
js.set("values/[1]/stringValue", txt);

js.toString(content);

//Set the transformation content.
field_transforms.transform_content = content.c_str();

//Add a field transformation object to a write object.
transform_write.document_transform.field_transforms.push_back(field_transforms);

//Add a write object to a write array.
writes.push_back(transform_write);

if (Firebase.Firestore.commitDocument(&fbdo, FIREBASE_PROJECT_ID, "" /* databaseId can be (default) or empty */, writes /* dynamic array of fb_esp_firestore_document_write_t */, "" /* transaction */))
{
Serial.println("PASSED");
Serial.println("------------------------------------");
Serial.println(fbdo.payload());
Serial.println("------------------------------------");
Serial.println();
}
else
{
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
Serial.println("------------------------------------");
Serial.println();
}
}
}
Loading

0 comments on commit 7274102

Please sign in to comment.