Best NTP-Time library for ESP32?

Deouss
Posts: 408
Joined: Tue Mar 20, 2018 11:36 am

Re: Best NTP-Time library for ESP32?

Postby Deouss » Sun Apr 01, 2018 8:47 pm

I think you have to find other way to wake up from sleep. Processor must not restart or it is simply not a deep sleep but soft restart.
If they call such restart a deep sleep - that is just ridiculous. There must be other way
Here is more information about sleep modes
https://esp-idf.readthedocs.io/en/lates ... modes.html
You can override behavior of RTC power down and keep pins/memory domain configuration intact.

nicechocolate
Posts: 1
Joined: Wed May 20, 2020 7:19 pm

Re: Best NTP-Time library for ESP32?

Postby nicechocolate » Wed May 20, 2020 10:10 pm

I don't know how useful this will be on an old post, but to summarise what I have found out recently:

The best library, I find, is timelib.h, the latest version is (seems to be!) by Paul Stoffregen although the library is listed under the name of Michael Margolis in the Arduino libraries list. It will take time from NTP servers and update an RTC if you have one, or just sit there and tell the time. It seems accurate to within a few seconds a week, even without an RTC, for my digital clock sketch I chose to update my time every 10 days just to be sure it was close enough, but I'm still developing, so it hasn't had the usage to lose or gain any time.

This page https://github.com/PaulStoffregen/Time is the git location, it's all there. The crucial function call is configTime(), which gets the time from the NTP server and updates the internal clock (software or RTC), though much of the documentation does not mention it at all!

https://github.com/PaulStoffregen/Time/issues/101 makes comment on installation of the library and mentions general issues, but is otherwise not much of a guide.

This page index.php deals with SOME of the functions in timelib.h

This page lastminuteengineers.com describes how you use it with a sample code, which worked for me and is the basis of my digital clock. I think that code is one of the examples from the library, but I haven't time to look, now.

Other than this, I used the NTPtime library by German Martin, (a year ago), as I recall, the examples worked fine on an ESP8266 and can be hacked around for your use.

I hope this helps someone. Please excuse ropy formatting.

[EDIT] Maybe I need to explain my parameters for "best": I want to be able to understand the examples to the extent that I can include functions and code from the library examples in my programmes, modify it to suit my purposes and have it work without too many errors.
There may well be better ways to do things, but this is an answer to the question. All I need to do is write a programme that works so I can embed it in my device, give it to my son and get on with something else. At 63 with a dicky heart, I don't have the time nor the memory to learn everything and this is my 10th programming language (let alone the dialects/versions), since I started with FORTRAN in 1975 on punch-card machines. Retirement is also my third career...
If I use the NTP server I don't need the hardware clock, just the Wifi SSID and password.
Last edited by nicechocolate on Sun May 24, 2020 12:13 am, edited 1 time in total.

rin67630
Posts: 75
Joined: Sun Mar 11, 2018 5:13 pm

Re: Best NTP-Time library for ESP32?

Postby rin67630 » Fri May 22, 2020 8:18 am

nicechocolate wrote:
Wed May 20, 2020 10:10 pm
...
The best library, I find, is timelib.h, the latest version is (seems to be!) by Paul Stoffregen
...
I strongly disagree. Been there, done that...
The Arduino timelib.h has been written for a blank Arduino and bases on millis() only.

A kind of bare metal time library that is not even fully POSIX compatible.
An ESP32 has a built-in RTC and it would have been silly not to use it.
The dedicated time.h lib therefore is already loaded.

lbernstone
Posts: 52
Joined: Mon Jul 22, 2019 3:20 pm

Re: Best NTP-Time library for ESP32?

Postby lbernstone » Fri May 22, 2020 6:41 pm

Arduino-esp32 is built as an extension to ESP-IDF. So, the arduino functions call down to ESP-IDF, which provides a lot of standard libraries. In this case, the relevant tools are LWIP, which provides an SNTP app, and stdlib time.h. The posix time functions will work, along with the handy C++ wrappers from arduino-esp32.

Code: Select all

#include <WiFi.h>
#include <esp_sleep.h>

#define NTP_SRV "time-a-b.nist.gov"
#define NTP_CYCLE 3600  // one hour between updates
#define NTP_TIMEOUT 30  // 30 seconds
#define TIME_TO_SLEEP 300  // five minutes

RTC_DATA_ATTR unsigned long lastUpdate = 0;

void setup() {
  Serial.begin(115200);
  Serial.printf("Now: %lu\nLast Update: %lu\n", time(NULL), lastUpdate);
  if (lastUpdate > time(NULL)) lastUpdate = 0;
  if ((lastUpdate == 0) || ((time(NULL) - lastUpdate) > NTP_CYCLE)) { 

    WiFi.begin("ssid","passwd");
    if (WiFi.waitForConnectResult() != WL_CONNECTED) {
      Serial.println("Unable to connect to WiFi");
      return;
    }

    struct tm now;
    configTime(0, 0, NTP_SRV); //UTC time
    if (!getLocalTime(&now, NTP_TIMEOUT * 1000ULL)) {
      Serial.println("Unable to sync with NTP server");
      return;
    }
    lastUpdate = time(NULL);
    Serial.println("NTP time updated");
  }
}

void loop() {  
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * 1000000ULL);
  Serial.println("Going to sleep now");
  Serial.flush(); 
  esp_deep_sleep_start();
}

Who is online

Users browsing this forum: No registered users and 16 guests