Comments on: Get Epoch/Unix Time with the ESP32 (Arduino) https://randomnerdtutorials.com/epoch-unix-time-esp32-arduino/ Learn ESP8266, ESP32, Arduino, and Raspberry Pi Wed, 04 Sep 2024 01:19:30 +0000 hourly 1 https://wordpress.org/?v=6.8.2 By: Henry D https://randomnerdtutorials.com/epoch-unix-time-esp32-arduino/#comment-953723 Wed, 04 Sep 2024 01:19:30 +0000 https://randomnerdtutorials.com/?p=101595#comment-953723 Sara and Rui,
I am trying to work with ESP8266 / ESP32 but am looking for epoch time in miliseconds. The return I am getting on a different system is a 13 character number. Everything I find on androids, ESP8266 and ESP32 is only 10 characters? Is there different libraries to call?

]]>
By: Henry D https://randomnerdtutorials.com/epoch-unix-time-esp32-arduino/#comment-953722 Wed, 04 Sep 2024 01:18:52 +0000 https://randomnerdtutorials.com/?p=101595#comment-953722 Sara and Rui,
I am trying to work with ESP8266 / ESP32 but am looking for epoch time in miliseconds. The return I am getting on a different system is a 13 character number. Everything I find on androids, ESP8266 and ESP32 is only 10 characters?

]]>
By: Sara Santos https://randomnerdtutorials.com/epoch-unix-time-esp32-arduino/#comment-838896 Mon, 05 Jun 2023 17:43:10 +0000 https://randomnerdtutorials.com/?p=101595#comment-838896 In reply to Chip F..

Hi.
I think that is a known issue.
Take a look at this discussion: https://forum.arduino.cc/t/esp32-cannot-connect-to-ntp-server-with-fixed-ip/591181
I hope this helps.
Regards,
Sara

]]>
By: Chip F. https://randomnerdtutorials.com/epoch-unix-time-esp32-arduino/#comment-838763 Sun, 04 Jun 2023 23:21:36 +0000 https://randomnerdtutorials.com/?p=101595#comment-838763 Like that guy in the woods somewhere, I’d like to start by saying thank you, Rui and Sara, for your wonderful tutorials.

This example worked for me until I tried setting a static IP address on the ESP32 as in https://randomnerdtutorials.com/esp32-useful-wi-fi-functions-arduino/#7 . After I added that code to setup(), I get the “Failed to obtain time” error.

The code I added above setup() was:
IPAddress local_IP(192, 168, 1, 150);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

The code I added in setup(), before configTime() was:
Serial.println(“Initializing Wi-Fi IP config…”);
if (!WiFi.config(local_IP, gateway, subnet)) {
Serial.println(“STA Failed to configure”);
}

This code seems to set the IP address as desired, but it breaks something, preventing the time software from working.

The board I’m using is an Adafruit Feather ESP32 V2. The IDE is Arduino IDE Version 2.1.0. The espressif/arduino-esp32 version is 2.0.9.

]]>
By: jj https://randomnerdtutorials.com/epoch-unix-time-esp32-arduino/#comment-830916 Wed, 19 Apr 2023 09:36:44 +0000 https://randomnerdtutorials.com/?p=101595#comment-830916 Thanks for the nice sample
Be aware that in case you set date’s > 20 years from now we error out with: Failed to obtain time 1
try this one……. setTime(2099,3,28,1,59,50,1);

void setTime(int yr, int month, int mday, int hr, int minute, int sec, int isDst){
//struct tm tm;
timeinfo.tm_year = yr – 1900; // Set date
timeinfo.tm_mon = month-1;
timeinfo.tm_mday = mday;
timeinfo.tm_hour = hr; // Set time
timeinfo.tm_min = minute;
timeinfo.tm_sec = sec;
timeinfo.tm_isdst = isDst; // 1 or 0
time_t t = mktime(&timeinfo);
Serial.printf(“Setting time: %s”, asctime(&timeinfo));
struct timeval AAAAAAAAAnow = { .tv_sec = t };
settimeofday(&AAAAAAAAAnow, NULL);
}

void printLocalTime()
{
if(!getLocalTime(&timeinfo)){
Serial.println(“Failed to obtain time 1”);
return;
}
/*
timeinfo.x
tm_sec int seconds after the minute 0-61*
tm_min int minutes after the hour 0-59
tm_hour int hours since midnight 0-23
tm_mday int day of the month 1-31
tm_mon int months since January 0-11
tm_year int years since 1900
tm_wday int days since Sunday 0-6
tm_yday int days since January 1 0-365
tm_isdst int Daylight Saving Time flag
*/

error
. CONNECTED
succeeded to connect to NTPServer connection and get the present time
Seconds: 40
NTP read: 2023 Apr 19 11:35:40
Epoch Time: 1681904140
Date1: 2023-04-19 11:35:40

lets change the time to the future
Setting time: Fri Mar 28 01:59:50 2053
[D][WiFiGeneric.cpp:374] _eventCallback(): Event: 3 – STA_STOP
[D][WiFiGeneric.cpp:374] _eventCallback(): Event: 3 – STA_STOP
Failed to obtain time 1
Failed to obtain time 1
Failed to obtain time 1
Failed to obtain time 1
Failed to obtain time 1

]]>
By: Gerallt Griffiths https://randomnerdtutorials.com/epoch-unix-time-esp32-arduino/#comment-650175 Thu, 22 Jul 2021 20:09:15 +0000 https://randomnerdtutorials.com/?p=101595#comment-650175 In reply to Thomas Denner.

I believe that it is the configTime() call that performs the actual ntp query, so we do it once only — in the setup() function. Thereafter, we free-wheel using the time facilities of the ESP32. If accurate time is required, it would make sense to call configTime() periodically, at an interval that is appropriate to compensate for the drift of your ESP32 clock.
Regards,
Gerallt.

]]>
By: Sara Santos https://randomnerdtutorials.com/epoch-unix-time-esp32-arduino/#comment-623661 Mon, 07 Jun 2021 09:23:47 +0000 https://randomnerdtutorials.com/?p=101595#comment-623661 In reply to mal oldis.

Hi.
That’s weird.
I’ve just testes the code now and it is working fine for me.
What’s the ESP32 add-on version that you’re using? You can check it by going to Tools > Board > Boards Manager and search for ESP32.
Regards,
Sara

]]>
By: mal oldis https://randomnerdtutorials.com/epoch-unix-time-esp32-arduino/#comment-623445 Mon, 07 Jun 2021 02:21:14 +0000 https://randomnerdtutorials.com/?p=101595#comment-623445 I have tried this code a number of times but in each case I get the “Failed to obtain time” message.
This code was embedded into the Telegram Bot software which works brilliantly. So the internet access is working OK. Even the stand-alone code fails to work.
Any suggestions. ?
Thanks in advance.

]]>
By: Thomas Denner https://randomnerdtutorials.com/epoch-unix-time-esp32-arduino/#comment-575192 Sat, 20 Mar 2021 10:48:07 +0000 https://randomnerdtutorials.com/?p=101595#comment-575192 Great site for newbies like me – than you for all the effort.

I have a question regarding NTP requests: does each call of the function cause network traffic or ist there some „intelligence“ in the system that the internal clock is used for frequent requests and the NTP servers are only called once in a while to synchronise?

I need the time very frequently but want to avoid permanent network traffic.

Thanks for consideration,

Thomas

]]>
By: Joseph https://randomnerdtutorials.com/epoch-unix-time-esp32-arduino/#comment-573843 Wed, 17 Mar 2021 17:26:37 +0000 https://randomnerdtutorials.com/?p=101595#comment-573843 Hi, how to change it to milliseconds? I try multiply it by 1000 and it shows wrong time. I do like this, Serial.println(epochTime * 1000);

]]>