summaryrefslogtreecommitdiff
path: root/components/sntp
diff options
context:
space:
mode:
authorMaël Gassmann <mael.gassmann@students.bfh.ch>2024-09-28 18:05:02 +0200
committerMaël Gassmann <mael.gassmann@students.bfh.ch>2024-09-28 18:06:43 +0200
commit129a2650c22915bc789b6bdbc9d360bd97059e2f (patch)
tree2cc012bc5ec48ab2596a5c9f1366b71df3484a3e /components/sntp
parent6ebf9d86d095604b9357abc8da1a1a6673856fa8 (diff)
[+] WiFi with dpp on first setup + SNTP
Diffstat (limited to 'components/sntp')
-rw-r--r--components/sntp/CMakeLists.txt3
-rw-r--r--components/sntp/time_sntp.c36
-rw-r--r--components/sntp/time_sntp.h18
3 files changed, 57 insertions, 0 deletions
diff --git a/components/sntp/CMakeLists.txt b/components/sntp/CMakeLists.txt
new file mode 100644
index 0000000..fc734f5
--- /dev/null
+++ b/components/sntp/CMakeLists.txt
@@ -0,0 +1,3 @@
+idf_component_register(SRCS "time_sntp.c"
+ INCLUDE_DIRS "."
+ PRIV_REQUIRES esp_system esp_netif esp_event)
diff --git a/components/sntp/time_sntp.c b/components/sntp/time_sntp.c
new file mode 100644
index 0000000..90cc1d1
--- /dev/null
+++ b/components/sntp/time_sntp.c
@@ -0,0 +1,36 @@
+#include "time_sntp.h"
+
+void set_tz(char* tz){
+ setenv("TZ", tz, 1); // Zürich tz
+ tzset();
+}
+
+void obtain_time(char t[4])
+{
+ time_t now;
+ struct tm timeinfo;
+ time(&now); // Get the current time
+ localtime_r(&now, &timeinfo);
+ strftime(t, sizeof(t)+sizeof(char), "%H%M", &timeinfo); // Seems to require +1 char size to do it correctly
+ ESP_LOGI(SNTP_TAG, "The local time is: %s", t);
+}
+
+void sync_time(void)
+{
+ ESP_ERROR_CHECK(esp_netif_init());
+
+ ESP_LOGI(SNTP_TAG, "Initializing and starting SNTP");
+ /*
+ * This is the basic default config with one server and starting the service
+ */
+ esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG(CONFIG_SNTP_TIME_SERVER);
+
+ esp_netif_sntp_init(&config);
+ // wait for time to be set
+ int retry = 0;
+ const int retry_count = 15;
+ while (esp_netif_sntp_sync_wait(2000 / portTICK_PERIOD_MS) == ESP_ERR_TIMEOUT && ++retry < retry_count) {
+ ESP_LOGI(SNTP_TAG, "Waiting for system time to be synched... (%d/%d)", retry, retry_count);
+ }
+ esp_netif_sntp_deinit();
+}
diff --git a/components/sntp/time_sntp.h b/components/sntp/time_sntp.h
new file mode 100644
index 0000000..a8a894e
--- /dev/null
+++ b/components/sntp/time_sntp.h
@@ -0,0 +1,18 @@
+#ifndef SNTP_H
+#define SNTP_H
+
+#include <time.h>
+#include <sys/time.h>
+#include "esp_system.h"
+#include "esp_event.h"
+#include "esp_log.h"
+#include "esp_netif.h"
+#include "esp_netif_sntp.h"
+#include "esp_sntp.h"
+
+static const char *SNTP_TAG = "SNTP";
+
+void set_tz(char* tz);
+void obtain_time(char t[4]);
+void sync_time(void);
+#endif \ No newline at end of file