NTPClient.h library returns wrong times when I capture time in Unix format

Hi all, I am working on a project to store environmental variables using the ESP32 board and the NTPClient.h library.

The sensor I am using is the BME688 and I am storing the data on an SD card using the OpenLog DEV-13712.

The problem I am having is that I am trying to capture the time in unix format but it goes backwards from 1721873246 to 1121873252.

Attached is a screenshot:

image

This is the code I have been working with:

#include <Wire.h>
#include <ESP32Time.h>
#include <WiFi.h>
#include "time.h"
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
#include "SparkFun_Qwiic_OpenLog_Arduino_Library.h"
#include <NTPClient.h>
#include <WiFiUdp.h>

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME680 bme;
OpenLog myLog;

double BME688[6];

char buff[300];

const char* ssid = "DECKARD 1389";
const char* password = "67U9{a51";


WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");


const char* ntpServer = "pool.ntp.org";


void initWiFi() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());
}


void setup() {
  Serial.begin(115200);
  Wire.begin();

  myLog.begin();

  if (!bme.begin()) {
    Serial.println("Could not find a valid BME680 sensor, check wiring!");
    while (1);
  }

  // Set up oversampling and filter initialization
  bme.setTemperatureOversampling(BME680_OS_8X);
  bme.setHumidityOversampling(BME680_OS_2X);
  bme.setPressureOversampling(BME680_OS_4X);
  bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
  bme.setGasHeater(320, 150); // 320*C for 150 ms

  initWiFi();
  
  // Inicia el cliente NTP
  timeClient.begin();
    
}

void loop() {

  timeClient.update();

    // =======================================================
    // BME688
    // =======================================================
    myLog.append("BME688.txt");
    if (bme.performReading()) BME688[0] = bme.temperature;
    if (bme.performReading()) BME688[1] = bme.pressure / 100.0;
    if (bme.performReading()) BME688[2] = bme.humidity;
    if (bme.performReading()) BME688[3] = bme.gas_resistance / 1000.0;
    if (bme.performReading()) BME688[4] = bme.readAltitude(SEALEVELPRESSURE_HPA);
    
    if (bme.performReading()) {
    
        sprintf(buff, "%ld,%s,%4.2f,%s,%4.2f,%s,%4.2f,%s,%6.2f,%s,%6.2f\n",
                timeClient.getEpochTime(),
                "Temperature_c", BME688[0],
                "Pressure_hpa", BME688[1],
                "Humidity _%", BME688[2],
                "VOC_KOhms", BME688[3],
                "Altitude", BME688[4]);
                
        Serial.print(buff);
        myLog.print(buff);
        
      }
     
    delay(500);

}

I honestly don't know where the error is coming from, whether from the NTPClient.h library or from the SD card storage.

I hope you can give me some guidance on how to fix this problem, I have also noticed that the function timeClient.update(); always returns false.

which library ? do you have a link?

if getEpochTime() returns an unsigned long %ld is not the right format, it should be %lu

(I've some demo code for using NTP on an ESP32 in this post in French )

1 Like

thank you for your answer, this is the link: GitHub - arduino-libraries/NTPClient: Connect to a NTP server

don't use the NtpClient library on the esp32.
use

  configTime(gmtOffset_sec, daylightOffset_sec, "pool.ntp.org");
  time_t now = time(nullptr);
2 Likes

+1
No library needed. It's now built into the ESP core.
I use this line, which does local time with daylight savings.

configTzTime("NZST-12NZDT,M9.5.0,M4.1.0/3", "nz.pool.ntp.org");

Location strings can be found here.

Leo..

@elioth-make-1004

as others have already pointed out, don't use an external library for NTP on the ESP.
The ESP32 time.h comes with everything you need for a reliable NTP.

Here I have an stripped down version from the IDE example:

https://werner.rothschopf.net/microcontroller/202103_arduino_esp32_ntp_en.htm

Thank you all for your contributions, they were very helpful.