1. 程式人生 > 實用技巧 >W800 藍芽配網並使用NTP獲取網路時間

W800 藍芽配網並使用NTP獲取網路時間

本節使用藍芽為w800配網連線路由器,並使用NTP來獲取網路時間。
下載藍芽配網軟體:https://pan.baidu.com/s/18Wox5lCUAU29nyLhingYLw
提取碼:6666
在主程式中建立藍芽配網的任務:

在任務中呼叫APIble_oneshot(4)配置模式為藍芽配網:
void ble_task(void)
{
u32 cnt=0;
u32 i=0;
struct tls_ethif *ethif;

LED_Init();
bt_enable(uart_1,TLS_BT_LOG_NONE);
tls_os_time_delay(HZ);//enable之後掃描需要做一點延遲,否則會出現not ready情形
ethif=tls_netif_get_ethif();
init_wifi_config();//用於配置wifi相關設定如自動連線等
printf("onshoot in progress\r\n");
ble_oneshot(4);//demo_connect_net中關於oneshoot模式
printf("configing...");//等待配置過程中
while(tls_wifi_get_oneshot_flag())//0 oneshot close 1 oneshotopen
tls_os_time_delay(HZ);//當oneshot開啟時延時1s
while(1)
{


    if(ethif->status)
	{

		ntp_updata_time();//獲取ntp伺服器函式,參照ntp.c demo拿取ntp伺服器資料
		tls_os_time_delay(HZ);
		printf("更新第 %d 次time\r\n",++i);
	}

	else
	{
		printf("press key 3s...\r\n");
		tls_os_time_delay(3*HZ);
	}
}

}

程式編譯下載到開發板執行後使用藍芽配網軟體(WMBleWiFi)進行一鍵配網出現藍芽名稱為WM-*********開頭的藍芽,
配置好wifi名字和密碼即可聯網。注意此時需要連線的wifi為2.4GHZ.配網成功後,無需每次上電配網,
重啟後自動聯網獲取ntp時間。配網完成後,串列埠會一直獲取ntp時間。