smart-oil-heating-control-s.../main/main.c
2024-12-16 22:12:29 +01:00

96 lines
2.0 KiB
C

#include <stdio.h>
#include "esp_log.h"
#include "driver/i2c.h"
#include <esp_system.h>
#include <bmp280.h>
#include <dht.h>
#include <aht.h>
#include <veml7700.h>
#include <string.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include "freertos/timers.h"
#include "nvs_flash.h"
#include <time.h>
#include <sys/time.h>
#include <esp_sntp.h>
#include "safety.h"
#include "metrics.h"
#include "outputs.h"
#include "inputs.h"
#include "control.h"
#define I2C_MASTER_SCL 19
#define I2C_MASTER_SDA 18
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 ...");
// Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
// TODO: Error handling!
//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(10000U));
// Obtain the current time
obtain_time();
// Print the current time
print_current_time();
}
}