From 15b74c91f46607e68c9141f99794ecfbc2d3ffbdb34ebee64735eda090a10e65 Mon Sep 17 00:00:00 2001 From: localhorst Date: Tue, 17 Dec 2024 22:26:04 +0100 Subject: [PATCH] set server and cleanup --- .vscode/c_cpp_properties.json | 27 +++++++++++++++++++++ main/main.c | 45 +++++++++++++---------------------- 2 files changed, 44 insertions(+), 28 deletions(-) create mode 100644 .vscode/c_cpp_properties.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..0546715 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,27 @@ +{ + "configurations": [ + { + "name": "ESP-IDF", + "compilerPath": "${default}", + "compileCommands": "${workspaceFolder}/build/compile_commands.json", + "includePath": [ + "${config:idf.espIdfPath}/components/**", + "${config:idf.espIdfPathWin}/components/**", + "${config:idf.espAdfPath}/components/**", + "${config:idf.espAdfPathWin}/components/**", + "${workspaceFolder}/**" + ], + "browse": { + "path": [ + "${config:idf.espIdfPath}/components", + "${config:idf.espIdfPathWin}/components", + "${config:idf.espAdfPath}/components/**", + "${config:idf.espAdfPathWin}/components/**", + "${workspaceFolder}" + ], + "limitSymbolsToIncludedHeaders": false + } + } + ], + "version": 4 + } \ No newline at end of file diff --git a/main/main.c b/main/main.c index a1f6628..8751ec7 100644 --- a/main/main.c +++ b/main/main.c @@ -26,24 +26,6 @@ static const char *TAG = "smart-oil-heater-control-system"; -// Function to wait for time synchronization -void obtain_time() -{ - time_t now = 0; - struct tm timeinfo = {0}; - - // Wait for the time to be set - // while (timeinfo.tm_year < (2020 - 1900)) - { - ESP_LOGI(TAG, "Waiting for system time to be set..."); - vTaskDelay(2000 / portTICK_PERIOD_MS); - time(&now); - localtime_r(&now, &timeinfo); - } - - ESP_LOGI(TAG, "System time is set."); -} - // Function to print the current time void print_current_time() { @@ -59,6 +41,13 @@ void print_current_time() ESP_LOGI(TAG, "Current time: %s", strftime_buf); } +// SNTP Callback function +void time_sync_notification_cb(struct timeval *tv) +{ + ESP_LOGI(TAG, "SNTP synchronization completed! Unix Time: %lld", tv->tv_sec); + ESP_LOGI(TAG, "Time synchronization callback called."); +} + void app_main(void) { ESP_LOGI(TAG, "starting ..."); @@ -73,24 +62,24 @@ void app_main(void) ESP_ERROR_CHECK(ret); // TODO: Error handling! - //initOutputs(); - //initInputs(); - //initSafety(); - //initControl(); + // initOutputs(); + // initInputs(); + // initSafety(); + // initControl(); initMetrics(); esp_sntp_setoperatingmode(SNTP_OPMODE_POLL); - esp_sntp_setservername(0, "10.1.0.1"); // Set NTP server + esp_sntp_setservername(0, "10.1.0.1"); // Set first NTP server local router + + sntp_set_time_sync_notification_cb(time_sync_notification_cb); // Register the SNTP time sync callback esp_sntp_init(); while (1) { - vTaskDelay(pdMS_TO_TICKS(10000U)); - - // Obtain the current time - obtain_time(); - + vTaskDelay(pdMS_TO_TICKS(1000)); // Print the current time print_current_time(); + + ESP_LOGI(TAG, "SNTP Server 0 reachability: %i", esp_sntp_getreachability(0)); } } \ No newline at end of file