Best NTP-Time library for ESP32?

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

Best NTP-Time library for ESP32?

Postby rin67630 » Thu Mar 29, 2018 9:26 am

Hi, I am looking forward to using a specific time library that uses the built-in RTC features of the ESP32.

That one seems to use them:
https://github.com/espressif/arduino-es ... leTime.ino

The code is hovewer scarcely documented, it's using the time.h lib which refers to timeLib.h...
That is however a developement of year 2011, can it be specifically intended for the ESP32, that came out later?

I am currently can't figure out how to use that undocumented example to use it to run tasks on specific timings. Polling the getLocalTime routine can't be the right way to use it, is it?

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

Re: Best NTP-Time library for ESP32?

Postby Deouss » Thu Mar 29, 2018 2:56 pm

I am looking for same thing. NTP seems easy - here RFCs https://tools.ietf.org/html/rfc1305 (v3) or rfc1059 (v1) or rfc7822 (v4)
however needs network low level programming.
Simple example in c# is here https://stackoverflow.com/questions/119 ... er-using-c
They query ntp server with TCP and get 48 bytes back with all time info

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

Re: Best NTP-Time library for ESP32?

Postby Deouss » Thu Mar 29, 2018 3:46 pm

I additionally found this library

https://github.com/arduino-libraries/NTPClient

and other example

https://github.com/gmag11/NtpClient/blo ... tESP32.ino

Looks like NTP is queried with UDP which is faster

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

Re: Best NTP-Time library for ESP32?

Postby rin67630 » Fri Mar 30, 2018 5:14 pm

Deouss wrote:I am looking for same thing. NTP seems easy - here RFCs https://tools.ietf.org/html/rfc1305 (v3) or rfc1059 (v1) or rfc7822 (v4)
however needs network low level programming.
Simple example in c# is here https://stackoverflow.com/questions/119 ... er-using-c
They query ntp server with TCP and get 48 bytes back with all time info
My problem is not only how to querry NTP with the ESP32, but also how to use its built-in RTC.
You are looking for generic ways to do it from scratch. That is inefficient.
The example given in https://github.com/espressif/arduino-es ... leTime.ino shows how to synchronise with NTP with a simple routine:

Code: Select all

configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
That's it!
The ESP32 seems to have low-level functions to gather this in a much more efficient way than the c++ code used to with the ESP8266 or Arduinos.
in Documents\Arduino\hardware\espressif\esp32\cores\esp32 you will find a routine esp32-hal-time.c
This seem to be the way to search for.

You will find elsewhere plenty of examples of legacy code, that may run on the ESP32, but don't use its capabilities.

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

Re: Best NTP-Time library for ESP32?

Postby Deouss » Fri Mar 30, 2018 6:56 pm

So - does it work? You are saying ntp sync is built into ESP itself?

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

Re: Best NTP-Time library for ESP32?

Postby rin67630 » Fri Mar 30, 2018 9:39 pm

Deouss wrote:So - does it work? You are saying ntp sync is built into ESP itself?
I have extended the SimpleTime.ino example with time variables to be used by a program:

Code: Select all

#include <WiFi.h>
#include "time.h"

const char* ssid       = "SSID";
const char* password   = "PASS";

const char* ntpServer = "de.pool.ntp.org";
const long  gmtOffset_sec = 3600;
const int   daylightOffset_sec = 3600;
int second;
int minute;
int hour;
int day;
int month;
int year;
int weekday;
long current;
struct tm timeinfo;
  /*
struct tm
{
int    tm_sec;   //   Seconds [0,60]. 
int    tm_min;   //   Minutes [0,59]. 
int    tm_hour;  //   Hour [0,23]. 
int    tm_mday;  //   Day of month [1,31]. 
int    tm_mon;   //   Month of year [0,11]. 
int    tm_year;  //   Years since 1900. 
int    tm_wday;  //   Day of week [0,6] (Sunday =0). 
int    tm_yday;  //   Day of year [0,365]. 
int    tm_isdst; //   Daylight Savings flag. 
}
 */  

void printLocalTime()
{

  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  Serial.println(&timeinfo, "%A, %d %B %Y %H:%M:%S");
}

void setup()
{
  Serial.begin(115200);
  
  //connect to WiFi
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }
  Serial.println(" CONNECTED");
  
  //init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  printLocalTime();

  //disconnect WiFi as it's no longer needed
  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);
}

void loop()
{
  delay(1000);
  
  printLocalTime();
  second = timeinfo.tm_sec;
  minute = timeinfo.tm_min;
  hour = timeinfo.tm_hour;
  day = timeinfo.tm_mday;
  month = timeinfo.tm_mon + 1;
  year = timeinfo.tm_year + 1900;
  weekday = timeinfo.tm_wday +1;
  Serial.print("Time from variables:  ");
  Serial.print(day);
  Serial.print(".");
  Serial.print(month);
  Serial.print(".");
  Serial.print(year);
  Serial.print(" --- ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);
}
Now I expect to be able to go to deepsleep and wake up without recalling the SNTP service...

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

Re: Best NTP-Time library for ESP32?

Postby Deouss » Sun Apr 01, 2018 2:31 pm

I just looked at \arduino-esp32-master\tools\sdk\include\lwip\apps\sntp
Seems like there is a dedicated code for sntp and configTime so no need to recreate ntp I guess.
If you drill down into esp-hal you can merge more features
Let us know how it works

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

Re: Best NTP-Time library for ESP32?

Postby rin67630 » Sun Apr 01, 2018 7:32 pm

Deouss wrote: Seems like there is a dedicated code for sntp and configTime so no need to recreate ntp I guess.
Sure, the SimpleTime example demonstrates exactly that pretty well.
I just added for didactic purposes the way to get individual time values.

Now the challange is to replace the delay(); by a deep sleep and to wake up without resynchronizing via NTP.

Unfortunately makers are left alone here, I did not find any usable example of waking up from deep sleep keeping time.

I hope it's possible.

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

Re: Best NTP-Time library for ESP32?

Postby Deouss » Sun Apr 01, 2018 7:52 pm

I would study datasheet. I think I saw some information pertaining to deep sleep that only certain level of interrupt can wake the device up. Or maybe it is some kind of external event signal - hopefully can be periodic. I know hardware timers should be able to completely wake system up

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

Re: Best NTP-Time library for ESP32?

Postby rin67630 » Sun Apr 01, 2018 8:02 pm

Deouss wrote:I would study datasheet. I think I saw some information pertaining to deep sleep that only certain level of interrupt can wake the device up. Or maybe it is some kind of external event signal - hopefully can be periodic. I know hardware timers should be able to completely wake system up
You can go to deep sleep for x microseconds and wake up automagically after that time.
The only unsolved problem yet is that the processor restarts from scratch and I don't know how to sense that the RTC is up-to date in order to avoid a power-costly new SNTP sync.

Who is online

Users browsing this forum: No registered users and 20 guests