[SOLVED] EPOCH TIME with ESP Board Manager v.3.0.1

Hi,
Do you guys know which version of this library: GitHub - arduino-libraries/NTPClient: Connect to a NTP server or any other library that works fine with the ESP board manager v.3.0.1 obtaining the Epoch time?
I use the function .getEpochTime(), assign the output value to an unsigned long variable and then cast it as a String and it does only return a string of 4 digits as the code below:

unsigned long now = timeClient.getEpochTime();
json.add("timestamp", (String)now);

Get Epoch/Unix Time with the ESP32 (Arduino) | Random Nerd Tutorials.

Followed their tutorial using the ESP8266. It doesn't work

This will work both with ESP32 and ESP8266

Yea, an ESP8266 and a ESP32 are different animals. Sorry for posting the wrong link.

I'm using Firebase Arduino Client Library for ESP8266 and ESP32 v.2.3.7 and the ESP board manager v.3.0.1
Can I ask you if you can help me putting this code (I use an ESP01) into this one:
Thx in advance

#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <Firebase_ESP_Client.h>

//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 "TIM-29846851"
#define WIFI_PASSWORD "***"

/* 2. Define the API Key */
#define API_KEY "***"

/* 3. Define the RTDB URL */
#define DATABASE_URL "***" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app

/* 4. Define the user Email and password that alreadey registerd or added in your project */
#define USER_EMAIL "***"
#define USER_PASSWORD "***"

//Define Firebase Data object
FirebaseData fbdo;

FirebaseAuth auth;
FirebaseConfig config;

unsigned long sendDataPrevMillis = 0;

int count = 0;
String values;

void setup()
{

  Serial.begin(9600);

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  //Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED)
  {
    //Serial.print(".");
    delay(300);
  }
  /*.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 = 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 = tokenStatusCallback; //see addons/TokenHelper.h

  //Or use legacy authenticate method
  //config.database_url = DATABASE_URL;
  //config.signer.tokens.legacy_token = "<database secret>";

  Firebase.begin(&config, &auth);

  Firebase.reconnectWiFi(true);

  timeClient.begin();
}

void loop()
{
  if (Firebase.ready() && (millis() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0))
  {
    bool Sr = false;

    while (Serial.available())
    {
      //get sensor data from serial put in sensor_data
      values = Serial.readString();
      Sr = true;
    }

    delay(1000);

    if (Sr == true)
    {
      //values=sensor_data;

      //get comma indexes from values variable
      int fristCommaIndex = values.indexOf(',');
      int secondCommaIndex = values.indexOf(',', fristCommaIndex + 1);
      int thirdCommaIndex = values.indexOf(',', secondCommaIndex + 1);
      int fourthCommaIndex = values.indexOf(',', thirdCommaIndex + 1);
      int fifthCommaIndex = values.indexOf(',', fourthCommaIndex + 1);

      Firebase.RTDB.setString(&fbdo, "/test/real/temp", values.substring(0, fristCommaIndex));
      Firebase.RTDB.setString(&fbdo, "/test/real/hum", values.substring(fristCommaIndex + 1, secondCommaIndex));
      Firebase.RTDB.setString(&fbdo, "/test/real/pres", values.substring(secondCommaIndex + 1, thirdCommaIndex));
      Firebase.RTDB.setString(&fbdo, "/test/real/alt", values.substring(thirdCommaIndex + 1, fourthCommaIndex));
      Firebase.RTDB.setString(&fbdo, "/test/real/lum", values.substring(fourthCommaIndex + 1));
      Firebase.RTDB.setTimestamp(&fbdo, "/test/real/timestamp");

      delay(10);
      
      if (count == 31)
      {
        FirebaseJson json;
        json.add("temp", values.substring(0, fristCommaIndex));
        json.add("hum", values.substring(fristCommaIndex + 1, secondCommaIndex));
        json.add("pres", values.substring(secondCommaIndex + 1, thirdCommaIndex));
        json.add("alt", values.substring(thirdCommaIndex + 1, fourthCommaIndex));
        json.add("lum", values.substring(fourthCommaIndex + 1));

        // Space to provide EPOCH TIME
        
        json.add("timestamp", epochTime);

        Firebase.RTDB.pushJSON(&fbdo, "/test/data", &json);
        Firebase.RTDB.updateNode(&fbdo, String("/test/data/" + fbdo.pushName()).c_str(), &json);
        count = 0;
      }
      count++;
    }
  }

}

Solved by updating the firebase-esp-client library to its latest version: 4.0.0
Where there is a proper function to get timestamp ad a double variable and then pushing it as a value in the json.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.