# rk2006_ntp_time **Repository Path**: liujiawm/rk2006_ntp_time ## Basic Information - **Project Name**: rk2006_ntp_time - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-05-17 - **Last Updated**: 2025-05-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ntp案例说明文档 ## 1、实验简介 随着物联网(IoT)设备的快速发展和普及,操作系统的性能和可扩展性成为开发者关注重点。精准的时间管理和多样化的功能是智能家居闹钟的关键特性。通过结合网络时间协议(NTP),可以确保设备在任何时间都能保持准确的时间,为用户提供高精度的时间服务。OpenHarmony作为面向多设备、多场景的轻量级开源操作系统,提供了全面且灵活的开发平台。 ## 2、软件设计 ### 2.1、整体软件设计 将通过ntp服务器时间获取这一具体应用,详细讲解如何利用OpenHarmony的优势,结合硬件和软件,开发通过NTP网络连接可以实现设备的时间同步的时间。 ## 3、软件设计 ### 3.1、主程序代码分析 #### 3.1.1、任务流程 #### 3.1.2、任务说明 主程序分为如下几个部分: - 创建iot线程,负责连接网络。 - 创建ntp时间同步线程,负责更新设备端的时间。 ```c void smart_clock_example(void) { unsigned int thread_id1,thread_id2; LOS_Msleep(1000); printf(LOG_SC_TAG_INFO"欢迎使用 智能时钟\r\n", __LINE__); create_task(&thread_id1,1024 * 4, 20, sc_iot_thread, NULL , "sc iot"); create_task(&thread_id2,1024 * 4, 20, sc_ntp_thread, NULL , "sc ntp"); } ``` ### 3.2、ntp任务 #### 3.2.1、任务流程 #### 3.2.2、任务说明 先判断是否连接wifi,在连接wifi状态下初始化ntp。之后每隔60s同步一次时间。 ```c static void *sc_ntp_thread(uint32_t args) { uint32_t now_time = 0; uint32_t ntp_time = 0; time_t now_time_t = 0; sc_ntp_time_t ntp_time_data; sc_ntp_time_t *ntp = &ntp_time_data; int time = 0; uint8_t network_connect_last_status = 0; // 网络最近连通状态标志,0为上一次为关闭,1为上一次为连通 // ntp初始化 THREAD_START_INFO if (get_sta_link() == 0) { ntp_init(); } printf(LOG_SC_TAG_NTP"init finish\r\n", __LINE__); while (1) { // 如果网络已连通,则进行ntp同步 if (get_sta_link() == 0) { if (network_connect_last_status == 0) { // 如果上一次网络未连通,则进行ntp重初始化 network_connect_last_status = 1; ntp_reinit(); LOS_Msleep(2000); } // 每隔60秒,进行ntp同步 if ((++time) % 300 == 0 || ntp->sync_status == 0 || ntp->year > 2030) { printf(LOG_SC_TAG_NTP"ntp_time: %10lu start======\n", __LINE__, ntp_time); ntp_time = ntp_get_time(NULL); printf(LOG_SC_TAG_NTP"ntp_time: %10lu end ======\n", __LINE__, ntp_time); if (ntp_time == 0) { ntp->sync_status = 0; now_time++; } else { ntp->sync_status = 1; now_time = ntp_time; } } else { now_time++; } } else { // 如果网络未连通,则不进行ntp同步,且ntp同步状态置为0 ntp->sync_status = 0; network_connect_last_status = 0; // 如果未连通,则时间加1(即该任务是每隔1秒执行一次) now_time++; } // 获取本地时间 now_time_t = (time_t)now_time; struct tm *now_tm = localtime(&now_time_t); // 赋值给公共结构体 ntp->year = now_tm->tm_year + 1900; ntp->month = now_tm->tm_mon + 1; ntp->day = now_tm->tm_mday; ntp->hour = now_tm->tm_hour; ntp->min = now_tm->tm_min; ntp->sec = now_tm->tm_sec; // 打印信息 if (now_tm->tm_sec % 30 == 0) { printf(LOG_SC_TAG_NTP"sync_status: %s now_tm: %04d-%02d-%02d %02d:%02d:%02d\n", __LINE__, (ntp->sync_status == 0) ? ("nosync") : ("sync"), now_tm->tm_year + 1900, now_tm->tm_mon + 1, now_tm->tm_mday, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec); } // 等待950ms,上述任务需要消耗一定时间,预计为50msec LOS_Msleep(950); } return NULL; } ``` ## 4、编译调试 修改 `vendor/lockzhiner/rk2206/sample` 路径下 BUILD.gn 文件,指定 `smart_ntp_example` 参与编译。 ```r "./d3_smart_clock:smart_ntp_example", ``` 修改 `device/lockzhiner/rk2206/sdk_liteos` 路径下 Makefile 文件,添加 `-lsmart_ntp_example` 参与编译。 ```r hardware_LIBS = -lhal_iothardware -lhardware -lm -lsmart_ntp_example ``` ## 5、运行结果 [sc ntp 185] ntp_time: 1747392092 start====== [sc ntp 187] ntp_time: 1747392380 end ====== [sc ntp 227] sync_status: sync now_tm: 2025-05-16 18:46:30 [sc ntp 227] sync_status: sync now_tm: 2025-05-16 18:47:00 [sc ntp 227] sync_status: sync now_tm: 2025-05-16 18:47:30 [sc ntp 227] sync_status: sync now_tm: 2025-05-16 18:48:00 [sc ntp 227] sync_status: sync now_tm: 2025-05-16 18:48:30 [sc ntp 227] sync_status: sync now_tm: 2025-05-16 18:49:00 [sc ntp 227] sync_status: sync now_tm: 2025-05-16 18:49:30