Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 8

Firebase .getInt() fails after Internet is reconnected - ESP8266

Solved! Go to Solution.

Hi. I'm working on a project where I need to read data from my Firebase database frequently. I use a NodeMCU (ESP8266-12) to connect to a WiFi network and read data from the Firebase. If the Internet is available initially, everything works perfect. I get proper data from Firebase. But if I disconnect the Internet (NOT WiFi), and reconnect it again, then Firebase.getInt() would still return 0, and not the value stored in my database. I checked Firebase.success() function and it also returns 0 after Internet is reconnected. But the odd thing is, Firebase.setInt() works. ie, data is written into Firebase properly even after reconnection. 

 

This is the code:

 


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

// Set these to run example.
#define FIREBASE_HOST "mydatabasehost"
#define FIREBASE_AUTH "authentication"
#define WIFI_SSID "OnePlus 3"
#define WIFI_PASSWORD "cristiano"


void setup() {


Serial.begin(9600);

// connect to wifi.
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());

Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);

}

 

void loop() {

Serial.println(Firebase.getInt("manual"));
if(Firebase.failed())
{
Serial.println("Internet lost");
delay(5000);
}
Firebase.setInt("x",20);
}

7 Replies
Solution
Commander Lvl 3
Message 2 of 8

Re: Firebase .getInt() fails after Internet is reconnected - ESP8266

I wouldn't know why this happens, but why don't you just reconnect to the WiFi network after you've detected the internet is lost (= just calling setup() again)?

 

PS: Next time could you please put your code in a code block? Using the </> icon in the toolbar?


- Mark
Copilot Lvl 2
Message 3 of 8

Re: Firebase .getInt() fails after Internet is reconnected - ESP8266

Thanks for the reply. I was thinking the same thing too, but in a different way. I thought about using an ESP.restart() once internet is reconnected. I tried it a couple of hours ago and looks like it worked. Restarting the whole system seemed to have fixed the issue. But still it's weird that it fails to read from firebase even when internet is connected.

 

By the way, is there any way I could send an unsigned long data to firebase using Arduino library functions (Like Firebase.setInt().. I couldn't find one for "long" ) ?

Commander Lvl 3
Message 4 of 8

Re: Firebase .getInt() fails after Internet is reconnected - ESP8266

For the first part, glad it worked fine!

 

For the second part of your new question: I really don't know enough about Firebase to really comment on that, but a quick Google search turned up this: https://stackoverflow.com/questions/31930406/storing-long-type-in-firebase - it might be of use. Otherwise I would recommend asking a new question on this forum, both so everything stays organized but also to make sure your new question gets the attention it deservers (because this one is already marked as solved, not many people will look at it anymore).


- Mark
Ground Controller Lvl 1
Message 5 of 8

Re: Firebase .getInt() fails after Internet is reconnected - ESP8266

I have the same problem. Also, sometime I've got Firebase .getInt() failed when power on. So I use ESP.restart() everytime when an Firebase.failed() 

I don't know why this happens but this trick works for me

Ground Controller Lvl 1
Message 6 of 8

Re: Firebase .getInt() fails after Internet is reconnected - ESP8266

you can use ArduinoJson and embedd the long var inside json object https://arduinojson.org/v5/example/

Ground Controller Lvl 1
Message 7 of 8

Re: Firebase .getInt() fails after Internet is reconnected - ESP8266

No need to reconnect the device to wifi. Just try to add some value to the firebase before checking the connection. This line will helps you.

void loop()

{

Firebase.setInt("a",0);

if(Firebase.failed())

{

.....

}

Ground Controller Lvl 1
Message 8 of 8

Re: Firebase .getInt() fails after Internet is reconnected - ESP8266

In my case, fix Firebase.cpp AND fix FirebaseArduino.cpp of firebasearduino libs and worked perfectly

1) Change FirebaseArduino.cpp:

void FirebaseCall::analyzeError(char* method, int status, const std::string& path_with_auth) {
if (status != 200) {
error_ = FirebaseError(status,
std::string(method) + " " + path_with_auth +
": " + http_->errorToString(status));
}

}

To:

void FirebaseCall::analyzeError(char* method, int status, const std::string& path_with_auth) {
if (status != 200) {
error_ = FirebaseError(status,
std::string(method) + " " + path_with_auth +
": " + http_->errorToString(status));
}
else {
error_ = FirebaseError();
}
}


2) Change FirebaseArduino.cpp:

int FirebaseArduino::getInt(const String& path) {
getRequest(path);
if (failed()) {
return 0;
}
return FirebaseObject(req_.get()->response().c_str()).getInt();
}

To:

int FirebaseArduino::getInt(const String& path) {
getRequest(path);
if (failed()) {
return -1;
}
return FirebaseObject(req_.get()->response().c_str()).getInt();
}

You can change all get function to: return -1 for your code