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.
Best NTP-Time library for ESP32?
-
- Posts: 1
- Joined: Wed May 20, 2020 7:19 pm
Re: Best NTP-Time library for ESP32?
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.
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.
Re: Best NTP-Time library for ESP32?
I strongly disagree. Been there, done that...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
...
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.
-
- Posts: 828
- Joined: Mon Jul 22, 2019 3:20 pm
Re: Best NTP-Time library for ESP32?
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();
}
Re: Best NTP-Time library for ESP32?
Hi all, tried a slight modification of the above code for an overnight test and it gained about 3 minutes..
It looks to me that the time's NOT getting updated from the NTP response.
TIA
Dave
Code: Select all
/*
* dw from https://esp32.com/viewtopic.php?f=19&t=5188&p=60278&hilit=sntp#p60278
*/
#include <WiFi.h>
#include <esp_sleep.h>
#define NTP_SRV "au.pool.ntp.org"
#define NTP_CYCLE 300 // one hour between updates //dw 5 minutes
#define NTP_TIMEOUT 30 // 30 seconds
#define TIME_TO_SLEEP 120 // five minutes //dw 2 minutes
RTC_DATA_ATTR unsigned long lastUpdate = 0;
void printLocalTime()
{
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}
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("xxxxxxxxxxxxx","yuyyyyyyyyy");
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Unable to connect to WiFi");
return;
}
struct tm now;
configTime(10 * 3600, 3600, NTP_SRV); //UTC time //dw AEST
if (!getLocalTime(&now, NTP_TIMEOUT * 1000ULL)) {
Serial.println("Unable to sync with NTP server");
return;
}
lastUpdate = time(NULL);
Serial.println("NTP time updated");
printLocalTime();
}
}
void loop() {
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * 1000000ULL);
Serial.println("Going to sleep now");
Serial.flush();
esp_deep_sleep_start();
}
TIA
Dave
-
- Posts: 2
- Joined: Mon Apr 19, 2021 1:29 am
Re: Best NTP-Time library for ESP32?
for what its worth:
https://github.com/ropg/ezTime
they claim to be backwards compatible with TimeLib.h
this has not been my experience
https://github.com/ropg/ezTime
they claim to be backwards compatible with TimeLib.h
this has not been my experience
Who is online
Users browsing this forum: No registered users and 76 guests