【XR806开发板试用】使用sntp获取时间
-
本着能水则水的心态,睡前搞了一个小的demo
如题,XR806既然可以联网,那使用SNTP获取个时间肯定不是问题。既然上面有人玩了MQTT,跟随大佬的脚步去 net 文件夹中看看都有什么库,果然不出我所料,有SNTP,那就好玩了。。。过程可能有点曲折,比如 sntp 获取不到时间。
再比如编译错误
这显然是 分区有问题 论坛有解决方法
效果:
main.c#include <stdio.h> #include "ohos_init.h" #include "kernel/os/os.h" #include "wifi_device.h" #include "sntp.h" static OS_Thread_t g_main_thread; static OS_Thread_t g_time_thread; #define WIFI_DEVICE_CONNECT_AP_SSID "*****************" #define WIFI_DEVICE_CONNECT_AP_PSK "************" //更改成自己的wifi static int wifi_device_connect() { const char ssid_want_connect[] = WIFI_DEVICE_CONNECT_AP_SSID; const char psk[] = WIFI_DEVICE_CONNECT_AP_PSK; if(WIFI_SUCCESS != EnableWifi()) { printf("Error: EnableWifi fail.\n"); return; } printf("EnableWifi Success.\n"); if (WIFI_STA_ACTIVE == IsWifiActive()) printf("Wifi is active.\n"); OS_Sleep(1); if (WIFI_SUCCESS != Scan()) { printf("Error: Scan fail.\n"); return; } printf("Wifi Scan Success.\n"); OS_Sleep(1); WifiScanInfo scan_results[30]; unsigned int scan_num = 30; if (WIFI_SUCCESS != GetScanInfoList(scan_results, &scan_num)) { printf("Error: GetScanInfoList fail.\n"); return; } WifiDeviceConfig config = { 0 }; int netId = 0; int i; for (i = 0; i < scan_num; i++) { if (0 == strcmp(scan_results[i].ssid, ssid_want_connect)) { memcpy(config.ssid, scan_results[i].ssid, WIFI_MAX_SSID_LEN); memcpy(config.bssid, scan_results[i].bssid, WIFI_MAC_LEN); strcpy(config.preSharedKey, psk); config.securityType = scan_results[i].securityType; config.wapiPskType = WIFI_PSK_TYPE_ASCII; config.freq = scan_results[i].frequency; break; } } if (i >= scan_num) { printf("Error: No found ssid in scan_results\n"); return; } printf("GetScanInfoList Success.\n"); if (WIFI_SUCCESS != AddDeviceConfig(&config, &netId)) { printf("Error: AddDeviceConfig Fail\n"); return; } printf("AddDeviceConfig Success.\n"); if (WIFI_SUCCESS != ConnectTo(netId)) { printf("Error: ConnectTo Fail\n"); return; } printf("ConnectTo Success\n"); } static int my_sntp_init(){ if(sntp_set_server(0,"0.cn.pool.ntp.org") == 0){ printf("set OK \r\n"); } } static void MainThread(void *arg) { sntp_time my_time,*time; time = &my_time; wifi_device_connect(); my_sntp_init(); while (1) { //printf("hello world!\r\n"); sntp_get_time(0,&my_time); // sntp_request("run"); //time = sntp_obtain_time(); printf("Now Time is %d:%d:%d\r\n",my_time.hour-161,my_time.min-139,my_time.sec); LOS_Msleep(1000); } } static void TimeThread(void *arg) { while (1) { sntp_request("run"); LOS_Msleep(1000); } } void SNTPTestMain(void) //(2) { printf("SNTP Test Start\n"); if (OS_ThreadCreate(&g_main_thread, "MainThread", MainThread, NULL, OS_THREAD_PRIO_APP, 4 * 1024) != OS_OK) { printf("[ERR] Create MainThread Failed\n"); } if (OS_ThreadCreate(&g_time_thread, "MainThread", TimeThread, NULL, OS_THREAD_PRIO_APP, 4 * 1024) != OS_OK) { printf("[ERR] Create MainThread Failed\n"); } } SYS_RUN(SNTPTestMain); //必须以app开头 否则不会调用
import("//device/xradio/xr806/liteos_m/config.gni") static_library("app_sntp_Test") { configs = [] sources = [ "src/main.c", ] cflags = board_cflags include_dirs = board_include_dirs include_dirs += [ ".", "//utils/native/lite/include", "//foundation/communication/wifi_lite/interfaces/wifiservice", "//device/xradio/xr806/xr_skylark/include/net/sntp", ] }
group("ohosdemo") { deps = [ "sntp_demo:app_sntp_Test", # "hello_demo:app_hello", #"iot_peripheral:app_peripheral", #"wlan_demo:app_WlanTest", # "LED:app_led", ] }
比较草率。。。
获取时间的方法中间有两种,一种是主动调用获取,一种是增加一条线程主动更新,然后随时获取,个人更推荐增加 一条线程主动更新的方法。
代码比较草率,希望dalao们盖楼指正
-
main.c 两种方法都有
-
亲,
sntp_get_time(NULL, &my_time);
参数有错误哦,这种方式
变量my_time应该是struct timeval
类型的。
sntp_get_time接口定义如下:/** * @brief Get time from the remote server. * @note This a blocking interface. * @param ntp_time: Pointer to the struct timeval. * arg: The pointer of sntp module parameter * @retval 0:success -1:fail */ int sntp_get_time(sntp_arg *arg, struct timeval *ntp_time)
Copyright © 2024 深圳全志在线有限公司 粤ICP备2021084185号 粤公网安备44030502007680号