sntp module

This commit is contained in:
Hendrik Schutter 2024-12-19 20:33:51 +01:00
parent 0b9f5e2997
commit e5bb620a37
8 changed files with 94 additions and 67 deletions

View File

@ -20,7 +20,10 @@
"*.inc": "c", "*.inc": "c",
"safety.h": "c", "safety.h": "c",
"event_groups.h": "c", "event_groups.h": "c",
"string.h": "c" "string.h": "c",
"time.h": "c",
"timers.h": "c",
"nvs_flash.h": "c"
}, },
"idf.openOcdConfigs": [ "idf.openOcdConfigs": [
"board/esp32-wrover-kit-3.3v.cfg" "board/esp32-wrover-kit-3.3v.cfg"

View File

@ -7,13 +7,15 @@
classDiagram classDiagram
Inputs <|-- Control Inputs <|-- Control
Outputs <|-- Control Outputs <|-- Control
Sntp <|-- Control
Inputs <|-- Safety Inputs <|-- Safety
Outputs <|--|> Safety Outputs <|--|> Safety
Inputs <|-- HTTP_Metrics Inputs <|-- Metrics
Outputs <|-- HTTP_Metrics Outputs <|-- Metrics
Control <|-- HTTP_Metrics Control <|-- Metrics
Safety <|-- HTTP_Metrics Safety <|-- Metrics
Sntp <|-- Metrics
class Inputs{ class Inputs{
+initInputs() +initInputs()
@ -52,7 +54,7 @@ Safety <|-- HTTP_Metrics
+initWifi() +initWifi()
} }
class SNTP{ class Sntp{
+initSntp() +initSntp()
+getSntpState() +getSntpState()
} }

View File

@ -15,5 +15,8 @@ menu "Smart Oil Heating Control System"
config STATIC_GATEWAY_IP_ADDR config STATIC_GATEWAY_IP_ADDR
string "Static IPv4 gateway address" string "Static IPv4 gateway address"
default "192.168.0.1" default "192.168.0.1"
config SNTP_SERVER_IP_ADDR
string "SNTP IPv4 server address"
default "192.168.0.1"
endmenu endmenu

View File

@ -5,6 +5,7 @@
#include "outputs.h" #include "outputs.h"
#include "inputs.h" #include "inputs.h"
#include "safety.h" #include "safety.h"
#include "sntp.h"
#define PERIODIC_INTERVAL 1U // run safety checks every 1sec #define PERIODIC_INTERVAL 1U // run safety checks every 1sec
@ -40,16 +41,20 @@ void taskControl(void *pvParameters)
{ {
vTaskDelay(PERIODIC_INTERVAL * 1000U / portTICK_PERIOD_MS); vTaskDelay(PERIODIC_INTERVAL * 1000U / portTICK_PERIOD_MS);
if (getSafetyState() == SAFETY_NO_ERROR) if (getSafetyState() != SAFETY_NO_ERROR)
{
// TODO: control the burner based on timetable
setCirculationPumpState(DISABLED);
setBurnerState(ENABLED);
}
else
{ {
ESP_LOGW(TAG, "Control not possible due to safety fault!"); 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);
} }
} }

View File

@ -1,19 +1,6 @@
#include <stdio.h>
#include "esp_log.h" #include "esp_log.h"
#include "driver/i2c.h"
#include <esp_system.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 "nvs_flash.h"
#include <time.h>
#include <sys/time.h>
#include <esp_sntp.h>
#include "safety.h" #include "safety.h"
#include "metrics.h" #include "metrics.h"
@ -21,34 +8,10 @@
#include "inputs.h" #include "inputs.h"
#include "control.h" #include "control.h"
#include "wifi.h" #include "wifi.h"
#include "sntp.h"
#define I2C_MASTER_SCL 19
#define I2C_MASTER_SDA 18
static const char *TAG = "smart-oil-heater-control-system"; 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) void app_main(void)
{ {
ESP_LOGI(TAG, "starting ..."); ESP_LOGI(TAG, "starting ...");
@ -63,25 +26,17 @@ void app_main(void)
ESP_ERROR_CHECK(ret); ESP_ERROR_CHECK(ret);
// TODO: Error handling! // TODO: Error handling!
// initOutputs(); initOutputs();
// initInputs(); initInputs();
// initSafety(); initSafety();
initWifi(); initWifi();
// initControl(); initSntp();
// initMetrics(); 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();
while (1) while (1)
{ {
vTaskDelay(pdMS_TO_TICKS(1000)); vTaskDelay(pdMS_TO_TICKS(1000));
// Print the current time // Do nothing ;-)
// print_current_time();
// ESP_LOGI(TAG, "SNTP Server 0 reachability: %i", esp_sntp_getreachability(0));
} }
} }

View File

@ -4,11 +4,14 @@
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_wifi.h" #include "esp_wifi.h"
#include "esp_log.h" #include "esp_log.h"
#include <time.h>
#include <sys/time.h>
#include "metrics.h" #include "metrics.h"
#include "outputs.h" #include "outputs.h"
#include "inputs.h" #include "inputs.h"
#include "safety.h" #include "safety.h"
#include "sntp.h"
static const char *TAG = "smart-oil-heater-control-system-metrics"; 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); aMetrics[u16MetricCounter].fMetricValue = (esp_timer_get_time() / 1000000U);
u16MetricCounter++; 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 RSSI*/
wifi_ap_record_t ap; wifi_ap_record_t ap;
esp_wifi_sta_get_ap_info(&ap); esp_wifi_sta_get_ap_info(&ap);

View File

@ -0,0 +1,30 @@
#include <time.h>
#include <sys/time.h>
#include <esp_sntp.h>
#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;
}

View File

@ -0,0 +1,11 @@
#pragma once
typedef enum _SntpState
{
SYNC_SUCCESSFUL,
SYNC_NOT_STARTED,
SYNC_FAILED,
} eSntpState;
void initSntp(void);
eSntpState getSntpState(void);