羁傲不驯ぃ · 2021年12月15日

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

前言

搭建XR806环境还是比较曲折的,但胜在社区大佬比较多,遇到的问题都及时发出来了,可以直接找到解决方法。这里就不多赘述,有问题可以发帖提问^-^
内心:手上没什么模块,应该玩什么呢,看看大佬玩了啥,mqtt OLED 都是好东西啊,MQTT来了灵感,看看还有什么网络库,果不其然,提供了很多网络库,在里面找到了sntp的,模块库。

值得一提的是,SNTP中提供了获取时间的接口和设置SNTP服务器的接口,但是还是没有研究是不是设置后实时更新的,这个后面再讨论,现在直接使用它来获取时间。

效果

连上网络后,时间才会打印出来。
sjadijahi dj

代码

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",
   ]
}

BUILD.gn

group("ohosdemo") {
    deps = [
        "sntp_demo:app_sntp_Test",
        # "hello_demo:app_hello",
        #"iot_peripheral:app_peripheral",
        #"wlan_demo:app_WlanTest",
        # "LED:app_led",
    ]
}

最后

main.c提供了两种获取时间的方法

  1. 主动调用的方法sntp_get_time(0,&my_time);
  2. 开一个线程出发更新sntp_request("run");

!注意
入口必须以app开头否则不会执行
构建过程遇到分区不够的时候,参考论坛方法解决
sntp有时候获取不到时间,需要设置时区

推荐阅读
关注数
13823
内容数
139
全志XR806开发板相关的知识介绍以及应用专栏。
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息