mobizt / Firebase-ESP32

[DEPRECATED]🔥 Firebase RTDB Arduino Library for ESP32. The complete, fast, secured and reliable Firebase Arduino client library that supports CRUD (create, read, update, delete) and Stream operations.
MIT License
414 stars 119 forks source link

Payload read timed out and connection refused after sending around 500 values to the Firebase #326

Closed DhyeyBhavsar closed 3 months ago

DhyeyBhavsar commented 3 months ago

So basically I am sending a sensor data to the Firebase every 1000ms and during the process the first error comes around 99th value which i send to Firebase like it says Payload timed out and it keeps on going for 212th, 350th and then around 516th value it comes like connection refused.

Error: At 100th, 212th and 350th it comes FAILED REASON: response payload read timed out

After around 500 values it comes FAILED REASON: connection refused

I want to send the data foe continuously every sec for longer period time or infinity.

Code:

include

include

include

include

include

include

include

include

include "addons/TokenHelper.h"

include "addons/RTDBHelper.h"

define WIFI_SSID "xxxx"

define WIFI_PASSWORD "xxxx"

define API_KEY "xxxx"

define DATABASE_URL "xxxxx"

define SCREEN_WIDTH 128

define SCREEN_HEIGHT 64

define OLED_MOSI 23

define OLED_CLK 18

define OLED_DC 16

define OLED_CS 5

define OLED_RESET 17

define Preesure_sensor 36

define inputCLK 27

define inputDT 14

define Button 26

define EEPROM_SIZE 400

const int CLK_pin = 27; const int DT_pin = 14;

//int buzzer=25; int n=0; unsigned long lastButtonPress = 0;

int lastState_Button = LOW; int State_LED = 0; int sum=0; int avg; int counter = 0; int counter1 ; float counter2=0.0f; //for calibration int currentStateCLK; int previousStateCLK;

int newcount=0; float sensor; float output_psi; float rounded_output_psi; float changepsi;

unsigned long lasttimedisplayblink = 0; const unsigned long blinkdelay = 1000;

unsigned long lasttimedisplayblink1 = 0; const unsigned long blinkdelay1 = 1000;

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

RTC_DS1307 rtc;

char daysOfWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };

FirebaseData fbdo, fbdo_s1, fbdo_s2, fbdo_s3; FirebaseAuth auth; FirebaseConfig config; bool signupOK = false; unsigned long sendDataPrevMillis = 0; unsigned long sendDataPrevMillis1 = 0; unsigned long sendDataPrevMillis2 = 0;

SemaphoreHandle_t baton; TaskHandle_t Task1,Task2;

int lowchange=0; int highchange=0; float calibratechange=0;

void setup() { pinMode (inputCLK,INPUT_PULLUP); pinMode (inputDT,INPUT_PULLUP); pinMode(Button, INPUT_PULLUP);

//pinMode(buzzer,OUTPUT); Serial.begin(115200); Serial.println(ESP.getSdkVersion());
if(!display.begin(SSD1306_SWITCHCAPVCC)) { Serial.println(F("SSD1306 allocation failed")); for(;;); }

if (! rtc.begin()) { Serial.println("RTC module is NOT found"); Serial.flush(); while (1); } rtc.adjust(DateTime(F(DATE), F(TIME)));

previousStateCLK = digitalRead(inputCLK);

EEPROM.begin(EEPROM_SIZE);

display.clearDisplay(); display.setTextSize(3); display.setTextColor(SSD1306_WHITE); display.setCursor(0, 0); display.print("hello World!! "); display.display(); delay(3000); display.clearDisplay();

wificonnection(); firebaseconnection();

//stream if(!Firebase.RTDB.beginStream(&fbdo_s1, "/test")) Serial.printf("stream 1 being error", "%s\n\n", fbdo_s1.errorReason().c_str()); if(!Firebase.RTDB.beginStream(&fbdo_s2, "/test2")) Serial.printf("stream 2 being error", "%s\n\n", fbdo_s2.errorReason().c_str()); if(!Firebase.RTDB.beginStream(&fbdo_s3, "/test3")) Serial.printf("stream 3 being error", "%s\n\n", fbdo_s3.errorReason().c_str());

//baton = xSemaphoreCreateMutex();

xTaskCreatePinnedToCore(
codeForTask1,            /* Task function. */
"Task1",                 /* name of task. */
10000,                    /* Stack size of task */
NULL,                     /* parameter of the task */
0,                        /* priority of the task */
&Task1,                   /* Task handle to keep track of created task */
0);                       /*core */

xTaskCreatePinnedToCore( codeForTask2, / Task function. / "Task2", / name of task. / 10000, / Stack size of task / NULL, / parameter of the task / 2, / priority of the task / &Task2, / Task handle to keep track of created task / 1); / Core / }

void wificonnection(){

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 to IP: "); Serial.println(WiFi.localIP()); Serial.println(); }

void firebaseconnection(){

config.api_key = API_KEY; config.database_url = DATABASE_URL; if(Firebase.signUp(&config, &auth, "", "")){ Serial.print("signUp OK"); signupOK = true; } else{ Serial.printf("%s\n", config.signer.signupError.message.c_str());
}

config.token_status_callback = tokenStatusCallback; Firebase.begin(&config, &auth); Firebase.reconnectWiFi(true); Serial.print("done"); }

void loop() {

}

void codeForTask1( void * parameter ) { for (;;){ ..................................................... } }

void codeForTask2( void * parameter ) { for (;;){ if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 1000 || sendDataPrevMillis == 0)){ sendDataPrevMillis = millis(); if(rounded_output_psi!=changepsi){ if (Firebase.RTDB.setFloat(&fbdo, "test/a", rounded_output_psi)){ Serial.println("PASSED"); Serial.println("PATH: " + fbdo.dataPath()); Serial.println("TYPE: " + fbdo.dataType()); } else { Serial.println("FAILED"); Serial.println("REASON: " + fbdo.errorReason()); } n=n+1; Serial.println(n); changepsi=rounded_output_psi; }

                           if(counter!=lowchange){
                           // Write an Int number on the database path PressureSensor/low
                           if (Firebase.RTDB.setInt(&fbdo, "test/b", counter)){
                           Serial.println("PASSED");
                           Serial.println("PATH: " + fbdo.dataPath());
                           Serial.println("TYPE: " + fbdo.dataType());
                           }
                           else {
                           Serial.println("FAILED");
                           Serial.println("REASON: " + fbdo.errorReason());
                           }  
                           lowchange=counter;
                           }

                           if(counter1!=highchange){
                           if (Firebase.RTDB.setInt(&fbdo, "test/c", counter1)){
                           Serial.println("PASSED");
                           Serial.println("PATH: " + fbdo.dataPath());
                           Serial.println("TYPE: " + fbdo.dataType());
                           }
                           else {
                           Serial.println("FAILED");
                           Serial.println("REASON: " + fbdo.errorReason());
                           }
                           highchange=counter1;
                           }

                           if(counter2!=calibratechange){
                           if (Firebase.RTDB.setFloat(&fbdo, "test/d", counter2)){
                           Serial.println("PASSED");
                           Serial.println("PATH: " + fbdo.dataPath());
                           Serial.println("TYPE: " + fbdo.dataType());
                           }
                           else {
                           Serial.println("FAILED");
                           Serial.println("REASON: " + fbdo.errorReason());
                           }
                           calibratechange=counter2;
                           }

//---------------------------------------read from firebase-------------------------------------------------
if (Firebase.ready() && signupOK){ if(!Firebase.RTDB.readStream(&fbdo_s1)) Serial.printf("stream 1 being error", "%s\n\n", fbdo_s1.errorReason().c_str()); if (fbdo_s1.streamAvailable()) { if (fbdo_s1.dataType() == "int") { counter = fbdo_s1.intData(); Serial.println("Successful read from"); } }

                           if(!Firebase.RTDB.readStream(&fbdo_s2))
                           Serial.printf("stream 2 being error", "%s\n\n", fbdo_s2.errorReason().c_str());
                           if (fbdo_s2.streamAvailable()) {
                           if (fbdo_s2.dataType() == "int") {
                           counter1 = fbdo_s2.intData();
                           Serial.println("Successful read from");
                           }
                           }

                           if(!Firebase.RTDB.readStream(&fbdo_s3))
                           Serial.printf("stream 3 being error", "%s\n\n", fbdo_s3.errorReason().c_str());
                           if (fbdo_s3.streamAvailable()) {
                           if (fbdo_s3.dataType() == "float") {
                           counter2 = fbdo_s3.floatData();
                           Serial.println("Successful read from");
                           }
                           }

                           }
                           delay(10);

} }

mobizt commented 3 months ago

Due to the issue in WiFiClient used internally with this library, you have to close the TCP session every 1 minute by calling fbdo.stopWiFiClient();

This cannot fix with the current library design API.

I recommend you move to the new FirebaseClient library instead.