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

Repeating Error Code -3, Not Connected using v4.3.3 #468

Closed djmick closed 1 year ago

djmick commented 1 year ago

After initial connection to RTDB using v4.3.3 i'm repeatedly getting "stream timed out, resuming..." followed by "error code: -3, reason: not connected" every 3 seconds. As you'll see in my example code, Firebase.ready() is being called repeatedly.

Opening port Port open Connecting to Wi-Fi. Connected with IP: 10.0.0.121


Current Boot Time (EDT): 01/21/2023 04:11:37 PM

Firebase Client v4.3.3


01/21/2023 04:11:37 PM Token info: type = id token (GITKit token), status = on request



01/21/2023 04:11:38 PM Token info: type = id token (GITKit token), status = ready


RTDB User Path: /users/xxxxxxxxxxxxxxxxxxxxxxxxxxxx 01/21/2023 04:11:38 PM


01/21/2023 04:11:39 PM stream timed out, resuming...


sream path, /users/xxxxxxxxxxxxxxxxxxxxxxxxxxxx event path, / data type, json event type, put

Pretty printed JSON data: { "dht": { "humitity": "19%", "lastUpdated": 1674330519425, "temperature": "71°F" }, "serverTimeStamp": 1674333984382 } Iterate JSON data:

0, Type: object, Name: dht, Value: {"humitity":"19%","lastUpdated":1674330519425,"temperature":"71°F"} 1, Type: object, Name: humitity, Value: "19%" 2, Type: object, Name: lastUpdated, Value: 1674330519425 3, Type: object, Name: temperature, Value: "71°F" 4, Type: object, Name: serverTimeStamp, Value: 1674333984382

Received stream payload size: 147 (Max. 147)


01/21/2023 04:11:43 PM stream timed out, resuming...

error code: -3, reason: not connected



01/21/2023 04:11:46 PM stream timed out, resuming...

error code: -3, reason: not connected



01/21/2023 04:11:50 PM stream timed out, resuming...

error code: -3, reason: not connected



01/21/2023 04:11:53 PM stream timed out, resuming...

error code: -3, reason: not connected



01/21/2023 04:11:56 PM stream timed out, resuming...

error code: -3, reason: not connected


`/**

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

if defined(ESP32) || defined(PICO_RP2040)

include

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>

/ 1. Define the WiFi credentials /

define WIFI_SSID "WIFI_AP"

define WIFI_PASSWORD "WIFI_PASSWORD"

// For the following credentials, see examples/Authentications/SignInAsUser/EmailPassword/EmailPassword.ino

/ 2. Define the API Key /

define API_KEY "API_KEY"

/ 3. Define the RTDB URL /

define DATABASE_URL "URL" //.firebaseio.com or ..firebasedatabase.app

/ 4. Define the user Email and password that alreadey registerd or added in your project /

define USER_EMAIL "USER_EMAIL"

define USER_PASSWORD "USER_PASSWORD"

// Define Firebase Data object FirebaseData stream; FirebaseData fbdo;

FirebaseAuth auth; FirebaseConfig config;

unsigned long sendDataPrevMillis = 0;

int count = 0;

volatile bool dataChanged = false;

String fbDbPath;

void streamCallback(FirebaseStream data) { Serial.printf("sream path, %s\nevent path, %s\ndata type, %s\nevent type, %s\n\n", data.streamPath().c_str(), data.dataPath().c_str(), data.dataType().c_str(), data.eventType().c_str()); printResult(data); // see addons/RTDBHelper.h Serial.println();

// This is the size of stream payload received (current and max value)
// Max payload size is the payload size under the stream path since the stream connected
// and read once and will not update until stream reconnection takes place.
// This max value will be zero as no payload received in case of ESP8266 which
// BearSSL reserved Rx buffer size is less than the actual stream payload.
Serial.printf("Received stream payload size: %d (Max. %d)\n\n", data.payloadLength(), data.maxPayloadLength());

// Due to limited of stack memory, do not perform any task that used large memory here especially starting connect to server.
// Just set this flag and check it status later.
dataChanged = true;

}

void streamTimeoutCallback(bool timeout) { Serial.println("***"); printCurrentTime(true); if (timeout) Serial.println("stream timed out, resuming...\n");

if (!stream.httpConnected())
    Serial.printf("error code: %d, reason: %s\n", stream.httpCode(), stream.errorReason().c_str());

Serial.println("***************");
Serial.println();
Serial.println();

}

void setup() {

Serial.begin(115200);

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

ntpInit(); //get time

Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);

/* Assign the api key (required) */
config.api_key = API_KEY;

/* Assign the user sign in credentials */
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;

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

/* Assign the callback function for the long running token generation task */
config.token_status_callback = my_tokenStatusCallback; // see addons/TokenHelper.h

config.signer.preRefreshSeconds = 3600 - 60;

Firebase.begin(&config, &auth);

Firebase.reconnectWiFi(true);

//Set the size of HTTP response buffers in the case where we want to work with large data.
fbdo.setResponseSize(1024);

String fbUid = auth.token.uid.c_str();
fbDbPath =  "/users/" + fbUid;
Serial.print("RTDB User Path: ");
Serial.println(fbDbPath);

printCurrentTime(true);

if (!Firebase.RTDB.beginStream(&stream, fbDbPath))
    Serial.printf("sream begin error, %s\n\n", stream.errorReason().c_str());

Firebase.RTDB.setStreamCallback(&stream, streamCallback, streamTimeoutCallback);

}

void loop() {

if (Firebase.ready() && (millis() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0))
{
    sendDataPrevMillis = millis();
    //count++;
    //FirebaseJson json;
    //json.add("data", "hello");
    //json.add("num", count);
    //Serial.printf("Set json... %s\n\n", Firebase.RTDB.setJSON(&fbdo, "/test/stream/data/json", &json) ? "ok" : fbdo.errorReason().c_str());
}

if (dataChanged)
{
    dataChanged = false;
    // When stream data is available, do anything here...
}

}

void my_tokenStatusCallback(TokenInfo info) { /** fb_esp_auth_token_status enum

const char* ntpServer = "pool.ntp.org"; const long gmtOffset_sec = 18000; const int daylightOffset_sec = 3600;

define TZ "EST5EDT,M3.2.0,M11.1.0"

void ntpInit() { configTime(0, 0, ntpServer); // 0, 0 because we will use TZ in the next line setenv("TZ", TZ, 1); // Set environment variable with your time zone tzset();

time_t now;
struct tm timeinfo;

if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
}
else {

    Serial.println("--------------------");
    Serial.print("Current Boot Time (EDT): ");
    Serial.println(&timeinfo, "%m/%d/%Y %I:%M:%S %p");
    Serial.println("--------------------");
    Serial.println();
}

}

void printCurrentTime(bool newLine) { time_t now; struct tm timeinfo;

//Serial.println();

if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
    return;
}
else if (newLine) {
    Serial.println(&timeinfo, "%m/%d/%Y %I:%M:%S %p");
}
else {
    Serial.print(&timeinfo, "%m/%d/%Y %I:%M:%S %p");
}

}

`

mobizt commented 1 year ago

The library works normally and there is nothing updated that involved this issue. This is not the library issue unless your network issue which you should use other AP to access the internet or reset your router or change your device or power supply.

mobizt commented 1 year ago

I will check again and update.

djmick commented 1 year ago

I'm seeing a clear difference between 4.3.2 & 4.3.3

here's serial log using 4.3.2 Opening port Port open

Connected with IP: 10.0.0.121


Current Boot Time (EDT): 01/21/2023 05:37:03 PM

Firebase Client v4.3.2


01/21/2023 05:37:03 PM Token info: type = id token (GITKit token), status = on request



01/21/2023 05:37:08 PM Token info: type = id token (GITKit token), status = ready


RTDB User Path: /users/xxxxxxxxxxxxxxxxxxxxxxxxxxxx 01/21/2023 05:37:08 PM


01/21/2023 05:37:09 PM stream timed out, resuming...


sream path, /users/xxxxxxxxxxxxxxxxxxxxxxxxxxxx event path, / data type, json event type, put

Pretty printed JSON data: { "dht": { "humitity": "19%", "lastUpdated": 1674330519425, "temperature": "71°F" }, "serverTimeStamp": 1674333984382 } Iterate JSON data:

0, Type: object, Name: dht, Value: {"humitity":"19%","lastUpdated":1674330519425,"temperature":"71°F"} 1, Type: object, Name: humitity, Value: "19%" 2, Type: object, Name: lastUpdated, Value: 1674330519425 3, Type: object, Name: temperature, Value: "71°F" 4, Type: object, Name: serverTimeStamp, Value: 1674333984382

Received stream payload size: 147 (Max. 147)


01/21/2023 05:37:39 PM



01/21/2023 05:38:09 PM Token info: type = id token (GITKit token), status = on refreshing



01/21/2023 05:38:14 PM Token info: type = id token (GITKit token), status = ready



01/21/2023 05:38:14 PM



01/21/2023 05:38:39 PM



01/21/2023 05:39:09 PM



01/21/2023 05:39:15 PM Token info: type = id token (GITKit token), status = on refreshing



01/21/2023 05:39:20 PM Token info: type = id token (GITKit token), status = ready


same code after clean install of 4.3.3 Opening port Port open

Connected with IP: 10.0.0.121


Current Boot Time (EDT): 01/21/2023 05:42:07 PM

Firebase Client v4.3.3


01/21/2023 05:42:07 PM Token info: type = id token (GITKit token), status = on request



01/21/2023 05:42:08 PM Token info: type = id token (GITKit token), status = ready


RTDB User Path: /users/xxxxxxxxxxxxxxxxxxxxxxxxxxxx 01/21/2023 05:42:08 PM


01/21/2023 05:42:09 PM stream timed out, resuming...


sream path, /users/xxxxxxxxxxxxxxxxxxxxxxxxxxxx event path, / data type, json event type, put

Pretty printed JSON data: { "dht": { "humitity": "19%", "lastUpdated": 1674330519425, "temperature": "71°F" }, "serverTimeStamp": 1674333984382 } Iterate JSON data:

0, Type: object, Name: dht, Value: {"humitity":"19%","lastUpdated":1674330519425,"temperature":"71°F"} 1, Type: object, Name: humitity, Value: "19%" 2, Type: object, Name: lastUpdated, Value: 1674330519425 3, Type: object, Name: temperature, Value: "71°F" 4, Type: object, Name: serverTimeStamp, Value: 1674333984382

Received stream payload size: 147 (Max. 147)


01/21/2023 05:42:13 PM stream timed out, resuming...

error code: -3, reason: not connected



01/21/2023 05:42:19 PM stream timed out, resuming...

error code: -3, reason: not connected


Port open


01/21/2023 05:42:30 PM Token info: type = id token (GITKit token), status = ready


RTDB User Path: /users/xxxxxxxxxxxxxxxxxxxxxxxxxxxx 01/21/2023 05:42:30 PM sream path, /users/xxxxxxxxxxxxxxxxxxxxxxxxxxxx event path, / data type, json event type, put

Pretty printed JSON data: { "dht": { "humitity": "19%", "lastUpdated": 1674330519425, "temperature": "71°F" }, "serverTimeStamp": 1674333984382 } Iterate JSON data:

0, Type: object, Name: dht, Value: {"humitity":"19%","lastUpdated":1674330519425,"temperature":"71°F"} 1, Type: object, Name: humitity, Value: "19%" 2, Type: object, Name: lastUpdated, Value: 1674330519425 3, Type: object, Name: temperature, Value: "71°F" 4, Type: object, Name: serverTimeStamp, Value: 1674333984382

Received stream payload size: 147 (Max. 147)


01/21/2023 05:42:31 PM stream timed out, resuming...

error code: -3, reason: not connected



01/21/2023 05:42:31 PM stream timed out, resuming...

error code: -3, reason: not connected



01/21/2023 05:42:36 PM stream timed out, resuming...

error code: -3, reason: not connected



01/21/2023 05:42:39 PM stream timed out, resuming...

error code: -3, reason: not connected



01/21/2023 05:42:45 PM stream timed out, resuming...

error code: -3, reason: not connected



01/21/2023 05:42:49 PM stream timed out, resuming...

error code: -3, reason: not connected



01/21/2023 05:42:53 PM stream timed out, resuming...

error code: -3, reason: not connected



01/21/2023 05:42:58 PM stream timed out, resuming...

error code: -3, reason: not connected



01/21/2023 05:43:01 PM stream timed out, resuming...

error code: -3, reason: not connected


mobizt commented 1 year ago

The library update is available, please update.