Read Firebase RTDB Data Only on Updates (ESP8266 or ESP32) #657
Closed
kkbughunter
started this conversation in
General
Replies: 2 comments 1 reply
-
https://github.com/mobizt/Firebase-ESP-Client/tree/main/examples/RTDB/DataChangesListener |
Beta Was this translation helpful? Give feedback.
1 reply
-
i got the Ans: to listen multiple path #include <Arduino.h>
#if defined(ESP32) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#elif __has_include(<WiFiNINA.h>)
#include <WiFiNINA.h>
#elif __has_include(<WiFi101.h>)
#include <WiFi101.h>
#elif __has_include(<WiFiS3.h>)
#include <WiFiS3.h>
#endif
#include <Firebase_ESP_Client.h>
#include <addons/TokenHelper.h>
#include <addons/RTDBHelper.h>
/* 1. Define the WiFi credentials */
#define WIFI_SSID "loop"
#define WIFI_PASSWORD "cyber@123"
#define API_KEY "AIzaSyBm_I3tnxfiSi8sd-jhiFNZpO5XgEj8NJ4"
#define DATABASE_URL "https://project1-ac5cd-default-rtdb.asia-southeast1.firebasedatabase.app/" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app
#define USER_EMAIL "[email protected]"
#define USER_PASSWORD "user1user1"
//Define Firebase Data object
FirebaseData fbdo;
FirebaseData stream;
FirebaseAuth auth;
FirebaseConfig config;
unsigned long sendDataPrevMillis = 0;
String parentPath = "/device";
String childPath[2] = {"/d1", "/d2"};
int count = 0;
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 ? "" : "");
}
}
Serial.println();
// Serial.printf("Received stream payload size: %d (Max. %d)\n\n", stream.payloadLength(), stream.maxPayloadLength());
}
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);
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();
Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
config.api_key = API_KEY;
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
config.database_url = DATABASE_URL;
config.token_status_callback = tokenStatusCallback;
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
//Recommend for ESP8266 stream, adjust the buffer size to match your stream data size
#if defined(ESP8266)
stream.setBSSLBufferSize(2048 /* Rx in bytes, 512 - 16384 */, 512 /* Tx in bytes, 512 - 16384 */);
#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.
if (!Firebase.RTDB.beginMultiPathStream(&stream, parentPath))
Serial.printf("sream begin error, %s\n\n", stream.errorReason().c_str());
Firebase.RTDB.setMultiPathStreamCallback(&stream, streamCallback, streamTimeoutCallback);
/** Timeout options, below is default config.
//WiFi reconnect timeout (interval) in ms (10 sec - 5 min) when WiFi disconnected.
config.timeout.wifiReconnect = 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()
{
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm working with the following code that reads data from a Firebase
Currently, the code reads data continuously from the path "/s1" in the database. I'd like to modify it to read data only when there's an update in the database at that path.
I've looked into the Firebase library documentation for stream functions, but I'm not sure how to integrate them effectively.
Could anyone provide guidance on how to achieve this behavior using the stream function or suggest alternative approaches, like event listeners?
Beta Was this translation helpful? Give feedback.
All reactions