From ccf28d09040e74678425e32166f07733f5d928ff Mon Sep 17 00:00:00 2001 From: jarvis Date: Thu, 27 Mar 2025 14:57:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=80=9A=E8=BF=87SNTP?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=97=B6=E9=97=B4=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 +- components/FluxTime/CMakeLists.txt | 3 ++ components/FluxTime/FluxTime.c | 50 ++++++++++++++++++++++++++++++ components/FluxTime/FluxTime.h | 32 +++++++++++++++++++ components/FluxUI/CMakeLists.txt | 2 +- components/FluxUI/ui_event_debug.c | 43 +++++++++++++++++++++++-- components/FluxUI/ui_events.c | 11 +++---- components/FluxWifi/FluxWifi.c | 25 +++++++++++++++ main/CMakeLists.txt | 2 +- main/main.cpp | 20 +++++++----- main/main.h | 2 ++ 11 files changed, 173 insertions(+), 20 deletions(-) create mode 100644 components/FluxTime/CMakeLists.txt create mode 100644 components/FluxTime/FluxTime.c create mode 100644 components/FluxTime/FluxTime.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 5055cd2..cee3cf4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -35,6 +35,7 @@ "fluxdisplayport.h": "c", "esp_lcd_panel_vendor.h": "c", "float.h": "c", - "esp_log.h": "c" + "esp_log.h": "c", + "sntp.h": "c" } } diff --git a/components/FluxTime/CMakeLists.txt b/components/FluxTime/CMakeLists.txt new file mode 100644 index 0000000..a8cd9de --- /dev/null +++ b/components/FluxTime/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "FluxTime.c" + REQUIRES driver freertos nvs_flash FluxUI lvgl esp_netif + INCLUDE_DIRS ".") diff --git a/components/FluxTime/FluxTime.c b/components/FluxTime/FluxTime.c new file mode 100644 index 0000000..7d19c3a --- /dev/null +++ b/components/FluxTime/FluxTime.c @@ -0,0 +1,50 @@ +#include +#include "FluxTime.h" + +#include "ui.h" +#include "esp_err.h" +#include "esp_log.h" + +/*包含SNTP的头文件*/ +#include "lwip/apps/sntp.h" +#include + +void time_synic_task(void *arg) +{ + + /*用来进行显示的字符串*/ + char strftime_buf[64]; + /*大数*/ + time_t now = 0; + /*时间结构体*/ + struct tm timeinfo = {0}; + + ESP_LOGI("SNTP..........","SNTP task start"); + + while (1) + { /*1秒钟更新一下显示时间*/ + vTaskDelay(1000 / portTICK_PERIOD_MS); + + time(&now); + localtime_r(&now, &timeinfo); + + strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); + + lv_label_set_text_fmt(ui_pageTimeDebug_labelHearderTime,"%s",strftime_buf); + + ESP_LOGI("SNTP","The current date/time in China is: %s", strftime_buf); + + } + +} + +char get_time_str(void) +{ + char strftime_buf[64]; + time_t now = 0; + struct tm timeinfo = {0}; + time(&now); + localtime_r(&now, &timeinfo); + strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); + return strftime_buf; +} \ No newline at end of file diff --git a/components/FluxTime/FluxTime.h b/components/FluxTime/FluxTime.h new file mode 100644 index 0000000..dcecaa8 --- /dev/null +++ b/components/FluxTime/FluxTime.h @@ -0,0 +1,32 @@ + +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ + +#ifndef _FLUX_TIME_H +#define _FLUX_TIME_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +#define USE_TIME_SYNC_TASK 0 + + + +/*时间同步任务*/ +void time_synic_task(void *arg); + +/*获取当前时间*/ +char get_time_str(void); + +#ifdef __cplusplus +} +#endif + +#endif /*_FLUX_TIME_H */ diff --git a/components/FluxUI/CMakeLists.txt b/components/FluxUI/CMakeLists.txt index 215990c..920eb4a 100644 --- a/components/FluxUI/CMakeLists.txt +++ b/components/FluxUI/CMakeLists.txt @@ -41,5 +41,5 @@ ui_event_debug.c ) idf_component_register(SRCS ${SOURCES} - PRIV_REQUIRES driver lvgl FluxWifi FluxUart FluxSD FluxProtocol + PRIV_REQUIRES driver lvgl FluxWifi FluxUart FluxSD FluxProtocol lwip esp_netif INCLUDE_DIRS "." ) diff --git a/components/FluxUI/ui_event_debug.c b/components/FluxUI/ui_event_debug.c index 6fee8a1..177ac90 100644 --- a/components/FluxUI/ui_event_debug.c +++ b/components/FluxUI/ui_event_debug.c @@ -19,7 +19,7 @@ /*选择是否使能Debug页面的操作*/ -#define MODE_TEST 1 +#define MODE_TEST 0 /*流量计当前示数回传函数——Debug模式*/ void on_buttonFluxRead_clicked(lv_event_t * e) @@ -45,7 +45,44 @@ void on_buttonFluxRead_clicked(lv_event_t * e) /*流量计当前示数显示——Debug模式*/ void on_buttonClearTextarea_clicked(lv_event_t * e) { - #if MODE_TEST +#if MODE_TEST lv_textarea_set_text(ui_pageFluxDebug_textAreaContent, ""); - #endif +#endif +} + +void on_buttonGetTime_clicked(lv_event_t * e) +{ + +#if MODE_TEST + /*获取当前北京时间*/ + /*当WiFi开启时进行时间获取*/ + + char strftime_buf[64]; + + time_t now = 0; + struct tm timeinfo = {0}; + + int retry = 0; + + while(timeinfo.tm_year < (2022 - 1900)) { + ESP_LOGI("SNTP","Waiting for system time to be set... (%d)", ++retry); + vTaskDelay(1000 / portTICK_PERIOD_MS); + time(&now); + localtime_r(&now, &timeinfo); + + strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); + + ESP_LOGI("SNTP","The current date/time in China is: %s", strftime_buf); + + if (retry == 2) + { + break; + } + + } + strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); + + ESP_LOGI("SNTP","The current date/time in China is: %s", strftime_buf); +#endif + } \ No newline at end of file diff --git a/components/FluxUI/ui_events.c b/components/FluxUI/ui_events.c index 1d138ca..6e6cc04 100644 --- a/components/FluxUI/ui_events.c +++ b/components/FluxUI/ui_events.c @@ -18,6 +18,10 @@ #include "FluxProtocol.h" +/*包含SNTP的头文件*/ +#include "lwip/apps/sntp.h" +#include + #define LOWERST_LIGHT_LEVEL (7222) /*更新当前测试标准*/ @@ -470,10 +474,3 @@ void on_dropdownPulseTimes_valueChanged(lv_event_t * e) ESP_ERROR_CHECK(ModbusRTU_Send_0306(ECHO_UART_PORT_NUM1,&flux_frame)); } - - -void on_buttonGetTime_clicked(lv_event_t * e) -{ - /*获取当前北京时间*/ - -} \ No newline at end of file diff --git a/components/FluxWifi/FluxWifi.c b/components/FluxWifi/FluxWifi.c index 4442187..41ca71c 100644 --- a/components/FluxWifi/FluxWifi.c +++ b/components/FluxWifi/FluxWifi.c @@ -3,6 +3,10 @@ #include "ui.h" +/*包含SNTP的头文件*/ +#include "lwip/apps/sntp.h" +#include + wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE]; int number = 0, ap_count = 0; @@ -69,14 +73,19 @@ static void event_handler(void* arg, esp_event_base_t event_base, } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { + /* Disconnected from Wi-Fi */ ESP_LOGI(TAG, "Disconnected from Wi-Fi"); lv_label_set_text_fmt(ui_pageWifiConnect_labelHeaderTitle, "%s", "WIFI-Disconnected"); lv_obj_clear_state(ui_pageHome_switchWifiShow, LV_STATE_CHECKED); lv_label_set_text(ui_pageWifiConnect_labelConnectWifi, "Connect"); + /*关闭SNTP*/ + sntp_stop(); + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + /*成功连入互联网同时获得IP地址*/ ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); @@ -85,6 +94,22 @@ static void event_handler(void* arg, esp_event_base_t event_base, lv_obj_add_state(ui_pageHome_switchWifiShow, LV_STATE_CHECKED); lv_label_set_text(ui_pageWifiConnect_labelConnectWifi, "disconnect"); + + /*开启SNTP进行时间同步*/ + /*初始化SNTP*/ + ESP_LOGI("on_buttonGetTime_clicked","wifi is open"); + + ESP_LOGI("SNTP","Initializing SNTP"); + + sntp_setoperatingmode(SNTP_OPMODE_POLL); + sntp_setservername(0,"ntp1.aliyun.com"); + sntp_setservername(1,"ntp2.aliyun.com"); + + sntp_init(); + + setenv("TZ","GMT-8", 1); + tzset(); + } } diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index d1e55b2..6c47c3d 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -4,7 +4,7 @@ if(CONFIG_EXAMPLE_LCD_IMAGE_FROM_EMBEDDED_BINARY) endif() idf_component_register(SRCS "main.cpp" ${embedded_images} - REQUIRES FluxDisplayPort FluxUI FluxPower spiffs FluxSD FluxButton FluxWifi FluxUart 78__esp-wifi-connect nvs_flash FluxProtocol + REQUIRES FluxDisplayPort FluxUI FluxPower spiffs FluxSD FluxButton FluxWifi FluxUart 78__esp-wifi-connect nvs_flash FluxProtocol FluxTime INCLUDE_DIRS ".") if(CONFIG_EXAMPLE_LCD_IMAGE_FROM_FILE_SYSTEM) diff --git a/main/main.cpp b/main/main.cpp index 4e3adf3..888fbb4 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -9,6 +9,8 @@ /*包含通讯协议头文件*/ #include "FluxProtocol.h" +#define PROGRESS_DELAY_TIME 500 + extern "C" void app_main(void) { /*初始化SD卡*/ @@ -49,38 +51,42 @@ extern "C" void app_main(void) lv_bar_set_value(ui_pageWelCome_barStartProgress,20,LV_ANIM_ON); lv_label_set_text(ui_pageWelCome_labelStartInfo,"turned on backlight 20%"); - vTaskDelay(500/portTICK_PERIOD_MS); + vTaskDelay(PROGRESS_DELAY_TIME/portTICK_PERIOD_MS); /*初始化按键*/ lv_bar_set_value(ui_pageWelCome_barStartProgress,30,LV_ANIM_ON); lv_label_set_text(ui_pageWelCome_labelStartInfo,"Release the power button 30%"); - vTaskDelay(500/portTICK_PERIOD_MS); + vTaskDelay(PROGRESS_DELAY_TIME/portTICK_PERIOD_MS); /*初始化电源管理系统*/ powerInit(); lv_bar_set_value(ui_pageWelCome_barStartProgress,50,LV_ANIM_ON); lv_label_set_text(ui_pageWelCome_labelStartInfo,"power management system 50%"); - vTaskDelay(500/portTICK_PERIOD_MS); + vTaskDelay(PROGRESS_DELAY_TIME/portTICK_PERIOD_MS); lv_bar_set_value(ui_pageWelCome_barStartProgress,70,LV_ANIM_ON); lv_label_set_text(ui_pageWelCome_labelStartInfo,"WiFi scanning environment 70%"); - vTaskDelay(500/portTICK_PERIOD_MS); + vTaskDelay(PROGRESS_DELAY_TIME/portTICK_PERIOD_MS); /*创建串口任务*/ /*创建UART1响应任务*/ - xTaskCreate(uart1_echoTask, "uart1_echo_task", ECHO_TASK_STACK_SIZE, NULL, 10, NULL); + xTaskCreate(uart1_echoTask, "uart1_echo_task", 8*1024, NULL, 10, NULL); lv_bar_set_value(ui_pageWelCome_barStartProgress,90,LV_ANIM_ON); lv_label_set_text(ui_pageWelCome_labelStartInfo,"serial port task 90%"); - vTaskDelay(300/portTICK_PERIOD_MS); + vTaskDelay(PROGRESS_DELAY_TIME/portTICK_PERIOD_MS); +#if USE_TIME_SYNC_TASK + /*创建本地时间同步任务*/ + xTaskCreate(time_synic_task, "time_synic_task", 8*1024, NULL, 10, NULL); +#endif #if 0/*暂时不支持呼吸模拟器*/ /*创建UART2响应任务*/ xTaskCreate(uart2_echoTask, "uart2_echo_task", ECHO_TASK_STACK_SIZE, NULL, 10, NULL); lv_bar_set_value(ui_pageWelCome_barStartProgress,100,LV_ANIM_ON); lv_label_set_text(ui_pageWelCome_labelStartInfo,"serial port task 100%"); - vTaskDelay(500/portTICK_PERIOD_MS); + vTaskDelay(PROGRESS_DELAY_TIME/portTICK_PERIOD_MS); #endif lv_scr_load_anim(ui_pageHome, LV_SCR_LOAD_ANIM_OUT_TOP, 800, 0, false); diff --git a/main/main.h b/main/main.h index 05a38ef..87a582c 100644 --- a/main/main.h +++ b/main/main.h @@ -49,5 +49,7 @@ /* Include key control header file */ #include "FluxButton.h" +/*包含时间同步头文件*/ +#include "FluxTime.h" #endif \ No newline at end of file