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
120 stars 6 forks source link

"TCP connection failed" Code "-1" #166

Open ehabtawfikelbwab opened 3 hours ago

ehabtawfikelbwab commented 3 hours ago

Hello Again,

I'm using firebase with mqtt mqtt is failed to initialize and firebase failed to initialize

I was trying to use realtime database stream from : Stream

i got : Event task: auth request sent Event task: error Error task: TCP connection failed | -1

  if (result.isEvent()) {
    Serial.println("Event task: " + result.appEvent().message());
  }

  if (result.isDebug()) {
    Serial.println("Debug task: " + result.debug() + "|" + result.payload());
  }

  if (result.isError()) {
    Serial.println("Error task: " + result.error().message() + " | " + String(result.error().code()));
  }
//**************************************** Firebase v ****************************************
void InitilizeFirebase() {
  if (CanUseFirebase()) {
    ssl_client1.setInsecure();
    ssl_client2.setInsecure();

    // Initialize the authentication handler.
    initializeApp(client2, app, getAuth(user_auth), asyncCB, "authTask");

    // Binding the authentication handler with your Database class object.
    app.getApp<RealtimeDatabase>(Database);

    Database.setSSEFilters("get,put,patch,keep-alive,cancel,auth_revoked");

    // Set your database URL
    Database.url(DATABASE_URL);

    Database.get(client, "users/" + FirebaseClient_Sub + "/devices", asyncCB, true /* SSE mode (HTTP Streaming) */, "streamTask");
    firebaseInitilized = true;

    Serial.println("Firebase Initilized!");
  } else {
    Serial.println("Error: Firebase not Initilized");
  }
}

void asyncCB(AsyncResult& result) {
  // Implementation of asyncCB here

  HandleFirebaseRequests(result);
}

void HandleFirebaseRequests(AsyncResult& result) {
  if (result.isEvent()) {
    Serial.println("Event task: " + result.appEvent().message());
  }

  if (result.isDebug()) {
    Serial.println("Debug task: " + result.debug() + "|" + result.payload());
  }

  if (result.isError()) {
    Serial.println("Error task: " + result.error().message() + "|" + String(result.error().code()));
  }

  if (result.available()) {
    RealtimeDatabaseResult& RTDB = result.to<RealtimeDatabaseResult>();
    if (RTDB.isStream()) {
      String v5 = RTDB.to<String>();
      Serial.println(v5);
    }
  }
}

void HandleFirebaseLoop() {
  if (firebaseInitilized && !FirebaseClient_Sub.isEmpty()) {
    app.loop();
    Database.loop();
  }
}

bool CanUseFirebase() {
  String FileName = String("/my_Sub");

  if (!LittleFS.exists(FileName)) {
    return false;  // Error code indicating the file does not exist
  }

  File file = LittleFS.open(FileName, "r");

  if (file) {
    String file_Content;

    while (file.available()) {
      file_Content += char(file.read());
    }

    if (file_Content.isEmpty() || file_Content.length() < 2) {
      return false;
    } else {
      FirebaseClient_Sub = file_Content;
      return true;
    }
  } else {
    return false;
  }
}

void SetFirebaseClient_Sub(String sub) {
  String FileName = String("/my_Sub");
  File file_ = LittleFS.open(FileName, "w");
  if (!file_) {
    Serial.println("Failed to open file for writing (Set sub)");
    return;
  }
  file_.println(sub);
  file_.close();

  InitilizeFirebase();
}
ehabtawfikelbwab commented 3 hours ago

Also if mqtt is initialized and working but i did start to initialize firebase after it is make mqtt stop

mobizt commented 1 hour ago

Your device is running out of memory which you have to manage when you use open many SSL sever connections to the server concurrency.

https://github.com/mobizt/FirebaseClient?tab=readme-ov-file#running-many-tasks-concurrency-using-different-async-clients-in-different-ssl-clients

mobizt commented 1 hour ago

Please use discussions section instead for the problems in such case of usage problems.