Hello everyone, I am new to the group and I am happy to be here. I used part of the code “Obtaining date and time with ESP32 in Arduino IDE (NTP Client)” of RNT and made a slight modification to adjust the time of a RTC DS1307 that I copied here the function code:
void ActualizarRTC(){
DateTime Ahora = rtc.now();
timeClient.begin();
timeClient.setTimeOffset(-18000);
while(!timeClient.update()) {
timeClient.forceUpdate();
}
String FechaNTP;FechaNTP = timeClient.getFormattedDate();
int a, m, d, h, n, s;
a = FechaNTP.substring( 0, 4).toInt();
m = FechaNTP.substring( 5, 7).toInt();
d = FechaNTP.substring( 8, 10).toInt();
h = FechaNTP.substring(11, 13).toInt();
n = FechaNTP.substring(14, 16).toInt();
s = FechaNTP.substring(17, 19).toInt();
rtc.adjust(DateTime(a, m, d, h, n, s));
}
I’m sure there’s a more elegant way to do it, but for now it works for me. The problem is that due to the “while” loop the code stops if there is no Internet connection. Since I am a newbie with microprocessors and programming languages, I cannot find a way to avoid this problem and I hope that one of you can help me. Thanks in advance.
Hi Paul.
I’m not sure if I understood your question: do you want to reconnect to Wi-Fi when it drops connection?
See this answer on this thread and see if you can adapt it to your case: https://github.com/espressif/arduino-esp32/issues/653#issuecomment-425645575
If you provide more details, maybe I can help you better.
Regards,
Sara
Hi Sara, thaks for your response. The “while” loop stops the code execution if the internet connection is drop. What I’d like to do is avoid this happens.
So, I’d like to modify this part of the code in way that it runs if there is connection or it skips it if there is not.
Hi Paul.
That happens because the you need wi-fi connection to get date and time from NTPClient.
So, I suggest that you add an if statement that checks whether Wi-Fi is connected or not. If it is connected, it gets date and time, if not, it does other stuff.
To check whether wi-fi is connected or not, you can use the following statement.
if (WiFi.status() != WL_CONNECTED){
I don’t know whether this will work in your specific example, but it is a way to check whether the wi-fi is connected or not.
Let me know if this helps.
Regards,
Sara
Hi Sara, the statement you suggest checks if there is WiFi available but not if there is internet connection. What I need is check if internet connection is available in order to avoid the while statement stops the code.
I could have WiFi because mi local router is working and lack of internet connection at the same time.
Regards,
Paul
Hi again.
Now I understand what you want to do.
Honestly, I don’t know how to do that. I’ve searched about that for a while and haven’t found a clear answer.
- https://github.com/espressif/arduino-esp32/issues/2056
- http://bbs.esp32.com/viewtopic.php?f=13&t=5006
I think at the moment there isn’t an easy way to do that. I’m sorry that I can’t help much.
Meanwhile, if you discover how to do that, please share with us. It will be useful for other readers.
Regards,
Sara
Hi Sara,
I found the way for pinging from the ESP32 to a web page using the pbecchi´s ESP_ping library, available at:
https://github.com/pbecchi/ESP32_ping/blob/master.
Using this library is possible to verify if there is an internet conection.
So, the code for checking the internet connection and adjust the RTC remains as follows:
Start your code here void ActualizarRTC() {
int ia[4] = { 172, 217, 3, 68 };
IPAddress pin = IPAddress(ia[0], ia[1], ia[2], ia[3]);
Serial.printf("Ping : %d.%d.%d.%d ->", ia[0], ia[1], ia[2], ia[3]);
if (ping_start(pin, 4, 0, 0, 5)) {
timeClient.begin();
timeClient.setTimeOffset(-18000); //ajuste de -5 horas GMT
while (!timeClient.update()) {
timeClient.forceUpdate();
}
String FechaNTP;
FechaNTP = timeClient.getFormattedDate();
int a, m, d, h, n, s;
a = FechaNTP.substring( 0, 4).toInt();
m = FechaNTP.substring( 5, 7).toInt();
d = FechaNTP.substring( 8, 10).toInt();
h = FechaNTP.substring(11, 13).toInt();
n = FechaNTP.substring(14, 16).toInt();
s = FechaNTP.substring(17, 19).toInt();
rtc.adjust(DateTime(a, m, d, h, n, s));
FechaHora(rtc.now());
Serial.println("Actualizado RTC");
}
else {
tft.println(" ");
tft.println(" Sin conexion");
tft.println(" a Internet");
delay(3000);
Serial.println("Sin conexion Internet");
}
}
Be warned that the pbecchi´s library does not work with the Arduino´s ESP32 Core version 1.0.1, but the version 1.0.0.
Maybe someone find useful this library.
Hi again Paul.
I’m happy that you’ve found an answer for your particular problem.
Thank you so much for sharing your code and this information. I’m sure it can be helpful for a lot of our readers.
Regards,
Sara 🙂
timeClient.getFormattedDate() ??? cette commande existe pas dans la Lib RTCLib ?
Hi.
That’s because you didn’t install the right library.
We’re using the NTPClient library forked by Taranais. You must install the library as explained in the tutorial.https://randomnerdtutorials.com/esp32-ntp-client-date-time-arduino-ide/
REgards,
Sara
Bonjour 🙂
Effectivement, je suis tout perdu avec tout c’est librairie et command :P….
Mais je crois que cette librairie NTPClient a évolué..
J’avais besoin de justement un format de date et trouvait plus rien..
Cerveau complètement brulé 😀
Mais tu m’a aidé 😉
j’ai trouvé ça :
getFormattedTime KEYWORD2
getFormattedUTCTime KEYWORD2
getFormattedDateTime KEYWORD2
getFormattedUTCDateTime KEYWORD2
Mais la :
getFormattedDate()
Ni est plus..
Ou aidé moi quelqu’un 😛
Merci
Hi.
You are right.
The library was updated and that method no longer exists. I have to update that tutorial.
Meanwhile, you can take a look at the following: https://randomnerdtutorials.com/esp32-date-time-ntp-client-server-arduino/
Also, these may help:
- https://randomnerdtutorials.com/epoch-unix-time-esp32-arduino/
- https://randomnerdtutorials.com/esp32-ntp-timezones-daylight-saving/
Regards,
Sara