So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Pink23t
#59130 Hi everyone,
I am most definitely a NOOB! I have not done any programming since my days with Amiga BBS systems before the http://WWW..

I came across this wonderful item ESP8266 about three weeks ago and it has renewed an old interest.
Thank you to all who share code as it is most certainly appreciated.

I have been playing around and reading / searching for info to help out. I noticed a lot of people trying to work out how to run a real time clock and wanted to share what I have found.
This is not a request for help, I am just sharing with those that may have already helped me out.

There is a RTC on the ESP8266 is a count of rtc clock cycles and there is a calculation to get time from it.
If using the Arduino IDE using millis() will work as normal. It does overflow.

BUT. Why worry. I found that if you include sntp.h and set up a NTP server for it to access, once you use sntp_init(); it will connect to the NTP server and get the correct time stamp. This continues to run in the background and by default will check the time server approx every 60 minutes.

Reading inside sntp.h and sntp.cpp it also resets the RTC counter each time and aligns it with NTP Stamp. If you call sntp_get_current_timestamp(); anytime in your program you will get the number of seconds since epoch 01-01-1970 Unix.
Each call to this function does not connect to the NTP server, I believe the macro uses the on board RTC that has been corrected each hour.

To get it in readable form use sntp_get_real_time(current_stamp).

With this, as long as you have a connection to a time server you do not need an external RTC like DS1307 or DS3231. I have mine connect to NTP at boot and it holds time no problems. I can see from my server logs it checks itself each hour.

If you use sntp_set_timezone(10); You can have it adjust to your local timezone. (10 being +10 hours)

I believe there is also a setting in the structure to allow for daylight savings, but as it is not used in my area I have not looked further.

I hope this info can help others,
Regards
Dave