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.
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
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
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
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?