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 101 forks source link

response payload read timed out #634

Closed yousefmlotfy656 closed 8 months ago

yousefmlotfy656 commented 8 months ago

hello dear , i found a problem with my code the after sometime i face a problems this is the serial monitor data chstate is 1 [415482][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" [415484][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" response payload read timed out [415500][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" response payload read timed out [415512][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" response payload read timed out [423483][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" [423485][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" response payload read timed out [423501][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" response payload read timed out [423513][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" response payload read timed out

i know that my internet connection not good enough but i want to make a stable code to reconnect again if any interuption happen in internet connection , so could you please help me in these part

this is my code , `/**

// بتاريخ 1/6/2024 // moshklet el connection beyfsl ba3d 2 minutes stopped with line firebase ready // but another problem exists is the retrieve the data from the firebase with brainstorm of doing a stream for the data from the firebase

include

if defined(ESP32)

include

include // https://github.com/tzapu/WiFiManager

elif defined(ESP8266)

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"

// select which pin will trigger the configuration portal when set to LOW

define TRIGGER_PIN 0

int timeout = 120; // seconds to run for

define API_KEY "AIzaSyCiIe1cxteVJtZFQl-7BKXX_6PJWcxed5Y"

// Insert RTDB URLefine the RTDB URL */

define DATABASE_URL "https://grad2023-bbcb3-default-rtdb.firebaseio.com/"

define Led_1 25

define Led_2 26

define Led_3 27

define Led_4 14

define Led_5 12

define Led_6 4

define Led_22 33

define Input_1 34

define Input_2 35

define button1Pin 32

// Define Firebase Data object. FirebaseData fbdo;

// Define firebase authentication. FirebaseAuth auth;

// Definee firebase configuration. FirebaseConfig config;

//======================================== Millis variable to send/store data to firebase database. unsigned long previousMillis1 = 0; // Variable to store the previous time const long interval1 = 1000;
unsigned long sendDataPrevMillis = 0; //const long sendDataIntervalMillis = 3000; //--> Sends/stores data to firebase database every 10 seconds. bool signupOK = false; unsigned long lastButtonPressTime = 0; const unsigned long debounceDelay = 200; int chstate=0; int chstate1=0; int dlchstate=0; int dlchstate1=0; int lichstate=0; int lichstate1=0; int stopper=0; String old="0"; String sstate="0"; String Engine_State="0"; String doorlock_State="0"; String light_State="0"; String cstate="0"; String dlcstate="0"; String licstate="0"; void setup() { WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
// put your setup code here, to run once:

Serial.begin(115200); WiFiManager wm;

bool res; while (WiFi.status() != WL_CONNECTED){ Serial.print("."); delay(5000); res = wm.autoConnect("AutoConnectAP","password"); // password protected ap // WiFi.reconnect(); } // res = wm.autoConnect(); // auto generated AP name from chipid // res = wm.autoConnect("AutoConnectAP"); // anonymous ap

if(!res) {
    Serial.println("Failed to connect");
    // ESP.restart();
} 
else {
    //if you get here you have connected to the WiFi    
    Serial.println("connected...yeey :)");
}

pinMode(TRIGGER_PIN, INPUT_PULLUP); pinMode(Led_1, OUTPUT); pinMode(Led_2, OUTPUT); pinMode(Led_3, OUTPUT); pinMode(Led_4, OUTPUT); pinMode(Led_5, OUTPUT); pinMode(Led_6, OUTPUT); pinMode(Led_22, OUTPUT); pinMode(Input_1, INPUT); pinMode(Input_2, INPUT); pinMode(button1Pin, INPUT); digitalWrite(Led_1,LOW);
digitalWrite(Led_2,LOW); digitalWrite(Led_3,LOW); digitalWrite(Led_4,LOW); digitalWrite(Led_5,HIGH); digitalWrite(Led_6,HIGH); // Assign the api key (required).

config.api_key = API_KEY;

// Assign the RTDB URL (required). config.database_url = DATABASE_URL;

// Sign up. Serial.println(); Serial.println("---------------Sign up"); Serial.print("Sign up new user... "); if (Firebase.signUp(&config, &auth, "", "")){ Serial.println("ok"); signupOK = true; } else{ Serial.printf("%s\n", config.signer.signupError.message.c_str()); } Serial.println("---------------");

// Assign the callback function for the long running token generation task. config.token_status_callback = tokenStatusCallback; //--> see addons/TokenHelper.h fbdo.setBSSLBufferSize(4096 / Rx buffer size in bytes from 512 - 16384 /, 1024 / Tx buffer size in bytes from 512 - 16384 /); config.timeout.serverResponse = 10 * 1000;

//Firebase.RTDB.enableClassicRequest(&fbdo, true);

fbdo.keepAlive(5, 5, 1); Firebase.begin(&config, &auth);

Serial.println("test6.8");

}

void loop() { /if (Firebase.signUp(&config, &auth, "", "")){ Serial.println("ok"); signupOK = true; } / if (WiFi.status() != WL_CONNECTED) { Serial.println("wifi disconected");

    delay(200);
}

if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 8000 || sendDataPrevMillis == 0)) { sendDataPrevMillis = millis(); Firebase.RTDB.getString(&fbdo, "/CarController/Engine_Start"); if (fbdo.dataType() == "string") {

        cstate= fbdo.stringData();
        if(cstate!= Engine_State )
        { Engine_State =cstate;
        chstate=1;
        }

    } else {

Serial.println(fbdo.errorReason());

}
if (Firebase.RTDB.getString(&fbdo, "/CarController/HardStop")) { if (fbdo.dataType() == "string") { sstate= fbdo.stringData();

    } }else {

Serial.println(fbdo.errorReason()); }

if (Firebase.RTDB.getString(&fbdo, "/CarController/CarLock")) { if (fbdo.dataType() == "string") {

        dlcstate= fbdo.stringData();
        if(dlcstate!= doorlock_State )
        { doorlock_State =dlcstate;
        dlchstate=1;
        }

    } }else {

Serial.println(fbdo.errorReason()); } if (Firebase.RTDB.getString(&fbdo, "/CarController/Light")) { if (fbdo.dataType() == "string") {

        licstate= fbdo.stringData();
        if(licstate!= light_State )
        { light_State =licstate;
        lichstate=1;
        }

    } }else {

Serial.println(fbdo.errorReason()); }

}
//if (!Firebase.ready()) { Firebase.begin(&config, &auth); // connect to firebase // Firebase.signUp(&config, &auth, "", ""); // Serial.println("ok");

//delay(1000);} int buttonState = digitalRead(button1Pin); if (buttonState == HIGH) { // Button is pressed if (Engine_State == "1") { Engine_State = "0"; // Change from 1 to 0 chstate=1; chstate1=1; } else { Engine_State = "1"; // Change from 0 to 1 chstate=1; chstate1=1; } } if (chstate1 == 1) { Firebase.RTDB.setString(&fbdo, "/CarController/Engine_Start", Engine_State.c_str()); Serial.print("Engine_State changed to: "); Serial.println(Engine_State); chstate1 = 0;

  }
  if(chstate==1){
  Serial.print("chstate is 1 ");
if (Engine_State == "1") {
    digitalWrite(Led_22, HIGH);
    //Serial.println("LED ON"); 
}
else {
    digitalWrite(Led_22, LOW);

   // Serial.println("LED OFF");
}

chstate=0; } if(Engine_State=="1") { if (sstate=="0") { digitalWrite(Led_1, HIGH); } else { digitalWrite(Led_1, LOW); } }else { digitalWrite(Led_1, LOW);}

int buttonState1 = digitalRead(Input_1); if (buttonState1 == HIGH) { // Button is pressed if (doorlock_State == "1") { doorlock_State = "0"; // Change from 1 to 0 dlchstate=1; dlchstate1=1; } else { doorlock_State = "1"; // Change from 0 to 1 dlchstate=1; dlchstate1=1; } } if (dlchstate1 == 1) { Firebase.RTDB.setString(&fbdo, "/CarController/CarLock", doorlock_State.c_str()); Serial.print("doorlock_State changed to: "); Serial.println(doorlock_State); dlchstate1 = 0;

  }
  if(dlchstate==1){
  Serial.print("chstate is 1 ");
if (doorlock_State == "1") {
    digitalWrite(Led_2, HIGH);
    //Serial.println("LED 2 ON"); 
}
else {
    digitalWrite(Led_2, LOW);

   // Serial.println("LED 2 OFF");
}

dlchstate=0; }

int buttonState2 = digitalRead(Input_2); if (buttonState2 == HIGH) { // Button is pressed if (light_State == "1") { light_State = "0"; // Change from 1 to 0 lichstate=1; lichstate1=1; } else { light_State = "1"; // Change from 0 to 1 lichstate=1; lichstate1=1; } } if (lichstate1 == 1) { Firebase.RTDB.setString(&fbdo, "/CarController/Light", light_State.c_str()); Serial.print("Light_State changed to: "); Serial.println(light_State); lichstate1 = 0;

  }
  if(lichstate==1){
  Serial.print("lichstate is 1 ");
if (light_State == "1") {
    digitalWrite(Led_3, HIGH);
    //Serial.println("LED 3 ON"); 
}
else {
    digitalWrite(Led_3, LOW);

   // Serial.println("LED 3 OFF");
}

lichstate=0; } }`

thank you dear.

mobizt commented 8 months ago

After calling the RTDB function, if it gives the error, you can stop WiFiClient.

fbdo.stopWiFiClient();

I recommend you move to new library with simpler, faster and more reliable. https://github.com/mobizt/FirebaseClient

yousefmlotfy656 commented 8 months ago

After calling the RTDB function, if it gives the error, you can stop WiFiClient.

fbdo.stopWiFiClient();

I recommend you move to new library with simpler, faster and more reliable. https://github.com/mobizt/FirebaseClient

thanks for your response,

actually i didnt know where i should place the stop wifi client and do you think this will solve the payload problem?

mobizt commented 8 months ago

It is because session time out due to server terminates the connection.

You may have to stop WiFiClient when the RTDB function returns false at least every one minute to allow library to reconnect to the server as Google closed the session.

yousefmlotfy656 commented 8 months ago

It is because session time out due to server terminates the connection.

You may have to stop WiFiClient when the RTDB function returns false at least every one minute to allow library to reconnect to the server as Google closed the session.

Thanks for your fast response , now i understand the problem but i didnt know what is the line of the rtdb function that returns false so please can you help me in this point (code)

thanks again.

mobizt commented 8 months ago

The function where fbdo was used.

Anyway, as I suggested, you should consider or prepare to move to new library,

The migration is not too difficult, but much reduce your code complexity.

mobizt commented 8 months ago

After you call to some function e.g. Firebase.RTDB.setString(&fbdo

If that function was called repeatedly for more than 1 minute, you may have to close the session (stop WiFiClient).

mobizt commented 8 months ago

Your code was suitable for async operation or using stream instead. https://github.com/mobizt/Firebase-ESP-Client/tree/main/examples/RTDB/DataChangesListener