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 6b1020967a171c549b3d956825fd0d395de9cce0.

* SimpleTime: add NTPoDHCP option and TimeZone env variable
This commit is contained in:
vortigont
2022-01-17 17:09:58 +03:00
committed by GitHub
parent c014eaf352
commit b3b3403296
8 changed files with 43 additions and 40 deletions

View File

@ -44,8 +44,6 @@ static void setTimeZone(long offset, int daylight)
/* /*
* configTime * configTime
* Source: https://github.com/esp8266/Arduino/blob/master/cores/esp8266/time.c * Source: https://github.com/esp8266/Arduino/blob/master/cores/esp8266/time.c
* Note: Bundled Arduino lwip supports only ONE ntp server, 2nd and 3rd options are silently ignored
* see CONFIG_LWIP_DHCP_MAX_NTP_SERVERS define in ./tools/sdk/esp32/sdkconfig
* */ * */
void configTime(long gmtOffset_sec, int daylightOffset_sec, const char* server1, const char* server2, const char* server3) void configTime(long gmtOffset_sec, int daylightOffset_sec, const char* server1, const char* server2, const char* server3)
{ {
@ -65,8 +63,6 @@ void configTime(long gmtOffset_sec, int daylightOffset_sec, const char* server1,
/* /*
* configTzTime * configTzTime
* sntp setup using TZ environment variable * sntp setup using TZ environment variable
* Note: Bundled Arduino lwip supports only ONE ntp server, 2nd and 3rd options are silently ignored
* see CONFIG_LWIP_DHCP_MAX_NTP_SERVERS define in ./tools/sdk/esp32/sdkconfig
* */ * */
void configTzTime(const char* tz, const char* server1, const char* server2, const char* server3) void configTzTime(const char* tz, const char* server1, const char* server2, const char* server3)
{ {

View File

@ -1,27 +1,65 @@
#include <WiFi.h> #include <WiFi.h>
#include "time.h" #include "time.h"
#include "sntp.h"
const char* ssid = "YOUR_SSID"; const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASS"; const char* password = "YOUR_PASS";
const char* ntpServer = "pool.ntp.org"; const char* ntpServer1 = "pool.ntp.org";
const char* ntpServer2 = "time.nist.gov";
const long gmtOffset_sec = 3600; const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600; const int daylightOffset_sec = 3600;
const char* time_zone = "CET-1CEST,M3.5.0,M10.5.0/3"; // TimeZone rule for Europe/Rome including daylight adjustment rules (optional)
void printLocalTime() void printLocalTime()
{ {
struct tm timeinfo; struct tm timeinfo;
if(!getLocalTime(&timeinfo)){ if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time"); Serial.println("No time available (yet)");
return; return;
} }
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
} }
// Callback function (get's called when time adjusts via NTP)
void timeavailable(struct timeval *t)
{
Serial.println("Got time adjustment from NTP!");
printLocalTime();
}
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
// set notification call-back function
sntp_set_time_sync_notification_cb( timeavailable );
/**
* NTP server address could be aquired via DHCP,
*
* NOTE: This call should be made BEFORE esp32 aquires IP address via DHCP,
* otherwise SNTP option 42 would be rejected by default.
* NOTE: configTime() function call if made AFTER DHCP-client run
* will OVERRIDE aquired NTP server address
*/
sntp_servermode_dhcp(1); // (optional)
/**
* This will set configured ntp servers and constant TimeZone/daylightOffset
* should be OK if your time zone does not need to adjust daylightOffset twice a year,
* in such a case time adjustment won't be handled automagicaly.
*/
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2);
/**
* A more convenient approach to handle TimeZones with daylightOffset
* would be to specify a environmnet variable with TimeZone definition including daylight adjustmnet rules.
* A list of rules for your zone could be obtained from https://github.com/esp8266/Arduino/blob/master/cores/esp8266/TZ.h
*/
//configTzTime(time_zone, ntpServer1, ntpServer2);
//connect to WiFi //connect to WiFi
Serial.printf("Connecting to %s ", ssid); Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
@ -30,18 +68,11 @@ void setup()
Serial.print("."); Serial.print(".");
} }
Serial.println(" CONNECTED"); 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() void loop()
{ {
delay(1000); delay(5000);
printLocalTime(); printLocalTime(); // it will take some time to sync time :)
} }

View File

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

View File

@ -17,10 +17,6 @@ WiFiMulti WiFiMulti;
// Set time via NTP, as required for x.509 validation // Set time via NTP, as required for x.509 validation
void setClock() { void setClock() {
/*
Note: Bundled Arduino lwip supports only ONE ntp server, 2nd and 3rd options are silently ignored
see CONFIG_LWIP_DHCP_MAX_NTP_SERVERS define in ./tools/sdk/esp32/sdkconfig
*/
configTime(0, 0, "pool.ntp.org", "time.nist.gov"); // UTC configTime(0, 0, "pool.ntp.org", "time.nist.gov"); // UTC
Serial.print(F("Waiting for NTP time sync: ")); Serial.print(F("Waiting for NTP time sync: "));

View File

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

View File

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

View File

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

View File

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