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:
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.