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

Unknown Parentpath data #651

Closed lchvsanilkumar closed 6 months ago

lchvsanilkumar commented 6 months ago

`/**

include

if defined(ESP32) || defined(ARDUINO_RASPBERRY_PI_PICO_W)

include

elif defined(ESP8266)

include

elif __has_include()

include

elif __has_include()

include

elif __has_include()

include

endif

include

// Provide the token generation process info.

include <addons/TokenHelper.h>

// Provide the RTDB payload printing info and other helper functions.

include <addons/RTDBHelper.h>

/ 1. Define the WiFi credentials /

define WIFI_SSID ""

define WIFI_PASSWORD ""

// For the following credentials, see examples/Authentications/SignInAsUser/EmailPassword/EmailPassword.ino

/ 2. Define the API Key /

define API_KEY ""

/ 3. Define the RTDB URL /

define DATABASE_URL "testing-912c2.firebaseio.com" //.firebaseio.com or ..firebasedatabase.app

/ 4. Define the user Email and password that alreadey registerd or added in your project /

define USER_EMAIL "@gmail.com"

define USER_PASSWORD "123456789"

// Define Firebase Data object FirebaseData fbdo; FirebaseData fbdo1; FirebaseData fbdo2; FirebaseData fbdo3;

FirebaseData stream;

FirebaseAuth auth; FirebaseConfig config;

unsigned long sendDataPrevMillis = 0;

String parentPaths[] = { "/AK401", "/AK402", "/AK403" }; String childPath[] = { "/S1", "/S2", "/S3", "/S4", "/S4D", "/S5D", "/Triggers/11", "/Triggers/12", "/Triggers/21", "/Triggers/22", "/Triggers/31", "/Triggers/32", "/Triggers/41", "/Triggers/42", "/Triggers/51", "/Triggers/52", "/Triggers/61", "/Triggers/62", "/Triggers/hour11", "/Triggers/hour12", "/Triggers/hour21", "/Triggers/hour22", "/Triggers/hour31", "/Triggers/hour32", "/Triggers/hour41", "/Triggers/hour42", "/Triggers/hour51", "/Triggers/hour52", "/Triggers/hour61", "/Triggers/hour62", "/Triggers/min11", "/Triggers/min12", "/Triggers/min21", "/Triggers/min22", "/Triggers/min31", "/Triggers/min32", "/Triggers/min41", "/Triggers/min42", "/Triggers/min51", "/Triggers/min52", "/Triggers/min61", "/Triggers/min62", "/Triggers/countback", "/Triggers/refreshvalue", "/Triggers/restart" };

int count = 0;

volatile bool dataChanged = false;

if defined(ARDUINO_RASPBERRY_PI_PICO_W)

WiFiMulti multi;

endif

void streamCallback(MultiPathStream stream) { size_t numChild = sizeof(childPath) / sizeof(childPath[0]);

for (size_t i = 0; i < numChild; i++) { if (stream.get(childPath[i])) { Serial.printf("path: %s, event: %s, type: %s, value: %s%s", stream.dataPath.c_str(), stream.eventType.c_str(), stream.type.c_str(), stream.value.c_str(), i < numChild - 1 ? "\n" : "");

}

}

Serial.println();

// This is the size of stream payload received (current and max value) // Max payload size is the payload size under the stream path since the stream connected // and read once and will not update until stream reconnection takes place. // This max value will be zero as no payload received in case of ESP8266 which // BearSSL reserved Rx buffer size is less than the actual stream payload. Serial.printf("Received stream payload size: %d (Max. %d)\n\n", stream.payloadLength(), stream.maxPayloadLength());

// Due to limited of stack memory, do not perform any task that used large memory here especially starting connect to server. // Just set this flag and check it status later. dataChanged = true; }

void streamTimeoutCallback(bool timeout) { if (timeout) Serial.println("stream timed out, resuming...\n");

if (!stream.httpConnected()) Serial.printf("error code: %d, reason: %s\n\n", stream.httpCode(), stream.errorReason().c_str()); }

void setup() {

Serial.begin(115200);

if defined(ARDUINO_RASPBERRY_PI_PICO_W)

multi.addAP(WIFI_SSID, WIFI_PASSWORD); multi.run();

else

WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

endif

Serial.print("Connecting to Wi-Fi"); unsigned long ms = millis(); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(300);

if defined(ARDUINO_RASPBERRY_PI_PICO_W)

if (millis() - ms > 10000)
  break;

endif

} Serial.println(); Serial.print("Connected with IP: "); Serial.println(WiFi.localIP()); Serial.println();

Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);

Serial.println("The MultipathStream is obsoleted, please use normal stream instead."); Serial.println("You can use may FirebaseData objects for multiple streaming in ESP32 and Raspberry Pi Pico."); Serial.println("In case of ESP8266, external Static RAM is recommend for multiple streaming."); Serial.println("For device with less memory, multiple streaming is not recommended.");

/ 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 RTDB URL (required) / config.database_url = DATABASE_URL;

// The WiFi credentials are required for Pico W // due to it does not have reconnect feature.

if defined(ARDUINO_RASPBERRY_PI_PICO_W)

config.wifi.clearAP(); config.wifi.addAP(WIFI_SSID, WIFI_PASSWORD);

endif

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

// Comment or pass false value when WiFi reconnection will control by your code or third party library e.g. WiFiManager Firebase.reconnectNetwork(true);

// Since v4.4.x, BearSSL engine was used, the SSL buffer need to be set. // Large data transmission may require larger RX buffer, otherwise connection issue or data read time out can be occurred. fbdo.setBSSLBufferSize(2048 / Rx buffer size in bytes from 512 - 16384 /, 1024 / Tx buffer size in bytes from 512 - 16384 /); stream.setBSSLBufferSize(2048 / Rx buffer size in bytes from 512 - 16384 /, 1024 / Tx buffer size in bytes from 512 - 16384 /);

// Or use legacy authenticate method // config.database_url = DATABASE_URL; // config.signer.tokens.legacy_token = "";

// To connect without auth in Test Mode, see Authentications/TestMode/TestMode.ino

Firebase.begin(&config, &auth);

// You can use TCP KeepAlive For more reliable stream operation and tracking the server connection status, please read this for detail. // https://github.com/mobizt/Firebase-ESP-Client#enable-tcp-keepalive-for-reliable-http-streaming // You can use keepAlive in ESP8266 core version newer than v3.1.2. // Or you can use git version (v3.1.2) https://github.com/esp8266/Arduino

if defined(ESP32)

stream.keepAlive(5, 5, 1);

endif

// The data under the node being stream (parent path) should keep small // Large stream payload leads to the parsing error due to memory allocation.

// The MultiPathStream works as normal stream with the payload parsing function. for (size_t i = 0; i < sizeof(parentPaths) / sizeof(parentPaths[0]); i++) { FirebaseData *currentStream;

// Assign the correct FirebaseData object based on the current index
if (i == 0) {
  currentStream = &fbdo1;
} else if (i == 1) {
  currentStream = &fbdo2;
} else {
  currentStream = &fbdo3;
}

if (!Firebase.RTDB.beginMultiPathStream(currentStream, parentPaths[i])) {
  Serial.printf("stream begin error for %s, %s\n\n", parentPaths[i].c_str(), currentStream->errorReason().c_str());
}
Firebase.RTDB.setMultiPathStreamCallback(currentStream, streamCallback, streamTimeoutCallback);

}

// Set the callback functions for each multipath stream // Firebase.RTDB.setMultiPathStreamCallback(&fbdo2, streamCallback, streamTimeoutCallback); // Firebase.RTDB.setMultiPathStreamCallback(&fbdo3, streamCallback, streamTimeoutCallback);

/** Timeout options, below is default config.

//Network reconnect timeout (interval) in ms (10 sec - 5 min) when network or WiFi disconnected. config.timeout.networkReconnect = 10 * 1000;

//Socket begin connection timeout (ESP32) or data transfer timeout (ESP8266) in ms (1 sec - 1 min). config.timeout.socketConnection = 30 * 1000;

//ESP32 SSL handshake in ms (1 sec - 2 min). This option doesn't allow in ESP8266 core library. config.timeout.sslHandshake = 2 60 1000;

//Server response read timeout in ms (1 sec - 1 min). config.timeout.serverResponse = 10 * 1000;

//RTDB Stream keep-alive timeout in ms (20 sec - 2 min) when no server's keep-alive event data received. config.timeout.rtdbKeepAlive = 45 * 1000;

//RTDB Stream reconnect timeout (interval) in ms (1 sec - 1 min) when RTDB Stream closed and want to resume. config.timeout.rtdbStreamReconnect = 1 * 1000;

//RTDB Stream error notification timeout (interval) in ms (3 sec - 30 sec). It determines how often the readStream //will return false (error) when it called repeatedly in loop. config.timeout.rtdbStreamError = 3 * 1000;

*/ }

void loop() {

// Firebase.ready() should be called repeatedly to handle authentication tasks.

if !defined(ESP8266) && !defined(ESP32)

Firebase.RTDB.runStream();

endif

if (Firebase.ready() && (millis() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0)) { sendDataPrevMillis = millis();

Serial.print("\nSet json...");

Serial.println("ok\n");

}

if (dataChanged) { dataChanged = false; // When stream data is available, do anything here... }

// After calling stream.keepAlive, now we can track the server connecting status if (!stream.httpConnected()) { // Server was disconnected! } }

// To pause stream // stream.pauseFirebase(true); // stream.clear(); // close session and release memory

// To resume stream with callback // stream.pauseFirebase(false); // Firebase.RTDB.setMultiPathStreamCallback(&stream, streamCallback, streamTimeoutCallback);`

i am gettting the data but the data is coming from which parentpath is unknown. Serial.printf("path: %s, event: %s, type: %s, value: %s%s", stream.dataPath.c_str(), stream.eventType.c_str(), stream.type.c_str(), stream.value.c_str(), i < numChild - 1 ? "\n" : ""); this gives the child datapath, but i want along with parent path...is it possible....please help me

mobizt commented 6 months ago

The absolute path is parent + child.

lchvsanilkumar commented 6 months ago

stream.dataPath.c_str() here it is child node path but i want along the parentpath because here i am using multiple parent nodes thats why i don't know which parentpath data is getting

mobizt commented 6 months ago

You should separate the callback for each stream if you don't know how to get the parent path. It is your code design, and you should solve it yourself.

lchvsanilkumar commented 6 months ago

thank you for your huge help