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

i have a problem with my code that disconects the tcp connection after few minutes #627

Closed yousefmlotfy656 closed 7 months ago

yousefmlotfy656 commented 7 months ago

`/**

// بتاريخ 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 33

define Led_2 4

define Led_3 25

define Led_4 26

define Led_5 27

define Led_6 14

define Led_22 32

define Input_1 34

define Input_2 35

define button1Pin 36

// 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 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; String Engine_State="0"; String cstate="0"; String DOOR_lOCK_state1; String DOORUNLOCK; String Caralarm; String TrunkOpen; String DOOR_lOCK_state; String Emergency_state; String Stopper_Button_State; int LED_01_State; int intValue; float floatValue; 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,HIGH);
digitalWrite(Led_2,HIGH); digitalWrite(Led_3,HIGH); digitalWrite(Led_4,HIGH); 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
Firebase.begin(&config, &auth); Firebase.reconnectWiFi(true); config.timeout.wifiReconnect = 60 1000; // 1 minute config.timeout.socketConnection = 30 1000; // 30 seconds config.timeout.serverResponse = 30 1000; // 30 seconds config.timeout.rtdbKeepAlive = 120 1000; // 2 minutes config.timeout.rtdbStreamReconnect = 60 1000; // 1 minute config.timeout.rtdbStreamError = 10 1000; // 10 seconds Firebase.RTDB.setMaxRetry(&fbdo, 3); fbdo.setBSSLBufferSize(4096 / Rx buffer size in bytes from 512 - 16384 /, 1024 / Tx buffer size in bytes from 512 - 16384 /);

//
fbdo.setResponseSize(2048);

}

void loop() {

 if (WiFi.status() != WL_CONNECTED) {
    Serial.println("wifi disconected");

    delay(200);
}

int buttonState = digitalRead(button1Pin);

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

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

    } }

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 (Firebase.ready()) { if (Firebase.ready() ){

    // Update the Engine State in Firebase
    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;

    }
    }

// }

// Retrieve Engine State from Firebase

delay(100); // for button rebounce } // Control LED based on Engine State if(chstate==1){ Serial.println("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; } // Other code or operations can be added here if needed delay(50); // Debouncing delay to avoid multiple readings due to button press

} ` this is a debug of a code that show the problem B⸮!⸮B⸮Oc)3B⸮⸮!⸮[ 5678][V][WiFiGeneric.cpp:97] set_esp_interface_ip(): Configuring Station static IP: 0.0.0.0, MASK: 0.0.0.0, GW: 0.0.0.0 wm:connectTimeout not set, ESP waitForConnectResult... [ 6288][V][WiFiGeneric.cpp:362] _arduino_event_cb(): STA Disconnected: SSID: Ap, BSSID: 6e:b0:35:52:ea:e4, Reason: 202 [ 6289][D][WiFiGeneric.cpp:1035] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED [ 6295][W][WiFiGeneric.cpp:1057] _eventCallback(): Reason: 202 - AUTH_FAIL [ 6302][D][WiFiGeneric.cpp:1077] _eventCallback(): WiFi Reconnect Running [ 6381][V][WiFiGeneric.cpp:97] set_esp_interface_ip(): Configuring Station static IP: 0.0.0.0, MASK: 0.0.0.0, GW: 0.0.0.0 [ 7020][V][WiFiGeneric.cpp:355] _arduino_event_cb(): STA Connected: SSID: Ap, BSSID: 6e:b0:35:52:ea:e4, Channel: 1, Auth: WPA2_PSK [ 7021][D][WiFiGeneric.cpp:1035] _eventCallback(): Arduino Event: 4 - STA_CONNECTED [ 7047][V][WiFiGeneric.cpp:369] _arduino_event_cb(): STA Got New IP:192.168.43.110 [ 7047][D][WiFiGeneric.cpp:1035] _eventCallback(): Arduino Event: 7 - STA_GOT_IP [ 7050][D][WiFiGeneric.cpp:1098] _eventCallback(): STA IP: 192.168.43.110, MASK: 255.255.255.0, GW: 192.168.43.57 wm:AutoConnect: SUCCESS *wm:STA IP Address: 192.168.43.110 connected...yeey :)

---------------Sign up Sign up new user... [ 7101][V][WiFiClientImpl.h:317] tcpConnect(): Starting socket ok

[ 8971][V][WiFiClientImpl.h:317] tcpConnect(): Starting socket chstate is 1 Engine_State changed to: 0 chstate is 1 Engine_State changed to: 1 chstate is 1 Engine_State changed to: 0 chstate is 1 [125729][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 104, "Connection reset by peer" [125730][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" [125736][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" Engine_State changed to: 1 chstate is 1 [125805][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" [125807][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" [125813][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" [125871][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" [125873][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" [125879][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" [125937][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" [125939][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" [125945][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" [126003][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" [126005][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" [126011][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" [126069][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" [126071][E][WiFiClientImpl.h:468] tcpWrite(): fail on fd 48, errno: 128, "Socket is not connected" please help me to handle this problem

mobizt commented 7 months ago

May be a router or AP problem, you should try other AP.

yousefmlotfy656 commented 7 months ago

May be a router or AP problem, you should try other AP.

I want the code if there is aproblem in connection they try again until internet comes again with consider that internet connection is stable in my wifi

mobizt commented 7 months ago

It's not because of your code and Firebase library but your network issue related to your AP (router) device or your ESP device.

In some case of ESP32, the outdate WiFi library bugs on old core SDK causes the problem when using WiFi.reconnect function.

This function was used in library internally via Firebase.reconnectWiFi.

If you use ESP32, you have to update the ESP32 Arduino Core SDK to lates to fix the bug.

yousefmlotfy656 commented 7 months ago

firstly i would to thank you for your comment , secondly i did the updated for the sdk and for all libraries used here and still face same problem if there any way to make reopen the tcp connection again if any problem exists to solve this problem

and thank you again

It's not because of your code and Firebase library but your network issue related to your AP (router) device or your ESP device.

In some case of ESP32, the outdate WiFi library bugs on old core SDK causes the problem when using WiFi.reconnect function.

This function was used in library internally via Firebase.reconnectWiFi.

If you use ESP32, you have to update the ESP32 Arduino Core SDK to lates to fix the bug.

firstly i would to thank you for your comment , secondly i did the updated for the sdk and for all libraries used here and still face same problem if there any way to make reopen the tcp connection again if any problem exists to solve this problem

and thank you again

mobizt commented 7 months ago

I all examples, you will see this comment (if you use it carefully) https://github.com/mobizt/Firebase-ESP-Client/blob/51a66259128d9bbc1be61eb9ceb28adc4eaf1a0a/examples/RTDB/Basic/Basic.ino#L118-L119

yousefmlotfy656 commented 7 months ago

a

I all examples, you will see this comment (if you use it carefully)

https://github.com/mobizt/Firebase-ESP-Client/blob/51a66259128d9bbc1be61eb9ceb28adc4eaf1a0a/examples/RTDB/Basic/Basic.ino#L118-L119

thanks for your comment, actually i did the test again but without wifi manager and face same problem

yousefmlotfy656 commented 7 months ago

I all examples, you will see this comment (if you use it carefully)

https://github.com/mobizt/Firebase-ESP-Client/blob/51a66259128d9bbc1be61eb9ceb28adc4eaf1a0a/examples/RTDB/Basic/Basic.ino#L118-L119 i just notice another point the the code didnt take the button high effect only when effect when i click for such a second

mobizt commented 7 months ago

You should test with library example alone, if you still get the issue your device or router is the problem.

mobizt commented 7 months ago

Actually, there is no problem with all library examples as I intensively test 24/7 for many years in many devices.

When integration use with third party library, you have to debug it yourself.