# NTPClient always begins at 0

**URL:** <https://forum.arduino.cc/t/ntpclient-always-begins-at-0/1040127>\
**Category:** Programming\
**Created:** [October 9, 2022, 10:52pm UTC](https://forum.arduino.cc/t/ntpclient-always-begins-at-0/1040127 "2022-10-09T22:52:17Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![legolor](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/legolor/32/753740_2.png) [@legolor](https://forum.arduino.cc/u/legolor)\
**Post date:** [October 9, 2022, 10:52pm UTC](https://forum.arduino.cc/t/ntpclient-always-begins-at-0/1040127/1 "2022-10-09T22:52:17Z")

</div>

Hello, I am tying to get the current epoch time using an ESP8266 (NodeMCU ESP-12E).

I followed a bunch of basic tutorials using the NTPClient, WiFiUdp, and ESP8266WiFi libraries. My script using the function 'getEpochTime()' compiles but no matter what I do always begins at 0 when I first upload the program. The epoch time number then increases by one and prints every second.

I of course want 'getEpochTime()' to print the actual current epoch time. As far as I understand declaring the 'timeClient' and then using 'begin()' and 'update()' should work. I am pretty sure my wifi is ok because I can successfully ping '[pool.ntp.org](http://pool.ntp.org)' in cmd. I have also tried updating ArduinoIDE and downloading older versions of the libraries.

Maybe somebody can point me in the right direction of something else to try? Code is below.

```cpp
#include "NTPClient.h"
#include "ESP8266WiFi.h"
#include "WiFiUdp.h"

const char *ssid = "mySSID";
const char *password = "myPSW";

const long utcOffsetInSeconds = 0;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

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

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

WiFi.begin(ssid, password);

while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
if ( WiFi.status() == WL_CONNECTED )
{
  Serial.println("WIFI connected");
  Serial.println(WiFi.localIP());
}
timeClient.begin();
}

void loop() {
timeClient.update();

Serial.println(timeClient.getEpochTime());

delay(1000);
}

```

---

<div class="post-metadata">

**Author:** ![Geek\_Emeritus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/geek_emeritus/32/238995_2.png) [@Geek\_Emeritus](https://forum.arduino.cc/u/Geek_Emeritus)\
**Post date:** [October 9, 2022, 11:26pm UTC](https://forum.arduino.cc/t/ntpclient-always-begins-at-0/1040127/2 "2022-10-09T23:26:33Z")

</div>

do you have an IP scanning program? can you see the ESP8266s IP address when you scan, after it boots?

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [October 9, 2022, 11:41pm UTC](https://forum.arduino.cc/t/ntpclient-always-begins-at-0/1040127/3 "2022-10-09T23:41:05Z")

</div>

Hi,  
I compiled and loaded your code on an ESP8266-12E without changing anything.  
See the result. It's normal.

PS: Then I added this line:  
Serial.println(timeClient.getFormattedTime());  
And the result is below.

```cpp
192.168.1.12
1665358690
1665358691
1665358692
1665358693
1665358694
1665358695
1665358696
1665358697
1665358698
1665358699
1665358700
1665358701
1665358702
1665358703
1665358704
1665358705
1665358706
1665358707

After new line:

192.168.1.12
1665359172
23:46:12
1665359173
23:46:13
1665359174
23:46:14
1665359175
23:46:15
1665359176
23:46:16
1665359177
23:46:17
1665359178
23:46:18
1665359179
23:46:19

```

---

<div class="post-metadata">

**Author:** ![legolor](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/legolor/32/753740_2.png) [@legolor](https://forum.arduino.cc/u/legolor)\
**Post date:** [October 10, 2022, 6:38am UTC](https://forum.arduino.cc/t/ntpclient-always-begins-at-0/1040127/4 "2022-10-10T06:38:47Z")

</div>

> [@Geek\_Emeritus](#):
>
> do you have an IP scanning program?

I'm not sure exactly what you mean.

I have the sketch print the IP after it connects to wifi in to the serial monitor via  
`Serial.println(WiFi.localIP());`  
It does successfully connect to WIFI and get an IP address.

---

<div class="post-metadata">

**Author:** ![legolor](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/legolor/32/753740_2.png) [@legolor](https://forum.arduino.cc/u/legolor)\
**Post date:** [October 10, 2022, 6:51am UTC](https://forum.arduino.cc/t/ntpclient-always-begins-at-0/1040127/5 "2022-10-10T06:51:26Z")

</div>

> [@ruilviana](#):
>
> I compiled and loaded your code on an ESP8266-12E without changing anything.  
> See the result. It's normal.

Thank you so much for trying it out! I guess at least I know now that it's not the codes fault.  
I am still having the same problem (with the other new line too).

Would you mind telling me the exact name/version of the libraries you have and where you downloaded them from? (The names of the folders of libraries for managing wifi and NTPClient in Documents-\>Arduino-\>libraries)(If you got them from the web or GitHub or the Arduino library manager)

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [October 10, 2022, 8:29am UTC](https://forum.arduino.cc/t/ntpclient-always-begins-at-0/1040127/6 "2022-10-10T08:29:58Z")

</div>

I propose to not use any 3rd party NTP lib on the ESP. NTP is part of the core functionality of the ESP Core.

See for example:

[NTP for the ESP8266 including day light saving Time (DST) without 3rd party library (rothschopf.net)](http://werner.rothschopf.net/202011_arduino_esp8266_ntp_en.htm)

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [October 10, 2022, 11:18am UTC](https://forum.arduino.cc/t/ntpclient-always-begins-at-0/1040127/7 "2022-10-10T11:18:58Z")

</div>

Hi,  
Arduino IDE = 1.8.9;  
board ESP8266 Community version = 2.6.3;  
ESP8266WiFi.h -- \> installed when installing the ES8266 board;  
WiFiUdp.h -- \> -- \> installed when installing the ES8266 board;

NTPCClient.h version=3.1.0  
from url=[GitHub - arduino-libraries/NTPClient: Connect to a NTP server](https://github.com/arduino-libraries/NTPClient)

---

<div class="post-metadata">

**Author:** ![legolor](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/legolor/32/753740_2.png) [@legolor](https://forum.arduino.cc/u/legolor)\
**Post date:** [October 10, 2022, 11:02pm UTC](https://forum.arduino.cc/t/ntpclient-always-begins-at-0/1040127/8 "2022-10-10T23:02:14Z")

</div>

> [@noiasca](#):
>
> I propose to not use any 3rd party NTP lib on the ESP. NTP is part of the core functionality of the ESP Core.

That's interesting, I didn't know that.

I tried the example from the website you attached and the same thing happened where it starts at 1970 and 0 seconds and then counts up. Without changing anything except entering the wifi this is my result:

```arduino
..............
WiFi connected
year:1970	month:1	day:1	hour:1	min:0	sec:3	wday4	standard
year:1970	month:1	day:1	hour:1	min:0	sec:4	wday4	standard
year:1970	month:1	day:1	hour:1	min:0	sec:5	wday4	standard
year:1970	month:1	day:1	hour:1	min:0	sec:6	wday4	standard
year:1970	month:1	day:1	hour:1	min:0	sec:7	wday4	standard

```

In the sketch it has `#include <time.h>`, in my libraries I only see a "TimeLib.h" file but if I try to include that instead it does not compile. Is it possible that my nonexistent time.h file could be the culprit?

---

<div class="post-metadata">

**Author:** ![legolor](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/legolor/32/753740_2.png) [@legolor](https://forum.arduino.cc/u/legolor)\
**Post date:** [October 10, 2022, 11:42pm UTC](https://forum.arduino.cc/t/ntpclient-always-begins-at-0/1040127/9 "2022-10-10T23:42:25Z")

</div>

Ok thanks, I tried it on an Arduino IDE 1.8 version and used the same github link for the library but still no success ☹

---

<div class="post-metadata">

**Author:** ![ZX80](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/zx80/32/643687_2.png) [@ZX80](https://forum.arduino.cc/u/ZX80)\
**Post date:** [October 11, 2022, 2:15am UTC](https://forum.arduino.cc/t/ntpclient-always-begins-at-0/1040127/10 "2022-10-11T02:15:01Z")

</div>

Have you tried with **#include \<time.h\>**?

---

<div class="post-metadata">

**Author:** ![legolor](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/legolor/32/753740_2.png) [@legolor](https://forum.arduino.cc/u/legolor)\
**Post date:** [October 11, 2022, 2:21am UTC](https://forum.arduino.cc/t/ntpclient-always-begins-at-0/1040127/11 "2022-10-11T02:21:34Z")

</div>

Yes, the program runs with `#include <time.h>` (with my issue). However I don't see a time.h file in my library folder. I do have a "TimeLib.h" file but if I include that instead of "time.h" the program does not compile.

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [October 11, 2022, 6:03am UTC](https://forum.arduino.cc/t/ntpclient-always-begins-at-0/1040127/12 "2022-10-11T06:03:14Z")

</div>

> [@legolor](#):
>
> I tried the example from the website you attached and the same thing happened where it starts at 1970 and 0 seconds and then counts up.

a) have you seen the hint:

_If you upload the sketch to your ESP you will notice that the timestamp will start with 01/01/1970. By default, the first NTP request begins after 60 seconds. That can be changed._

Have you waited at least one minute?

b) the sketch needs \<time.h\> - if the sketch doesn't compile with \<time.h\> there is something wrong with your installation. Which core are you using?

---

<div class="post-metadata">

**Author:** ![legolor](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/legolor/32/753740_2.png) [@legolor](https://forum.arduino.cc/u/legolor)\
**Post date:** [October 11, 2022, 10:51pm UTC](https://forum.arduino.cc/t/ntpclient-always-begins-at-0/1040127/13 "2022-10-11T22:51:42Z")

</div>

Thank you for the details  
A) Even after waiting a minute it just continues counting. (I have left it on for much longer)

B) I was unclear and I think wrong before. It does run with \<time.h\> and I found time.h in my AppData folder in I guess the "core libraries".  
I'm not sure exactly what you mean by "Which core are you using?". I am using IDE 2.0.0 but have also tried it on 1.8.15. The file path to where I found the time.h directiry was `...\AppData\Local\Arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\3.0.3-gcc10.3-9bcba0b\xtensa-lx106-elf\include\time.h`. The board I am compiling for is the "NodeMCU 1.0 (ESP-12E Module)".

Thanks!

---

<div class="post-metadata">

**Author:** ![benit19](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@benit19](https://forum.arduino.cc/u/benit19)\
**Post date:** [January 9, 2023, 10:58am UTC](https://forum.arduino.cc/t/ntpclient-always-begins-at-0/1040127/14 "2023-01-09T10:58:16Z")

</div>

Hi Legolor,

I have the exact same issue when running the code at the university. However, when I use my own phone sharing internet connection, I do not have the issue.  
Is it your case as well (is it working when using your phone's wifi)?

---

<div class="post-metadata">

**Author:** ![legolor](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/legolor/32/753740_2.png) [@legolor](https://forum.arduino.cc/u/legolor)\
**Post date:** [April 25, 2023, 11:49pm UTC](https://forum.arduino.cc/t/ntpclient-always-begins-at-0/1040127/15 "2023-04-25T23:49:09Z")

</div>

benit19,

Yes, you are right. I tried it when I went home a few months ago and it worked on normal wifi!

I also got it to work at university by registering it for the university iot network (instead of the guest network). Maybe something to look into if anyone else has this issue and their university has a special network that has more capabilities but can still be signed into with only ssid and psw (no extra verification as was on the main network).

Still looking for a better workaround though 🙂

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 22, 2023, 11:49pm UTC](https://forum.arduino.cc/t/ntpclient-always-begins-at-0/1040127/16 "2023-10-22T23:49:57Z")

</div>

Closed on October 22, 2023, 11:49pm UTC

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