diff --git a/main/main.c b/main/main.c index 1ee85ff..a1f6628 100644 --- a/main/main.c +++ b/main/main.c @@ -11,6 +11,9 @@ #include #include "freertos/timers.h" #include "nvs_flash.h" +#include +#include +#include #include "safety.h" #include "metrics.h" @@ -23,6 +26,39 @@ 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() +{ + time_t now; + struct tm timeinfo; + + time(&now); + localtime_r(&now, &timeinfo); + + char strftime_buf[64]; + strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); + + ESP_LOGI(TAG, "Current time: %s", strftime_buf); +} + void app_main(void) { ESP_LOGI(TAG, "starting ..."); @@ -37,14 +73,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_init(); + while (1) { - vTaskDelay(pdMS_TO_TICKS(2048)); + vTaskDelay(pdMS_TO_TICKS(10000U)); + + // Obtain the current time + obtain_time(); + + // Print the current time + print_current_time(); } } \ No newline at end of file diff --git a/main/metrics.c b/main/metrics.c index c7cce67..2823dbb 100644 --- a/main/metrics.c +++ b/main/metrics.c @@ -38,6 +38,8 @@ esp_err_t get_metrics_handler(httpd_req_t *req); void initMetrics(void) { connect_wifi(); + return; + setup_server(); BaseType_t taskCreated = xTaskCreate(