I'm for the moment totally clueless of why Firebase stops working after one hour.
I have stripped down the sketch now to a bare minimum to mitigate the risk of influence of my rookie code. But still no success.
Found this about using NTP and change the time after Firebase was initiated, which I did. But it's now removed but the problem remains. It's an ESP8266 i am using.
Found this thread:
Auth token is expired · Discussion #83 · mobizt/Firebase-ESP-Client · GitHub
But I do not really get it.
Please help! I am not getting anywhere. :- (
StefanL38 - Please, do not comment/trash this thread.
/*
07:00:50.673 -> *wm:AutoConnect
07:00:50.673 -> *wm:Connecting to SAVED AP: turbo2
07:00:51.154 -> *wm:connectTimeout not set, ESP waitForConnectResult...
07:00:53.045 -> *wm:AutoConnect: SUCCESS
07:00:53.045 -> *wm:STA IP Address: 10.42.0.237
07:00:53.076 -> connected...yeey :)
07:00:53.076 -> Firebase Client v4.3.4
07:00:53.076 ->
07:00:53.076 -> Token info: type = id token (GITKit token), status = on request
07:00:54.328 -> Token info: type = id token (GITKit token), status = ready
07:00:56.380 -> Did Write : 4840
07:00:56.508 -> Did Read : 4840
07:15:56.531 -> FAILED set Millis REASON: send request failed
07:15:57.590 -> Did Read : 4840
07:30:58.694 -> Did Write : 1807082
07:30:58.823 -> Did Read : 1807082
07:45:59.926 -> Did Write : 2708312
07:46:00.054 -> Did Read : 2708312
07:59:10.507 -> Refreshing token
08:01:01.146 -> FAILED set Millis REASON: Permission denied
08:01:02.203 -> FAILED - get Millis REASON: Permission denied
*/
// 230126
// AutoConnectNonBlocking WiFi
//###############################################################################
//################## Wifimanager INIT ################################
//###############################################################################
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager 2.0.15-rc.1 Tzapu
WiFiManager wm;
//###############################################################################
//################## Firebase INIT ######################################
//###############################################################################
/**
* Created by K. Suwatchai (Mobizt)
*
* Email: k_suwatchai@hotmail.com
*
* Github: https://github.com/mobizt/Firebase-ESP-Client
*
* Copyright (c) 2023 mobizt
*
*/
/** This example will show how to authenticate as a user with Email and password.
*
* You need to enable Email/Password provider.
* In Firebase console, select Authentication, select Sign-in method tab,
* under the Sign-in providers list, enable Email/Password provider.
*
* From this example, the user will be granted to access the specific location that matches
* the user uid.
*
* This example will modify the database rules to set up the security rule which need to
* guard the unauthorized access with the user Email.
*/
#include <Arduino.h>
#if defined(ESP32) || defined(PICO_RP2040)
//#include <WiFi.h>
#elif defined(ESP8266)
//#include <ESP8266WiFi.h>
#endif
#include <Firebase_ESP_Client.h> // Latest 4.3.4 by Mobitz
// 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 "AP"
//#define WIFI_PASSWORD "AP-password"
/** 2. Define the API key
*
* The API key (required) can be obtained since you created the project and set up
* the Authentication in Firebase console. Then you will get the API key from
* Firebase project Web API key in Project settings, on General tab should show the
* Web API Key.
*
* You may need to enable the Identity provider at https://console.cloud.google.com/customer-identity/providers
* Select your project, click at ENABLE IDENTITY PLATFORM button.
* The API key also available by click at the link APPLICATION SETUP DETAILS.
*
*/
#define API_KEY "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
/* 3. Define the user Email and password that already registerd or added in your project */
#define USER_EMAIL "xxxxxxxxxxx@gmail.com"
#define USER_PASSWORD "xxxxxxxxxxxxxxxxxxxxxxxxx"
/* 4. If work with RTDB, define the RTDB URL */
#define DATABASE_URL "xxxxxxxxxxxxxxxxxxxxxxxxx.europe-west1.firebasedatabase.app" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app
/** 5. Define the database secret (optional)
*
* This database secret needed only for this example to modify the database rules
*
* If you edit the database rules yourself, this is not required.
*/
#define DATABASE_SECRET "DATABASE_SECRET"
/* 6. Define the Firebase Data object */
FirebaseData fbdo;
/* 7. Define the FirebaseAuth data for authentication data */
FirebaseAuth auth;
/* 8. Define the FirebaseConfig data for config data */
FirebaseConfig config;
void setup() {
//###############################################################################
//################## Wifimanager Setup ################################
//###############################################################################
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
// put your setup code here, to run once:
Serial.begin(115200);
//reset settings - wipe credentials for testing
//wm.resetSettings();
wm.setConfigPortalBlocking(false);
wm.setConfigPortalTimeout(60);
//automatically connect using saved credentials if they exist
//If connection fails it starts an access point with the specified name
if (wm.autoConnect("AutoConnectAP")) {
Serial.println("connected...yeey :)");
} else {
Serial.println("Configportal running");
}
//###############################################################################
//################## Firebase Setup ######################################
//###############################################################################
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 */
config.database_url = DATABASE_URL;
Firebase.reconnectWiFi(true);
fbdo.setResponseSize(4096);
String base_path = "/UsersData/";
/* Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
/* Initialize the library with the Firebase authen and config */
Firebase.begin(&config, &auth);
/** Now modify the database rules (if not yet modified)
*
* The user, <user uid> in this case will be granted to read and write
* at the certain location i.e. "/UsersData/<user uid>".
*
* If you database rules has been modified, please comment this code out.
*
* The character $ is to make a wildcard variable (can be any name) represents any node key
* which located at some level in the rule structure and use as reference variable
* in .read, .write and .validate rules
*
* For this case $userId represents any <user uid> node that places under UsersData node i.e.
* /UsersData/<user uid> which <user uid> is user UID.
*
* Please check your the database rules to see the changes after run the below code.
*/
String var = "$userId";
String val = "($userId === auth.uid && auth.token.premium_account === true && auth.token.admin === true)";
Firebase.RTDB.setReadWriteRules(&fbdo, base_path, var, val, val, DATABASE_SECRET);
/** path for user data is now "/UsersData/<user uid>"
*
* The id token can be accessed from Firebase.getToken().
*
* The refresh token can be accessed from Firebase.getRefreshToken().
*/
}
unsigned long dataMillis = 0;
int count = 0;
unsigned long testMillis;
bool firstRun = true;
int getMillis;
unsigned int lastTokenRefresh;
void loop() {
wm.process();
// put your main code here, to run repeatedly:
// if ((millis() - dataMillis > 30000 && Firebase.ready()) || (firstRun == true && Firebase.ready())) { // Run once every hour
if ((millis() - dataMillis > 900000) || (firstRun == true)) { // Run once every hour
testMillis = millis();
if (Firebase.RTDB.setInt(&fbdo, "dataExchange/theMillis", (testMillis))) { // set millis
//Serial.println("PASSED");
//Serial.println("PATH: " + fbdo.dataPath());
//Serial.println("TYPE: " + fbdo.dataType());
Serial.print("Did Write : ");
Serial.println(testMillis);
//setInt = false;
} else {
Serial.println("FAILED set Millis REASON: " + fbdo.errorReason());
//setInt = false;
}
if (Firebase.RTDB.getInt(&fbdo, "dataExchange/theMillis")) { // get millis
getMillis = fbdo.intData();
// Serial.println("PATH: " + fbdo.dataPath());
// Serial.println("TYPE: " + fbdo.dataType());
Serial.print("Did Read : ");
Serial.println(getMillis);
} else {
Serial.println("FAILED - get Millis REASON: " + fbdo.errorReason());
}
firstRun = false;
dataMillis = millis();
}
if (millis() - lastTokenRefresh > 3500000) { // Run once every hour to "refresh token". Did obviously not help..
Serial.println("Refreshing token");
Firebase.getRefreshToken();
lastTokenRefresh = millis();
}
}