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' 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.
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.
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)
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;
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:
..............
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?
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.
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?
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)".
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)?
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).