sntp module
This commit is contained in:
		
							
								
								
									
										5
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							| @ -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" | ||||
|  | ||||
							
								
								
									
										12
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								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() | ||||
|     } | ||||
|  | ||||
| @ -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 | ||||
|  | ||||
| @ -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); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										61
									
								
								main/main.c
									
									
									
									
									
								
							
							
						
						
									
										61
									
								
								main/main.c
									
									
									
									
									
								
							| @ -1,19 +1,6 @@ | ||||
| #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" | ||||
| @ -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 ;-) | ||||
|     } | ||||
| } | ||||
| @ -4,11 +4,14 @@ | ||||
| #include "freertos/task.h" | ||||
| #include "esp_wifi.h" | ||||
| #include "esp_log.h" | ||||
| #include <time.h> | ||||
| #include <sys/time.h> | ||||
|  | ||||
| #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); | ||||
|  | ||||
							
								
								
									
										30
									
								
								main/sntp.c
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								main/sntp.c
									
									
									
									
									
								
							| @ -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; | ||||
| } | ||||
|  | ||||
							
								
								
									
										11
									
								
								main/sntp.h
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								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); | ||||
		Reference in New Issue
	
	Block a user