Navigation

    全志在线开发者论坛

    • Register
    • Login
    • Search
    • Categories
    • Tags
    • 在线文档
    • 社区主页

    【XR806开发板试用】使用sntp获取时间

    Wireless & Analog Series
    2
    3
    1816
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • BedRock
      BedRock LV 6 last edited by

      本着能水则水的心态,睡前搞了一个小的demo😳 😳 😳
      如题,XR806既然可以联网,那使用SNTP获取个时间肯定不是问题。既然上面有人玩了MQTT,跟随大佬的脚步去 net 文件夹中看看都有什么库,果然不出我所料,有SNTP,那就好玩了。。。

      过程可能有点曲折,比如 sntp 获取不到时间。40e5a6bd-58db-45a5-af06-8a232499785e-image.png
      再比如编译错误
      97c96819-e957-4940-bea5-8dbb2b344c54-image.png
      这显然是 分区有问题 论坛有解决方法
      效果:
      cc6bcfdd-a96b-4ba0-8799-1f1802118fcb-image.png
      8ed83d0f-523d-4474-a476-c9d59881e3d0-image.png
      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开头 否则不会调用
      

      BUILD.gn

      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们盖楼指正😁

      1 Reply Last reply Reply Quote Share 0
      • BedRock
        BedRock LV 6 last edited by

        main.c 两种方法都有😁

        1 Reply Last reply Reply Quote Share 0
        • L
          ljfly LV 2 last edited by

          亲,

          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)
          
          1 Reply Last reply Reply Quote Share 0
          • 1 / 1
          • First post
            Last post

          Copyright © 2024 深圳全志在线有限公司 粤ICP备2021084185号 粤公网安备44030502007680号

          行为准则 | 用户协议 | 隐私权政策