diff --git a/.vscode/settings.json b/.vscode/settings.json index 34ff52a..b007ac5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -20,7 +20,10 @@ "*.inc": "c", "safety.h": "c", "event_groups.h": "c", - "string.h": "c" + "string.h": "c", + "time.h": "c", + "timers.h": "c", + "nvs_flash.h": "c" }, "idf.openOcdConfigs": [ "board/esp32-wrover-kit-3.3v.cfg" diff --git a/README.md b/README.md index f0dbad7..bdafc6b 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,15 @@ classDiagram Inputs <|-- Control Outputs <|-- Control +Sntp <|-- Control Inputs <|-- Safety Outputs <|--|> Safety -Inputs <|-- HTTP_Metrics -Outputs <|-- HTTP_Metrics -Control <|-- HTTP_Metrics -Safety <|-- HTTP_Metrics +Inputs <|-- Metrics +Outputs <|-- Metrics +Control <|-- Metrics +Safety <|-- Metrics +Sntp <|-- Metrics class Inputs{ +initInputs() @@ -52,7 +54,7 @@ Safety <|-- HTTP_Metrics +initWifi() } - class SNTP{ + class Sntp{ +initSntp() +getSntpState() } diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index 990fa8e..c855f47 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -15,5 +15,8 @@ menu "Smart Oil Heating Control System" config STATIC_GATEWAY_IP_ADDR string "Static IPv4 gateway address" default "192.168.0.1" + config SNTP_SERVER_IP_ADDR + string "SNTP IPv4 server address" + default "192.168.0.1" endmenu diff --git a/main/control.c b/main/control.c index 0b64e78..42f89f8 100644 --- a/main/control.c +++ b/main/control.c @@ -5,6 +5,7 @@ #include "outputs.h" #include "inputs.h" #include "safety.h" +#include "sntp.h" #define PERIODIC_INTERVAL 1U // run safety checks every 1sec @@ -40,16 +41,20 @@ void taskControl(void *pvParameters) { vTaskDelay(PERIODIC_INTERVAL * 1000U / portTICK_PERIOD_MS); - if (getSafetyState() == SAFETY_NO_ERROR) - { - // TODO: control the burner based on timetable - - setCirculationPumpState(DISABLED); - setBurnerState(ENABLED); - } - else + if (getSafetyState() != SAFETY_NO_ERROR) { ESP_LOGW(TAG, "Control not possible due to safety fault!"); + continue; } + + if (getSntpState() != SYNC_SUCCESSFUL) + { + ESP_LOGW(TAG, "Control not possible due to sntp fault!"); + continue; + } + + // TODO: control the burner based on timetable + setCirculationPumpState(DISABLED); + setBurnerState(ENABLED); } } \ No newline at end of file diff --git a/main/main.c b/main/main.c index a34a9bc..82c2d63 100644 --- a/main/main.c +++ b/main/main.c @@ -1,19 +1,6 @@ -#include #include "esp_log.h" -#include "driver/i2c.h" #include -#include -#include -#include -#include -#include -#include -#include -#include "freertos/timers.h" #include "nvs_flash.h" -#include -#include -#include #include "safety.h" #include "metrics.h" @@ -21,34 +8,10 @@ #include "inputs.h" #include "control.h" #include "wifi.h" - -#define I2C_MASTER_SCL 19 -#define I2C_MASTER_SDA 18 +#include "sntp.h" static const char *TAG = "smart-oil-heater-control-system"; -// 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); -} - -// 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 ..."); @@ -63,25 +26,17 @@ void app_main(void) ESP_ERROR_CHECK(ret); // TODO: Error handling! - // initOutputs(); - // initInputs(); - // initSafety(); + initOutputs(); + initInputs(); + initSafety(); initWifi(); - // initControl(); - // initMetrics(); - - // esp_sntp_setoperatingmode(SNTP_OPMODE_POLL); - // 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(); + initSntp(); + initControl(); + initMetrics(); while (1) { vTaskDelay(pdMS_TO_TICKS(1000)); - // Print the current time - // print_current_time(); - - // ESP_LOGI(TAG, "SNTP Server 0 reachability: %i", esp_sntp_getreachability(0)); + // Do nothing ;-) } } \ No newline at end of file diff --git a/main/metrics.c b/main/metrics.c index 1a3886d..dcb90cf 100644 --- a/main/metrics.c +++ b/main/metrics.c @@ -4,11 +4,14 @@ #include "freertos/task.h" #include "esp_wifi.h" #include "esp_log.h" +#include +#include #include "metrics.h" #include "outputs.h" #include "inputs.h" #include "safety.h" +#include "sntp.h" static const char *TAG = "smart-oil-heater-control-system-metrics"; @@ -57,6 +60,21 @@ void taskMetrics(void *pvParameters) aMetrics[u16MetricCounter].fMetricValue = (esp_timer_get_time() / 1000000U); u16MetricCounter++; + /*SNTP Status*/ + strcpy(aMetrics[u16MetricCounter].caMetricName, "sntp_status"); + aMetrics[u16MetricCounter].fMetricValue = getSntpState(); + u16MetricCounter++; + + /*System time + time_t now; + struct tm timeinfo; + time(&now); + localtime_r(&now, &timeinfo); + strcpy(aMetrics[u16MetricCounter].caMetricName, "system_unixtime"); + aMetrics[u16MetricCounter].fMetricValue = (float) now; + u16MetricCounter++; + */ + /*Wifi RSSI*/ wifi_ap_record_t ap; esp_wifi_sta_get_ap_info(&ap); diff --git a/main/sntp.c b/main/sntp.c index 473a0f4..638bfa4 100644 --- a/main/sntp.c +++ b/main/sntp.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include "esp_log.h" + +#include "sntp.h" + +static const char *TAG = "smart-oil-heater-control-system-sntp"; +static eSntpState sntpState = SYNC_NOT_STARTED; +void time_sync_notification_cb(struct timeval *tv); + +void initSntp(void) +{ + esp_sntp_setoperatingmode(SNTP_OPMODE_POLL); + esp_sntp_setservername(0, CONFIG_SNTP_SERVER_IP_ADDR); + + sntp_set_time_sync_notification_cb(time_sync_notification_cb); + esp_sntp_init(); +} + +eSntpState getSntpState(void) +{ + return sntpState; +} + +void time_sync_notification_cb(struct timeval *tv) +{ + ESP_LOGI(TAG, "SNTP synchronization! Unix Time: %lld", tv->tv_sec); + sntpState = SYNC_SUCCESSFUL; +} diff --git a/main/sntp.h b/main/sntp.h index 473a0f4..4feb88b 100644 --- a/main/sntp.h +++ b/main/sntp.h @@ -0,0 +1,11 @@ +#pragma once + +typedef enum _SntpState +{ + SYNC_SUCCESSFUL, + SYNC_NOT_STARTED, + SYNC_FAILED, +} eSntpState; + +void initSntp(void); +eSntpState getSntpState(void); \ No newline at end of file