• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

ntp not connecting to a ntp server

Q&A Forum › Category: ESP32 › ntp not connecting to a ntp server
0 Vote Up Vote Down
Barry asked 3 years ago

I have a program that is now quite large. It is based on the WiFi Manager. It has multiple pages and is working well.
I wanted to add NTP in order to support logging with a timestamp. I downloaded and ran the example:
https://randomnerdtutorials.com/esp32-ntp-client-date-time-arduino-ide/
The example works fine. When I incorporate the code into my program, it gets stuck in the loop

while (!timeClient.update()) {
timeClient.forceUpdate();
Serial.print(“,”);
delay(50);
}

I know that the ESP is connected to the LAN as I can access it using my browser. I believe the sequence of events from the example is intact and in the same order.  Also looked at the gitbhub examples from Taranais and think it is correct in to code.
Possibly there is a problem with UDP?
Thanks to everyone for any suggestions.
 

Question Tags: Ntp wifimanager
5 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
 
If you’re creating a project based on the wi-fi manager, make sure you call those lines only when the ESP is connected to the network. When it is in access point mode, it won’t work.
 
Without further information, it is very difficult to find out what might be wrong.
Do you get any errors on the serial monitor?
 
Regards,
Sara

0 Vote Up Vote Down
Barry answered 3 years ago

I’m still trying this. I have the #include and the declarations on the front of the code.
I moved all of the NTP down inside of Main (loop) with a counter to not execute until the program is running in LAN mode. I do a Wifi scan and verify I am connected to the proper router with the desired IP. Quite confident that it is NOT in AP mode.

 
loop {
if(ntp_delay<5) {     // inserts a delay and only runs once.
ntp_delay++;
}
if(ntp_delay == 5){
Serial.println(“starting NTP count==5”); //?????????????????????
ntp_delay++;
if ( (WiFi.status() == 3) &&
((ip.c_str() == “192.168.1.200” ) ) ){ // status 3=connected
Serial.println(WiFi.localIP());
timeClient.begin();
timeClient.setTimeOffset(-21600); // CST -21600, DST – 18000
}
Serial.println(“end of NTP setup”);
}

if (HB_count == 100) {
get_time();
}

The counters are not self-evident, but you can see that I’m looking for wifi statues before calling the NTP functions.
get_time() is the part that reads the lib function and displays the time. It hangs in the WHILE loop

void get_time() {
Serial.println(“Get time”);
while(!timeClient.update()) {
timeClient.forceUpdate();
Serial.print(“,”);
}
formattedDate = timeClient.getFormattedDate();
Serial.println(formattedDate);

     // Extract date
     int splitT = formattedDate.indexOf(“T”);
     dayStamp = formattedDate.substring(0, splitT);
     Serial.print(“DATE: “);
     Serial.println(dayStamp);
     // Extract time
     timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
     Serial.print(“HOUR: “);
     Serial.println(timeStamp);
} // get_time
 
 
 

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
Can you share the complete code with me? Or a section of a working code that I can test?

That way I would be able to test it myself and try to understand what might be the issue.
Regards,
Sara

0 Vote Up Vote Down
Pawel Szczepan answered 3 years ago

Dear Barry. I am currently testing examples from the book I have purchased and cannot connect to the NTP server when I enter my own IP. If I allow DHCP everything is OK. In the microgrid, devices have a fixed IP, which is why I want to solve this problem. You can help?

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
Can you tell me what examples are you testing?
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.