Wrong date and time using arduino esp32

I am using wemos d1 mini with sd card adapter and tinygps++.

I am using TimeLib.h to adjust time and capture the gps coordinates using TinyGPS++.

The problem is that the csv file saved is dated as 12/31/2097 and the date/hour saved in the time column is 2000-0-0 0:0:0.

How can I use the current time and date?

#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <ESP8266WiFi.h>
#include <SD.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#include <Time.h>
#include <TimeLib.h>


#define UTC_offset +3  // european
#define SD_CS      D8

String logFileName = "";


#define LOG_RATE 500
char currentTime[5];

.
.
.

sprintf_P(currentTime, PSTR("%02d:%02d"),hour(),minute());
.
.
.
        logFile.print(getEncryption(i,"")); logFile.print(',');
        logFile.print(tinyGPS.date.year());   logFile.print('-');
        logFile.print(tinyGPS.date.month());  logFile.print('-');
        logFile.print(tinyGPS.date.day());    logFile.print(' ');
        logFile.print(tinyGPS.time.hour());   logFile.print(':');
        logFile.print(tinyGPS.time.minute()); logFile.print(':');
        logFile.print(tinyGPS.time.second()); logFile.print(',');
.
.
  setTime(tinyGPS.time.hour(), tinyGPS.time.minute(), tinyGPS.time.second(), tinyGPS.date.day(), tinyGPS.date.month(), tinyGPS.date.year());
  adjustTime(UTC_offset * SECS_PER_HOUR);  

Any ideas why this error and how to fix it?

If I am using

setTime(hour(), minute(), second(), day(), month(), year());
      adjustTime(UTC_offset * SECS_PER_HOUR); 

The output is the same.

I have had good success with time things and the ESP32 using this library: GitHub - fbiego/ESP32Time: An Arduino library for setting and retrieving internal RTC time on ESP32 boards

1 Like

Is the problem just related to the .csv file label, or with the time data that is inside the .csv file?

@Idahowalker , I guess Wemos d1 mini is based on ESP8266. It doesn´t have an internal RTC though. Looks like @pythoneatssquirrel idea is to retrieve the time data from the GPS/NMEA. If that´s the case, it will be worthless trying to set up the time manually.

I need the current date and time regardless if it is from gps or not. It's a personal project regarding data gathering and I would need to use current date and time

I am using the date and time services provided by time.h and have no problem whatsoever, including allowing it to handle time zone and daylight time offset. There is no good reason to use the DateTime library with this API. But I let it use NTP for time setting. I would guess, there must be a relatively simple way to set the time using time.h.

I think your problem with setTime() is that you are passing the parameters incorrectly. Have you consulted the API reference?

Also, setting the time to UTC and then adjusting it to the local time zone is, to be blunt, a travesty. The local time is available with localTime().

Look at the ESP "SimpleTime" sketch for help on that. Setting the time from GPS will be easier once you get that straightened out.

This

#include <Time.h>
#include <TimeLib.h>

should never happen. The libraries are made for completely different purposes.

If it´s that simple, you should consider using a RTC+SD module like this one:

https://robotdyn.com/wifi-d1-mini-data-logger-shield-rtc-ds1307-with-battery-microsd.html

and get the time from the RTC.

You should log in UTC time. It's unambiguous.

error: 'getLocalTime' was not declared in this scope
94 | getLocalTime(timeinfo);
| ^~~~~~~~~~~~
exit status 1
Error compiling for board LOLIN(WEMOS) D1 R2 & mini.

Esp32-Time gives error when I try to verify the example for d1 mini

Does your d1 mini have an RTC?

No, just a SD shield :slight_smile: facing this fact, I only can set the time and date manually each time I turn it on, or use wi-fi auth to connect to the server that provides date and time, right?

If you do not have a RTC then and you want the correct date and time after a reboot then you will need someway to get date and time such as manually entering it or getting the time from some other source. This has been explained to you several times already. In what way is this concept difficult to grasp?

understood :slight_smile: thank you!

one more question.

I did setTime(11, 45, 30, 28, 10, 2021); and it worked for the date/time column but the csv file is still dated 12/31/2097. How can I set the time for that also?

Edit the SD library or one of its dependencies to make a function that allows you to manually set the date and time at file creation.

1 Like