ESP8266 loses connection after some time

Hi. I’m using the ESP8266-01 Wi-Fi module. I’ve programmed the ESP using Arduino IDE. The ESP will constant read data from Firebase and perform the required action (send data to microcontroller to light up or turn off the LED). After testing it for one day, I found out that the ESP8266 seems lose internet connection after some time as the microcontroller doesn’t light up or turn off the LED after I changed the data in Firebase. I’ve added the code to check for connection in my loop but it’s not working. (The ESP is able to control the LED after I powered it up but loses connection after about 1 hour)

Here is my code:

#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>

#define FIREBASE_HOST "esp8266-01-d9e36.firebaseio.com"
#define FIREBASE_AUTH "XaULswRCs2SzVx39qbZDzsNa8vn1qjNl9NIH4kNA"
#define WIFI_SSID "tanstreamyx"
#define WIFI_PASSWORD "881516525333"


void setup() {
  Serial.begin(9600);
  delay(1000);
                      
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}

void loop() {
  if (WiFi.status() != WL_CONNECTED){
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
    }
  
    Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  }

  // read data from Firebase and control LED

}

Thanks in advance.

I see firebase credentials in your code, which doesn't seem like a good idea unless they're fake.

You've left out the code that talks to firebase though.

  }

  // read data from Firebase and control LED

}

That's exactly the code we would have liked to see... So you use a lot of String magic?

#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>

#define FIREBASE_HOST "esp8266-01-d9e36.firebaseio.com"
#define FIREBASE_AUTH "XaULswRCs2SzVx39qbZDzsNa8vn1qjNl9NIH4kNA"
#define WIFI_SSID "tanstreamyx"
#define WIFI_PASSWORD "881516525333"

String sw0_status_n;
String sw1_status_n;
String sw2_status_n;
String sw0_status_o = "0";
String sw1_status_o = "0";
String sw2_status_o = "0";

String t0_n;
String t1_n;
String t2_n;
String t0_o = "0H0M";
String t1_o = "0H0M";
String t2_o = "0H0M";

void setup() {
  Serial.begin(9600);
  delay(1000);
                      
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  Serial.println("Connected to Wi-Fi.");
  
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);

  Serial.println("Connected to Firebase");
}

void loop() {
  if (WiFi.status() != WL_CONNECTED){
    
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
    }

    Serial.println("Reconnect to Wi-Fi.");
    
    Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);

    Serial.println("Reconnect to Firebase.");
    
  }

  sw0_status_n = Firebase.getString("SW0/status");
  
  if (sw0_status_n == "1" && sw0_status_o == "0") {
    sw0_status_o = "1";
    Serial.print("SW0/1");                          
  } 
  else if (sw0_status_n == "0" && sw0_status_o == "1" ) {
    sw0_status_o = "0";
    Serial.print("SW0/0");
  }

  sw1_status_n = Firebase.getString("SW1/status");
  
  if (sw1_status_n == "1" && sw1_status_o == "0") {
    sw1_status_o = "1";
    Serial.print("SW1/1");                          
  } 
  else if (sw1_status_n == "0" && sw1_status_o == "1" ) {
    sw1_status_o = "0";
    Serial.print("SW1/0");
  }

  sw2_status_n = Firebase.getString("SW2/status");
  
  if (sw2_status_n == "1" && sw2_status_o == "0") {
    sw2_status_o = "1";
    Serial.print("SW2/1");                          
  } 
  else if (sw2_status_n == "0" && sw2_status_o == "1" ) {
    sw2_status_o = "0";
    Serial.print("SW2/0");
  }
  Serial.println("Completed one round");
}

Here’s the complete code. I’ve modified the code, printing something to the serial monitor to see what’s going on. I found out that the ESP is actually not disconnected from the internet. The ESP actually does not read data from Firebase after about one hour.

Is there any setting in Firebase that I missed out or perhaps there is some limit for reading data from Firebase?

*The Firebase here refers to Firebase Realtime Database.

Well, as Septillion suspected, you're using a lot of String objects. That may be causing problems, although since you've got a lot more RAM than a typical Arduino, perhaps not. Get hold of the freemem function and check how much you have to spare.

You're spamming the firebase database pretty hard. It may be cutting you off due to exceeding the number of calls you can make in a time period, especially if you're using a free service. Try sticking a delay in loop, or better use millis so you only call once every few seconds. Note that this will change how long it takes to crash if you do have a problem with Strings.

Does the system start working again immediately if you bounce the ESP?

wildbill: Well, as Septillion suspected, you're using a lot of String objects. That may be causing problems, although since you've got a lot more RAM than a typical Arduino, perhaps not.

So, the problem might due to the string takes up a lot of RAM space? Even if the string is not being sent? Can you please explain more clearly about how the string causing the ESP not reading from Firebase?

wildbill: Get hold of the freemem function and check how much you have to spare.

freemem function? Function used to check RAM?

Strings use dynamic memory allocation, which in turn can fragment your heap to the point that subsequent allocations will silently fail. At that point, all bets are off as to how your program will behave. Read more about it here.

freemem does indeed check how much spare RAM you have. If you see it falling over time, it suggests that there will eventually be trouble.

wildbill: Read more about it here.

So, I should use character array instead of string?

wildbill: freemem does indeed check how much spare RAM you have. If you see it falling over time, it suggests that there will eventually be trouble.

Do you mean freeRam()? Is that a correct way to do it if I print the freeRam() at the end of the loop?

pauline95: So, I should use character array instead of string?

Yes.

Do you mean freeRam()? Is that a correct way to do it if I print the freeRam() at the end of the loop?

Pretty sure the version I had was freemem. Adafruit has one under yet another name here. I'd suggest that printing it every few minutes would be better. Otherwise, any other debugging prints you're using will be swamped.

wildbill: Pretty sure the version I had was freemem.

Is the freemem a built in function or we will need to download the .h and .cpp file in order to use it?

Just grab the source and paste it into your sketch.

Alright. From the link that you provided, I should be copying the code for SRAM right?

It seems like the hole in the heap will continue to increase as time goes on. Am I right? So, if the ESP is to be powered on 24/7, would it be better if I perform ESP.restart() at a regular interval to reset the heap? (I think the ESP.restart() will reset everything?)

Recently I’m testing my ESP again and there is something weird with the ESP. When I test it at about 1900 to 2100, it loses connection with the Firebase after about 2 hours (Firebase.failed() always returns true). However, if I test it at a later time (2130 to 0330 for the first day and 1030 to 0230 for the second day), it doesn’t lose connection with Firebase. It seems like there is no connection problem during midnight but will have problem at earlier time? Does anyone know the reason for this?

Capacity limits?

Does this "firebase' has a limit on how many (API?) calls can be done from a single IP/end point with-in an hour? a day?

Try doing a call twice an hour.. and see how long you can go for...

Based on the Firebase pricing plan (here), the limit for read is 10GB per month and I didn't exceed that limit.

Also, if the Firebase is preventing me from reading the data, why would it has no problem for about 5 hours when I test it till midnight but has problem if I test it at an earlier time?