Skip to content

Commit b3b3403

Browse files
authored
NTP Examples: revert obsolete comment and updated Time example (#6073)
* Revert "Examples update, add a note for configTime() that only one ntp server is supported by lwip", fixed in espressif/esp32-arduino-lib-builder#51 This reverts commit 6b10209. * SimpleTime: add NTPoDHCP option and TimeZone env variable
1 parent c014eaf commit b3b3403

File tree

8 files changed

+43
-40
lines changed

8 files changed

+43
-40
lines changed

cores/esp32/esp32-hal-time.c

-4
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ static void setTimeZone(long offset, int daylight)
4444
/*
4545
* configTime
4646
* Source: https://github.com/esp8266/Arduino/blob/master/cores/esp8266/time.c
47-
* Note: Bundled Arduino lwip supports only ONE ntp server, 2nd and 3rd options are silently ignored
48-
* see CONFIG_LWIP_DHCP_MAX_NTP_SERVERS define in ./tools/sdk/esp32/sdkconfig
4947
* */
5048
void configTime(long gmtOffset_sec, int daylightOffset_sec, const char* server1, const char* server2, const char* server3)
5149
{
@@ -65,8 +63,6 @@ void configTime(long gmtOffset_sec, int daylightOffset_sec, const char* server1,
6563
/*
6664
* configTzTime
6765
* sntp setup using TZ environment variable
68-
* Note: Bundled Arduino lwip supports only ONE ntp server, 2nd and 3rd options are silently ignored
69-
* see CONFIG_LWIP_DHCP_MAX_NTP_SERVERS define in ./tools/sdk/esp32/sdkconfig
7066
* */
7167
void configTzTime(const char* tz, const char* server1, const char* server2, const char* server3)
7268
{
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,65 @@
11
#include <WiFi.h>
22
#include "time.h"
3+
#include "sntp.h"
34

45
const char* ssid = "YOUR_SSID";
56
const char* password = "YOUR_PASS";
67

7-
const char* ntpServer = "pool.ntp.org";
8+
const char* ntpServer1 = "pool.ntp.org";
9+
const char* ntpServer2 = "time.nist.gov";
810
const long gmtOffset_sec = 3600;
911
const int daylightOffset_sec = 3600;
1012

13+
const char* time_zone = "CET-1CEST,M3.5.0,M10.5.0/3"; // TimeZone rule for Europe/Rome including daylight adjustment rules (optional)
14+
1115
void printLocalTime()
1216
{
1317
struct tm timeinfo;
1418
if(!getLocalTime(&timeinfo)){
15-
Serial.println("Failed to obtain time");
19+
Serial.println("No time available (yet)");
1620
return;
1721
}
1822
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
1923
}
2024

25+
// Callback function (get's called when time adjusts via NTP)
26+
void timeavailable(struct timeval *t)
27+
{
28+
Serial.println("Got time adjustment from NTP!");
29+
printLocalTime();
30+
}
31+
2132
void setup()
2233
{
2334
Serial.begin(115200);
24-
35+
36+
// set notification call-back function
37+
sntp_set_time_sync_notification_cb( timeavailable );
38+
39+
/**
40+
* NTP server address could be aquired via DHCP,
41+
*
42+
* NOTE: This call should be made BEFORE esp32 aquires IP address via DHCP,
43+
* otherwise SNTP option 42 would be rejected by default.
44+
* NOTE: configTime() function call if made AFTER DHCP-client run
45+
* will OVERRIDE aquired NTP server address
46+
*/
47+
sntp_servermode_dhcp(1); // (optional)
48+
49+
/**
50+
* This will set configured ntp servers and constant TimeZone/daylightOffset
51+
* should be OK if your time zone does not need to adjust daylightOffset twice a year,
52+
* in such a case time adjustment won't be handled automagicaly.
53+
*/
54+
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2);
55+
56+
/**
57+
* A more convenient approach to handle TimeZones with daylightOffset
58+
* would be to specify a environmnet variable with TimeZone definition including daylight adjustmnet rules.
59+
* A list of rules for your zone could be obtained from https://github.com/esp8266/Arduino/blob/master/cores/esp8266/TZ.h
60+
*/
61+
//configTzTime(time_zone, ntpServer1, ntpServer2);
62+
2563
//connect to WiFi
2664
Serial.printf("Connecting to %s ", ssid);
2765
WiFi.begin(ssid, password);
@@ -30,18 +68,11 @@ void setup()
3068
Serial.print(".");
3169
}
3270
Serial.println(" CONNECTED");
33-
34-
//init and get the time
35-
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
36-
printLocalTime();
3771

38-
//disconnect WiFi as it's no longer needed
39-
WiFi.disconnect(true);
40-
WiFi.mode(WIFI_OFF);
4172
}
4273

4374
void loop()
4475
{
45-
delay(1000);
46-
printLocalTime();
76+
delay(5000);
77+
printLocalTime(); // it will take some time to sync time :)
4778
}

libraries/FFat/examples/FFat_time/FFat_time.ino

-4
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,6 @@ void setup(){
148148
Serial.println("IP address: ");
149149
Serial.println(WiFi.localIP());
150150
Serial.println("Contacting Time Server");
151-
/*
152-
Note: Bundled Arduino lwip supports only ONE ntp server, 2nd and 3rd options are silently ignored
153-
see CONFIG_LWIP_DHCP_MAX_NTP_SERVERS define in ./tools/sdk/esp32/sdkconfig
154-
*/
155151
configTime(3600*timezone, daysavetime*3600, "time.nist.gov", "0.pool.ntp.org", "1.pool.ntp.org");
156152
struct tm tmstruct ;
157153
delay(2000);

libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino

-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ WiFiMulti WiFiMulti;
1717

1818
// Set time via NTP, as required for x.509 validation
1919
void setClock() {
20-
/*
21-
Note: Bundled Arduino lwip supports only ONE ntp server, 2nd and 3rd options are silently ignored
22-
see CONFIG_LWIP_DHCP_MAX_NTP_SERVERS define in ./tools/sdk/esp32/sdkconfig
23-
*/
2420
configTime(0, 0, "pool.ntp.org", "time.nist.gov"); // UTC
2521

2622
Serial.print(F("Waiting for NTP time sync: "));

libraries/LittleFS/examples/LITTLEFS_time/LITTLEFS_time.ino

-4
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,6 @@ void setup(){
160160
Serial.println("IP address: ");
161161
Serial.println(WiFi.localIP());
162162
Serial.println("Contacting Time Server");
163-
/*
164-
Note: Bundled Arduino lwip supports only ONE ntp server, 2nd and 3rd options are silently ignored
165-
see CONFIG_LWIP_DHCP_MAX_NTP_SERVERS define in ./tools/sdk/esp32/sdkconfig
166-
*/
167163
configTime(3600*timezone, daysavetime*3600, "time.nist.gov", "0.pool.ntp.org", "1.pool.ntp.org");
168164
struct tm tmstruct ;
169165
delay(2000);

libraries/SD/examples/SD_time/SD_time.ino

-4
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,6 @@ void setup(){
164164
Serial.println("IP address: ");
165165
Serial.println(WiFi.localIP());
166166
Serial.println("Contacting Time Server");
167-
/*
168-
Note: Bundled Arduino lwip supports only ONE ntp server, 2nd and 3rd options are silently ignored
169-
see CONFIG_LWIP_DHCP_MAX_NTP_SERVERS define in ./tools/sdk/esp32/sdkconfig
170-
*/
171167
configTime(3600*timezone, daysavetime*3600, "time.nist.gov", "0.pool.ntp.org", "1.pool.ntp.org");
172168
struct tm tmstruct ;
173169
delay(2000);

libraries/SD_MMC/examples/SDMMC_time/SDMMC_time.ino

-4
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,6 @@ void setup(){
164164
Serial.println("IP address: ");
165165
Serial.println(WiFi.localIP());
166166
Serial.println("Contacting Time Server");
167-
/*
168-
Note: Bundled Arduino lwip supports only ONE ntp server, 2nd and 3rd options are silently ignored
169-
see CONFIG_LWIP_DHCP_MAX_NTP_SERVERS define in ./tools/sdk/esp32/sdkconfig
170-
*/
171167
configTime(3600*timezone, daysavetime*3600, "time.nist.gov", "0.pool.ntp.org", "1.pool.ntp.org");
172168
struct tm tmstruct ;
173169
delay(2000);

libraries/SPIFFS/examples/SPIFFS_time/SPIFFS_time.ino

-4
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,6 @@ void setup(){
148148
Serial.println("IP address: ");
149149
Serial.println(WiFi.localIP());
150150
Serial.println("Contacting Time Server");
151-
/*
152-
Note: Bundled Arduino lwip supports only ONE ntp server, 2nd and 3rd options are silently ignored
153-
see CONFIG_LWIP_DHCP_MAX_NTP_SERVERS define in ./tools/sdk/esp32/sdkconfig
154-
*/
155151
configTime(3600*timezone, daysavetime*3600, "time.nist.gov", "0.pool.ntp.org", "1.pool.ntp.org");
156152
struct tm tmstruct ;
157153
delay(2000);

0 commit comments

Comments
 (0)