summaryrefslogtreecommitdiff
path: root/software/main/sntp.c
diff options
context:
space:
mode:
authorEric Dao <eric@erickhangdao.com>2022-07-09 22:38:28 -0400
committerEric Dao <eric@erickhangdao.com>2022-07-09 22:38:28 -0400
commitcb234a53f606de8bd136b596a316e912ddf15185 (patch)
treec5f4bf00e4e7e038c24f4ff79f3c5f7af9474ca9 /software/main/sntp.c
parent9dd37009b3dc7d4d155f5c1a529ace46e55c8e0d (diff)
downloadmotorized_blinds-master.tar.gz
motorized_blinds-master.tar.bz2
motorized_blinds-master.zip
added licenseHEADmaster
Diffstat (limited to 'software/main/sntp.c')
-rw-r--r--software/main/sntp.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/software/main/sntp.c b/software/main/sntp.c
new file mode 100644
index 0000000..c639a79
--- /dev/null
+++ b/software/main/sntp.c
@@ -0,0 +1,45 @@
+#include <string.h>
+#include <time.h>
+#include <sys/time.h>
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+#include "freertos/event_groups.h"
+#include "esp_system.h"
+#include "esp_event.h"
+#include "esp_log.h"
+#include "esp_attr.h"
+#include "esp_sleep.h"
+#include "nvs_flash.h"
+#include "esp_sntp.h"
+
+#include "../include/sntp.h"
+
+void obtain_time(void);
+void initialize_sntp(void);
+
+void time_sync_notification_cb(struct timeval *tv) {
+ ESP_LOGI(SNTP_TAG, "Notification of a time synchronization event");
+}
+
+void initialize_sntp(void) {
+ ESP_LOGI(SNTP_TAG, "Initializing SNTP");
+ sntp_setoperatingmode(SNTP_OPMODE_POLL);
+ sntp_setservername(0, "pool.ntp.org");
+ sntp_set_time_sync_notification_cb(time_sync_notification_cb);
+ sntp_init();
+}
+
+void obtain_time(void) {
+ initialize_sntp();
+
+ time_t now = 0;
+ struct tm timeinfo = {0};
+ int retry = 0;
+ const int retry_count = 10;
+ while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
+ ESP_LOGI(SNTP_TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
+ vTaskDelay(2000 / portTICK_PERIOD_MS);
+ }
+ time(&now);
+ localtime_r(&now, &timeinfo);
+} \ No newline at end of file