Skip to content

Second and third NTP servers are not working #4964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
szerwi opened this issue Mar 21, 2021 · 10 comments
Closed

Second and third NTP servers are not working #4964

szerwi opened this issue Mar 21, 2021 · 10 comments
Labels
Status: Stale Issue is stale stage (outdated/stuck)

Comments

@szerwi
Copy link

szerwi commented Mar 21, 2021

Hardware:

Board: ESP32 Dev Module
Core Installation version: 1.0.5
IDE name: Arduino IDE
Flash Frequency: 80Mhz
PSRAM enabled: no
Upload Speed: 115200
Computer OS: Windows 10

Description:

I am using SimpleTime example. I have added second and third NTP server, but when I change first NTP server's address to wrong one, it cannot obtain time. configTzTime() and configTime() are not using second and third NTP servers.

Sketch: (leave the backquotes for code formatting)

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

const char* ssid       = "xxx";
const char* password   = "xxx";

const char* ntpServer = "wrong.ntp.org";
const char* ntpServer2 = "pl.pool.ntp.org";
const char* ntpServer3 = "3.pl.pool.ntp.org";
const long  gmtOffset_sec = 3600;
const int   daylightOffset_sec = 3600;

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("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }
  Serial.println(" CONNECTED");
  
  configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", ntpServer, ntpServer2, ntpServer3);
  //configTime(gmtOffset_sec, daylightOffset_sec, ntpServer, ntpServer2, ntpServer3); //same situation
  printLocalTime();

  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);
}

void loop()
{
  delay(1000);
  printLocalTime();
}

Debug Messages:

Connecting to xxx [D][WiFiGeneric.cpp:374] _eventCallback(): Event: 0 - WIFI_READY
[D][WiFiGeneric.cpp:374] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:374] _eventCallback(): Event: 4 - STA_CONNECTED
[D][WiFiGeneric.cpp:374] _eventCallback(): Event: 7 - STA_GOT_IP
[D][WiFiGeneric.cpp:419] _eventCallback(): STA IP: 192.168.1.113, MASK: 255.255.255.0, GW: 192.168.1.1
. CONNECTED
Failed to obtain time
[D][WiFiGeneric.cpp:374] _eventCallback(): Event: 5 - STA_DISCONNECTED
[D][WiFiGeneric.cpp:374] _eventCallback(): Event: 3 - STA_STOP
Failed to obtain time
Failed to obtain time
Failed to obtain time
Failed to obtain time

@lbernstone
Copy link
Contributor

@szerwi
Copy link
Author

szerwi commented Mar 21, 2021

@lbernstone Thanks for the information. Do you plan to add it in the future?

@lbernstone
Copy link
Contributor

That comes from upstream (lwip), but I seriously doubt it. It is a pretty good chunk of memory that most people won't use. Using a pool address from ntp.org is pretty reliable.

@fthiebolt
Copy link

fthiebolt commented Apr 17, 2021

Hello, i was facing the same issue ... moreover, i really need NTP FROM DHCP answer (yes our university is blocking outgoing ntp packets !!)
After struggling for a while, i was able to recompile lwip with support for 3 NTP servers along with NTP from DHCP answer:

git clone -b v3.3.5 --recursive https://github.com/espressif/esp-idf.git esp-idf-v3.3.5
export IDF_PATH=${HOME}/esp-idf-v3.3.5
cd esp-idf-v3.3.5
export IDF_TARGET="esp32"
./install.sh
. ./export.sh
cd examples/get-started/blink
cp ~/.arduino15/packages/esp32/hardware/esp32/1.0.6/tools/sdk/sdkconfig sdkconfig
make menuconfig
    <load sdkconfig>
vim ${IDF_PATH}/components/lwip/lwip/src/include/lwip/opt.h
    880 #define LWIP_DHCP_GET_NTP_SRV           1
    887 #define LWIP_DHCP_MAX_NTP_SERVERS       3
idf.py build

Then you need to backport those mods to your arduino release:

apply same opt.h mods to:
    ~/.arduino15/packages/esp32/hardware/esp32/1.0.6/tools/sdk/include/lwip/lwip/opt.h
cp build/esp-idf/lwip/liblwip.a ~/.arduino15/packages/esp32/hardware/esp32/1.0.6/tools/sdk/li

need to mention i'm using 1.0.6 esp32 arduino so i chose esp-idf 3.3.5

Of course, i would have loved a much more simpler way to achieve this simple lwip mod (fairly simple on esp8266-arduino)
Hope this helps

@Rotzbua
Copy link
Contributor

Rotzbua commented Apr 29, 2021

I think it should be mentioned in the examples like:

configTime(0, 0, "pool.ntp.org", "time.nist.gov");

Also if possible a compile warning or runtime log would be useful.

@stale
Copy link

stale bot commented Jun 28, 2021

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale Issue is stale stage (outdated/stuck) label Jun 28, 2021
@vortigont
Copy link
Contributor

Ugh! This really took me a while to find out why the heck a function with 3 ntp server params just silently ignores two of it. At least a note in source comments would be nice.

@stale
Copy link

stale bot commented Jun 28, 2021

[STALE_CLR] This issue has been removed from the stale queue. Please ensure activity to keep it openin the future.

@stale stale bot removed the Status: Stale Issue is stale stage (outdated/stuck) label Jun 28, 2021
me-no-dev pushed a commit that referenced this issue Jul 22, 2021
… is supported by lwip (#5343)

lwip lib bundled with esp32 Arduino supports only one ntp server. Any additional servers set are just silently ignored.
This default is different from esp8266 Arduino core and very confusing. Most of the examples provided uses 3 different ntp servers for redundancy while only the first one is used actually.
Addressing issue #4964
@stale
Copy link

stale bot commented Aug 28, 2021

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale Issue is stale stage (outdated/stuck) label Aug 28, 2021
@stale
Copy link

stale bot commented Sep 19, 2021

[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.

@stale stale bot closed this as completed Sep 19, 2021
vortigont added a commit to vortigont/esp32-arduino-lib-builder that referenced this issue Nov 2, 2021
 - increase default number of NTP servers up to 3 (match with Arduino esp8266)
 - activate SNTP over DHCP requests (match with Arduino esp8266)

addressing issue  espressif/arduino-esp32#4964
provided via  espressif/esp-idf#7336
me-no-dev pushed a commit to espressif/esp32-arduino-lib-builder that referenced this issue Dec 20, 2021
- increase default number of NTP servers up to 3 (match with Arduino esp8266)
 - activate SNTP over DHCP requests (match with Arduino esp8266)

addressing issue  espressif/arduino-esp32#4964
provided via  espressif/esp-idf#7336
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Stale Issue is stale stage (outdated/stuck)
Projects
None yet
Development

No branches or pull requests

5 participants