-
Hi. Highly appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 6 replies
-
The current auth token can be obtained from You can check its status with.
Normally, library will refresh the token 5 minutes before it expires (60 - 5 or every 55 minutes). You can set the token refresh time earlier than that default value, e.g., refresh in 10 minutes since it created with.
Or you want to refresh token immediately with. |
Beta Was this translation helpful? Give feedback.
-
Thank you mobizt. Actually, instead of the real token itself, I want to get the getTokenstatus like the code inside TokenHelper.h below /* The helper function to get the token status string */
} Is there anyway I can get the result of that into a string ? |
Beta Was this translation helpful? Give feedback.
-
You are right. My guess was wrong when I thinking that update the database during token refreshing cause the 8266 to crash. /* 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 fbdo;
FirebaseAuth auth;
FirebaseConfig config;
FirebaseJson json;
WiFiUDP UDP;
NTPClient timeClient(UDP, "jp.pool.ntp.org"); //original "pool.ntp.org"
bool logging = 1;
unsigned long sendDataPrevMillis = 0, uptime_ml;
unsigned long count = 0, start_ts;
String uid, databasePath;
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
WiFiMulti multi;
#endif
void setup()
{
Serial.begin(115200);
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
multi.addAP(WIFI_SSID, WIFI_PASSWORD);
multi.run();
#else
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
#endif
Serial.print("Connecting to Wi-Fi");
unsigned long ms = millis();
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
if (millis() - ms > 10000)
break;
#endif
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
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 = ***** ;
auth.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 = tokenStatusCallback; // see addons/TokenHelper.h
// The WiFi credentials are required for Pico W
// due to it does not have reconnect feature.
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
config.wifi.clearAP();
config.wifi.addAP(WIFI_SSID, WIFI_PASSWORD);
#endif
Firebase.begin(&config, &auth);
// Comment or pass false value when WiFi reconnection will control by your code or third party library
Firebase.reconnectWiFi(true);
Firebase.setDoubleDigits(5);
//config.signer.preRefreshSeconds = 3600-120; // every 2 mins
// Print user UID
uid = auth.token.uid.c_str();
Serial.print("User UID: ");
Serial.println(uid);
databasePath = "/UsersData/" + uid + "/ESP_Test";
start_ts = getTime();
//Serial.print("Token status is ");
//Serial.println(getTokenStatus(info));
}
void loop()
{
//while (config.token_status_callback != "ready")
Serial_process();
// Firebase.ready() should be called repeatedly to handle authentication tasks.
up_time_log();
log_data_fb();
}
void log_data_fb(){
if (logging && (millis() - sendDataPrevMillis > 10000 || sendDataPrevMillis == 0))
{
sendDataPrevMillis = millis();
if (count == 0)
{
/*
json.set("value/round/" + String(count), F("cool!"));
json.set(F("vaue/ts/.sv"), F("timestamp"));
Serial.printf("Set json... %s\n", Firebase.set(fbdo, databasePath +"/test/json", json) ? "ok" : fbdo.errorReason().c_str());
*/
}
else
{
//json.add(String(count), "smart!");
int rpm = random(1,100);
float power = random(0.00,100.00);
unsigned long timestamp = getTime();
json.add("RPM", String(rpm));
json.add("Power", String(power));
json.add("TS", String(timestamp));
Serial.printf("Update node... %s\n", Firebase.updateNode(fbdo, databasePath +"/test/json/readings/"+ String(timestamp), json) ? "ok" : fbdo.errorReason().c_str());
}
Serial.println();
if (count == 12){
Firebase.refreshToken(&config);
count = 0;
}
count++;
}
}
void up_time_log(){
if (millis() - uptime_ml > 10000 && Firebase.ready())
{
uptime_ml = millis();
unsigned long up_time_sec = round(millis()/1000);
Serial.printf("Up date uptime... %s\n", Firebase.setInt(fbdo, databasePath + "/up_time/" + String(start_ts), up_time_sec ) ? "ok" : fbdo.errorReason().c_str());
}
}
// Function that gets current epoch time
unsigned long getTime() {
timeClient.update();
unsigned long now = timeClient.getEpochTime();
return now;
}
Can you help me point out the cause of crashing here, I already change the memory setting to option 3 ( 16kB cache + 48kB IRAM & 2heap shared) |
Beta Was this translation helpful? Give feedback.
-
Please update the library to latest version and try again. Let me know if you still get the issue. |
Beta Was this translation helpful? Give feedback.
-
You should check your device Free Heap and enable Arduino IDE debug options for WiFi, SSL and OOM (out of memory). When device crashed with stack status message in serial, you should decode that stack trace with this Arduino IDE plug in tool. |
Beta Was this translation helpful? Give feedback.
-
You are trying to add value repeatedly with the same key. json.add("RPM", String(rpm));
json.add("Power", String(power));
json.add("TS", String(timestamp)); Which multiple objects in JSON with the same key or name is valid and allowed. This causes your json data grow up and takes more memory and device crached. You should call |
Beta Was this translation helpful? Give feedback.
-
Hi again. Thank you for your suggestion. I am not a pro at C language, so it might take me some times to get used with the debug tool you showed me, but will try when time allows me. But for simplify the usage, I am still prefer to use json as global. Can you give me a quick explain of how to use the clear() for json ? Not sure if it's related or not, but when I tried to use the above code along with your ESP-Email-Client library to send email, I can't get it to work when I tried to connect to smtp server, it crashed. Have you experience any similar situation ? |
Beta Was this translation helpful? Give feedback.
-
Use Library was intensively tested 24/7 for years and bugs free in latest version. This library works fine with ESP Mail Client library. You should debug what I mentioned before. The SSL Client used in Your device free heap may be low when using two You can see what is going on internally with core WiFi and SSL engine libraries from Arduino debug information. |
Beta Was this translation helpful? Give feedback.
-
The latest library version is 4.3.13. If you use ESP8266 core v3.x.x, you should set the heap memory options by follow this guide. |
Beta Was this translation helpful? Give feedback.
-
My bad, the board is 3.1.2, the library is 4.3.10 (not the latest) sorry. Currently switching back the json to global and using set instead of add, and it still good for about 30mins... |
Beta Was this translation helpful? Give feedback.
-
Thank you mobizt, for spending explaining many things. My board still up for more than 1 hour now which means very positive result for the memory trouble. I will try to pause and and resume + email server connecting. Again really appreciated your support. I still have some other questions about the usage of the library but not related to this, maybe some other times. |
Beta Was this translation helpful? Give feedback.
The current auth token can be obtained from
Firebase.getToken()
.You can check its status with.
Firebase.authenticated()
<- returns Firebase authentication statusFirebase.isTokenExpired()
<- returns token expiry statusFirebase.ready()
<- returns authentication status AND token expiry status AND network status.<FirebaseData>.httpConnected()
returns server connecting status in almost present time.Normally, library will refresh the token 5 minutes before it expires (60 - 5 or every 55 minutes). You can set the token refresh time earlier than that default value, e.g., refresh in 10 minutes since it created with.
config.signer.preRefreshSeconds = 3600 - 10 * 60;
Or you want to refresh toke…