Trigger sntp request manually

Clickau
Posts: 1
Joined: Thu Jan 02, 2020 9:55 pm

Trigger sntp request manually

Postby Clickau » Thu Jan 02, 2020 10:10 pm

Basically, I want to create a clock that, at startup, tries to get the time over NTP, and if it fails it asks the user for the time. But I want it to periodically retry to get the time over NTP, every 15 minutes or so. In the beginning, I can call getLocalTime to get the time via NTP, but, if that fails, after setting the time supplied by the user with settimeofday, I can no longer trigger a NTP request, because getLocalTime() and esp-idf functions like time() get the time supplied by the user. After digging around in the esp-idf, i found the function sntp_request which I think would do what I want, but I can't call it because I can't find the sntp.c file that contains it in the arduino framework.

lbernstone
Posts: 33
Joined: Mon Jul 22, 2019 3:20 pm

Re: Trigger sntp request manually

Postby lbernstone » Sun Jan 05, 2020 5:13 pm

If you use configTime, it will create a sntp task that will attempt to connect to an NTP server (every 60 minutes)- no additional config necessary. After you call it, you can call getLocalTime with a timeout (the second parameter), or set a timer if you want it non-blocking. If it fails to get the time from an ntp server, then you run your user query and set the time with settimeofday. The sntp task will still be running, and whenever it is able to connect, it will sync the time.
https://github.com/espressif/arduino-es ... hal-time.c
https://github.com/espressif/esp-lwip/b ... ntp/sntp.c
https://github.com/lbernstone/ESP32_settimeofday

Who is online

Users browsing this forum: No registered users and 22 guests