mobizt / FirebaseClient

🔥Async Firebase Client for Arduino. Supports Realtime Database, Cloud Firestore Database, Firebase Storage, Cloud Messaging, Google Cloud Functions and Google Cloud Storage.
MIT License
121 stars 7 forks source link

messaging and database ins same sketch #118

Closed epna closed 3 months ago

epna commented 3 months ago

hi,

if i build a sketch of messaging, it works fine if i buidl a sketch of databse (get data); il works fine if i merge the two sketch get data doesn't work

please help Thanks

include

include

include "password.h"

include

void authHandlerBDD(); void authHandlerMess(); void timeStatusCB(uint32_t &ts); void printResult(AsyncResult &aResult); void printError(int code, const String &msg); DefaultNetwork network; // initilize with boolean parameter to enable/disable network reconnection // ServiceAuth is required for Cloud Messaging. ServiceAuth sa_auth(timeStatusCB, FIREBASE_CLIENT_EMAIL, FIREBASE_PROJECT_ID, PRIVATE_KEY, 3000 / expire period in seconds (<= 3600) /); UserAuth user_auth(API_KEY, USER_EMAIL, USER_PASSWORD); FirebaseApp appMess, appBDD;

include

WiFiClientSecure ssl_client;

using AsyncClient = AsyncClientClass; AsyncClient aClient1(ssl_client, getNetwork(network)),aClient2(ssl_client, getNetwork(network)); Messaging messaging; RealtimeDatabase Database; AsyncResult aResult_no_callback_Mess,aResult_no_callback_BDD ; bool taskCompleted = false;

void setup() { Serial.begin(115200); WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

Serial.print("Connecting to Wi-Fi");
unsigned long ms = millis();
while (WiFi.status() != WL_CONNECTED)
{
    Serial.print(".");
    delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();

Firebase.printf("Firebase Client v%s\n", FIREBASE_CLIENT_VERSION);
Serial.println("Initializing appMess..");
ssl_client.setInsecure();
ssl_client.setBufferSizes(4096, 1024);
initializeApp(aClient1, appMess, getAuth(sa_auth), aResult_no_callback_Mess);
initializeApp(aClient2, appBDD, getAuth(user_auth), aResult_no_callback_BDD);
authHandlerMess();
authHandlerBDD();
appMess.getApp<Messaging>(messaging);
appBDD.getApp<RealtimeDatabase>(Database);
Database.url(DATABASE_URL);

}

void loop() { authHandlerMess(); authHandlerBDD(); appMess.loop(); appBDD.loop(); messaging.loop(); Database.loop();

if (appMess.ready() && !taskCompleted)
{
    taskCompleted = true;
    Serial.println("Sending message...");
    Messages::Message msg;
    msg.topic("sgdm_piscinea");
    Messages::Notification notification;
    notification.body("Notification body").title("Notification title");
    object_t data, obj1, obj2, obj3, obj4;
    JsonWriter writer;

    writer.create(obj1, "name", string_t("wrench"));
    writer.create(obj2, "mass", string_t("1.3kg"));
    writer.create(obj3, "count", string_t("3"));
    writer.join(data, 3 /* no. of object_t (s) to join */, obj1, obj2, obj3);

    // object_t data2("{\"name\":\"wrench\",\"mass\":\"1.3kg\",\"count\":\"3\"}");
    msg.data(data);
    Messages::AndroidConfig androidConfig;
    androidConfig.priority(Messages::AndroidMessagePriority::_HIGH);
    Messages::AndroidNotification androidNotification;
    androidNotification.notification_priority(Messages::NotificationPriority::PRIORITY_HIGH);
    androidConfig.notification(androidNotification);
    msg.android(androidConfig);
    String payload = messaging.send(aClient1, Messages::Parent(FIREBASE_PROJECT_ID), msg);

    if (aClient1.lastError().code() == 0)
        Serial.println(payload);
    else
        printError(aClient1.lastError().code(), aClient1.lastError().message());
}
getData2();
delay(1000);

}

void authHandlerMess() { // Blocking authentication handler with timeout unsigned long ms = millis(); while (appMess.isInitialized() && !appMess.ready() && millis() - ms < 120 * 1000) { // This JWT token process required for ServiceAuth and CustomAuth authentications JWT.loop(appMess.getAuth()); printResult(aResult_no_callback_Mess); } }

void authHandlerBDD() { // Blocking authentication handler with timeout unsigned long ms = millis(); while (appBDD.isInitialized() && !appBDD.ready() && millis() - ms < 120 * 1000) { // This JWT token process required for ServiceAuth and CustomAuth authentications JWT.loop(appBDD.getAuth()); printResult(aResult_no_callback_BDD); } }

void timeStatusCB(uint32_t &ts) {

if defined(ESP8266) || defined(ESP32) || defined(CORE_ARDUINO_PICO)

if (time(nullptr) < FIREBASE_DEFAULT_TS)
{

    configTime(3 * 3600, 0, "pool.ntp.org");
    while (time(nullptr) < FIREBASE_DEFAULT_TS)
    {
        delay(100);
    }
}
ts = time(nullptr);

elif __has_include() || __has_include()

ts = WiFi.getTime();

endif

}

void printResult(AsyncResult &aResult) { if (aResult.isEvent()) { Firebase.printf("Event task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.appEvent().message().c_str(), aResult.appEvent().code()); }

if (aResult.isDebug())
{
    Firebase.printf("Debug task: %s, msg: %s\n", aResult.uid().c_str(), aResult.debug().c_str());
}

if (aResult.isError())
{
    Firebase.printf("Error task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.error().message().c_str(), aResult.error().code());
}

}

void printError(int code, const String &msg) { Firebase.printf("Error, msg: %s, code: %d\n", msg.c_str(), code); } void getData2() { Serial.println("Synchronous Get2... "); Database.get(aClient2, "/test/int", aResult_no_callback_BDD); delay(500); } void getData() { Serial.println("Synchronous Get... "); Serial.print("Get int... "); int v1 = Database.get(aClient2, "/test/int"); if (aClient2.lastError().code() == 0) Serial.println(v1); else printError(aClient2.lastError().code(), aClient2.lastError().message()); }

mobizt commented 3 months ago

You have to read the library documentation thoroughly to understand the library concept and usage.

mobizt commented 3 months ago

You must remove all delay functions from your code because this is async client library.

Read the documentation is a must.

mobizt commented 3 months ago

You can use only one FirebaseApp and ServiceAuth for both Messaging and Realtime Database to reduce the code complexity.

The FirebaseApp is the authentication task handler which can be used globally for all Firebase services apps if those apps support the authentication that provides by FirebaseApp.

mobizt commented 3 months ago

You can use only one FirebaseApp and ServiceAuth for both Messaging and Realtime Database to reduce the code complexity.

The FirebaseApp is the authentication task handler which can be used globally for all Firebase services apps if those apps support the authentication that provide by FirebaseApp.

mobizt commented 3 months ago

Only NTP configTime waiting delay can be used. Other delays in your code must be removed.