HarmonyOS小熊派 | HarmonyOS WiFi编程开发--UDP客户端

BearPi-HM_Nano开发板WiFi编程开发——UDP客户端

本示例将演示如何在BearPi-HM_Nano开发板上使用socket编程创建UDP客户端,就收客户端消息并回复固定消息。

BearPi-HM_Nano

socket API分析

本案例主要使用了以下几个API完socket编程实验

1、socket()

sock_fd = socket(AF_INET, SOCK_STREAM, 0)) //AF_INT:ipv4, SOCK_STREAM:tcp协议

描述:

在网络编程中所需要进行的第一件事情就是创建一个socket,无论是客户端还是服务器端,都需要创建一个socket,该函数返回socket文件描述符,类似于文件描述符。socket是一个结构体,被创建在内核中。

2、sendto()

int sendto ( socket s , const void * msg, int len, unsigned int flags,
const struct sockaddr * to , int tolen ) ;

描述:

sendto() 用来将数据由指定的socket传给对方主机。参数s为已建好连线的socket。参数msg指向欲连线的数据内容,参数flags 一般设0,

3、recvfrom()

int recvfrom(int s, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen);

描述: 从指定地址接收UDP数据报。

参数:

名字 描述
s socket描述符.
buf UDP数据报缓存地址.
len UDP数据报长度.
flags 该参数一般为0.
from 对方地址
fromlen 对方地址长度.

软件设计

主要代码分析

完成Wifi热点的连接需要以下几步

  1. 通过 socket 接口创建一个socket,AF_INT表示ipv4,SOCK_STREAM表示使用tcp协议
  2. 调用 sendto 接口发送数据到服务端。
  3. 调用 recvfrom 接口接收服务端发来的数据
yuchuan@yuchuan:~/MasterData/SharedData/MasterHarmonyOSCode/HarmonyOSCode/applications/sample/BearPi/BearPi-HM_Nano/sample/D3_YuchuanIoTUDPClient$ tree
.
├── BUILD.gn
├── include
│   └── wifi_connect.h
├── src
│   └── wifi_connect.c
└── yuchuan_udp_client.c

2 directories, 4 files
yuchuan@yuchuan:~/MasterData/SharedData/MasterHarmonyOSCode/HarmonyOSCode/applications/sample/BearPi/BearPi-HM_Nano/sample/D3_YuchuanIoTUDPClient$

yuchuan_udp_client.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifi_connect.h"

#include "lwip/sockets.h"

#define WIFI_ACOUNT "886699"
#define WIFI_PASSWORD "tesrrere"
#define TASK_PRIORITY 24
#define _PROT_ 8888

// 在sock_fd 进行监听,在 new_fd 接收新的链接
int sock_fd;

int addr_length;
static const char *send_data = "Hello YuchuanHuaying UDP Send Data.";

/* UDP 客户端 任务的业务函数 */
static void yuchuanUDPClientTask(void)
{
    // 服务器的地址信息
    struct sockaddr_in send_addr;
    socklen_t addr_length = sizeof(send_addr);
    char recevBuff[512];

    // 连接WIFI
    wifiConnect(WIFI_ACOUNT, WIFI_PASSWORD);

    // 创建socket
    if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
    {
        perror("Create Socket Falied.\r\n");
        exit(1);
    }
    
    // 初始化预连接的服务器地址
    send_addr.sin_family = AF_INET;
    // extern uint16_t htons (uint16_t __hostshort)
    send_addr.sin_port = htons(_PROT_);
    send_addr.sin_addr.s_addr = inet_addr("101.11.233.133");
    addr_length = sizeof(send_addr);

    // 总计发送 count 次数据
    while (1)
    {
        /* Set N bytes of S to 0.  */
        // extern void bzero (void *__s, size_t __n) __THROW __nonnull ((1));
        bzero(recevBuff, sizeof(recevBuff));

        // 发送数据到服务器
        /* Send N bytes of BUF on socket FD to peer at address ADDR (which is
        ADDR_LEN bytes long).  Returns the number sent, or -1 for errors.

        This function is a cancellation point and therefore not marked with
        __THROW.  */
        // extern ssize_t sendto (int __fd, const void *__buf, size_t __n, int __flags, __CONST_SOCKADDR_ARG __addr, socklen_t __addr_len);
        sendto (sock_fd, send_data, strlen(send_data), 0, (struct sockaddr *)&send_addr, addr_length);

        // 线程休眠一段时间
        sleep(10);

        // 接收服务端返回的数据
        // recvfrom (int __fd, void *__restrict __buf, size_t __n, int __flags, __SOCKADDR_ARG __addr, socklen_t *__restrict __addr_len)
        recvfrom (sock_fd, recevBuff, sizeof(recevBuff), 0, (struct sockaddr *)&send_addr, &addr_length);
        printf("%s:%d=>%s\n", inet_ntoa(send_addr.sin_addr), ntohs(send_addr.sin_port), recevBuff);
    }
    
    // 关闭socket
    closesocket(sock_fd);
}

/* UDP 客户端的入口函数 */
static void yuchuanUDPClientEntry(void)
{
    osThreadAttr_t threadAttr;
    threadAttr.attr_bits = 0U;
    threadAttr.cb_mem = NULL;
    threadAttr.cb_size = 0U;
    threadAttr.stack_mem = NULL;
    threadAttr.stack_size = 10240;
    threadAttr.priority = TASK_PRIORITY;

    threadAttr.name = "YuchuanUDPClientTask";
    if (osThreadNew((osThreadFunc_t)yuchuanUDPClientTask, NULL, &threadAttr) == NULL)
    {
        printf("Falied To Create YuchuanUDPClientTask.!!!\r\n");
    }
}

APP_FEATURE_INIT(yuchuanUDPClientEntry);

include/wifi_connect.h

/*
 * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __WIFI_CONNECT_H__
#define __WIFI_CONNECT_H__

int wifiConnect(const char *ssid, const char *psk);

#endif /* __WIFI_CONNECT_H__ */

src/wifi_connect.c

/*
 * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifi_device.h"
#include "wifi_error_code.h"

#include "lwip/api_shell.h"
#include "lwip/ip4_addr.h"
#include "lwip/netif.h"
#include "lwip/netifapi.h"

#define DEF_TIMEOUT 15
#define ONE_SECOND 1
#define SELECT_WIFI_SECURITYTYPE WIFI_SEC_TYPE_PSK
#define SELECT_WLAN_PORT "wlan0"

WifiErrorCode errorCode;
WifiEvent wifiEventHandler = {0};

static int ssid_count = 0;
static int staScanSuccess = 0;
static int staConnectSuccess = 0;

/* WiFi 站点改变扫描的回调函数 */
static void OnWifiScanStateChangedCallBack(int state, int size)
{
    if (size > 0)
    {
        ssid_count = size;
        staScanSuccess = 1;
    }
    printf("WiFi CallBack Function Is Success Scan:%d ,%d.\r\n", state, size);
    return;
}

/* WiFi 连接改变的回调函数 */
static void OnWifiConnectionChangedCallBack(int state, WifiLinkedInfo *info)
{
    if (info == NULL)
    {
        printf("OnWifiConnectionChangedCallBack: info is null state is %d.\r\n", state);
    }
    else
    {
        if (state == WIFI_STATE_AVALIABLE)
        {
            staConnectSuccess = 1;
        }
        else
        {
            staConnectSuccess = 0;
        }
    }
}

/* WiFi 加入站点的回调函数 */
static void OnHotspotStaJoinCallBack(StationInfo *info)
{
    (void)info;
    printf("OnHotspotStaJoinCallBack: Sta Join AP.\r\n");
    return;
}

/* WiFi 离开站点的回调函数 */
static void OnHotspotStaLeaveCallBack(StationInfo *info)
{
    (void)info;
    printf("OnHotspotStaLeaveCallBack: info is null.\r\n");
    return;
}

/* WiFi 状态改变的回调函数 */
static void OnHotspotStateChangedCallBack(int state)
{
    printf("OnHotspotStateChangedCallBack: state is %d.\r\n", state);
    return;
}

/* 初始化WIFI */
static void wifiInit(void)
{
    printf(">>>WiFi Init Start ......\r\n");

    /** Scan state change */
    // void (*OnWifiScanStateChanged)(int state, int size);
    wifiEventHandler.OnWifiScanStateChanged = OnWifiScanStateChangedCallBack;

    /** Connection state change */
    // void (*OnWifiConnectionChanged)(int state, WifiLinkedInfo* info);
    wifiEventHandler.OnWifiConnectionChanged = OnWifiConnectionChangedCallBack;

    /** Station connected */
    // void (*OnHotspotStaJoin)(StationInfo* info);
    wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinCallBack;

    /** Station disconnected */
    // void (*OnHotspotStaLeave)(StationInfo* info);
    wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveCallBack;

    /** Hotspot state change */
    // void (*OnHotspotStateChanged)(int state);
    wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedCallBack;

    // 注册 WiFi 事件
    /**
     * @brief Registers a callback for a specified Wi-Fi event.
     *
     * The registered callback will be invoked when the Wi-Fi event defined in {@link WifiEvent} occurs. \n
     *
     * @param event Indicates the event for which the callback is to be registered.
     * @return Returns {@link WIFI_SUCCESS} if the callback is registered successfully; returns an error code defined
     * in {@link WifiErrorCode} otherwise.
     * @since 1.0
     * @version 1.0
    */
    // WifiErrorCode RegisterWifiEvent(WifiEvent* event);
    errorCode = RegisterWifiEvent(&wifiEventHandler);
    if (errorCode == WIFI_SUCCESS)
    {
        printf("Register WiFi Event Successed!!!\r\n");
    }
    else
    {
        printf("Register WiFi Event Falied And ErrorCode is %d.\r\n", errorCode);
    }
}

/* 等待扫描结果的函数 */
static void waitScanResult(void)
{
    int scanTimeOut = DEF_TIMEOUT;
    while (scanTimeOut > 0)
    {
        sleep(ONE_SECOND);
        scanTimeOut--;
        if (staScanSuccess == 1)
        {
            printf("waitScanResult: WiFi Scan Result Is Success[%d]s.\r\n", (DEF_TIMEOUT - scanTimeOut));
            break;
        }
    }
    if (scanTimeOut <= 0)
    {
        printf("waitScanResult: WiFi Scan Result TimeOut.\r\n");
    }
}

/* 等待连接结果的函数 */
static int waitConnectResult(void)
{
    int connectTimeOut = DEF_TIMEOUT;
    while (connectTimeOut > 0)
    {
        sleep(ONE_SECOND);
        connectTimeOut--;
        if (staConnectSuccess == 1)
        {
            printf("waitConnectResult: WiFi Connect Result Is Success[%d]s.\r\n", (DEF_TIMEOUT - connectTimeOut));
            break;
        }
    }
    if (connectTimeOut <= 0)
    {
        printf("waitConnectResult: WiFi Connect TimeOut.\r\n");
        return 0;
    }
    return 1;
}

/* 连接 WIFI 的API接口 */
int wifiConnect(const char *ssid, const char *psk)
{
    WifiScanInfo *info = NULL;
    unsigned int size = WIFI_SCAN_HOTSPOT_LIMIT;
    static struct netif *lwipNetIf = NULL;

    // 延迟2s连接
    osDelay(200);
    printf(">>>System Init WiFi ......\r\n");

    // 初始化WIFI
    wifiInit();

    // 使能WiFi
    /**
     * @brief Enables the station mode.
     *
     * @return Returns {@link WIFI_SUCCESS} if the station mode is enabled; returns an error code defined in
     * {@link WifiErrorCode} otherwise.
     * @since 1.0
     * @version 1.0
    */
    // WifiErrorCode EnableWifi(void);
    if (EnableWifi() != WIFI_SUCCESS)
    {
        printf("Enable WiFi Falied, Error Code Is %d.\r\n", errorCode);
        return -1;
    }

    // 判断WiFi 是否激活
    /**
     * @brief Checks whether the station mode is enabled.
     *
     * @return Returns {@link WIFI_STA_ACTIVE} if the station mode is enabled; returns {@link WIFI_STA_NOT_ACTIVE}
     * otherwise.
     * @since 1.0
     * @version 1.0
    */
    // int IsWifiActive(void);
    if (IsWifiActive() != WIFI_STA_ACTIVE)
    {
        printf("WiFi Station Is No Active.\r\n");
        return -1;
    }

    // 分配内存空间,保存WiFi信息
    info = malloc(sizeof(WifiScanInfo) * WIFI_SCAN_HOTSPOT_LIMIT);
    if (info == NULL)
    {
        return -1;
    }

    // 轮询查找WiFi列表
    do
    {
        // 重置标志
        ssid_count = 0;
        staScanSuccess = 0;

        // 开始扫描
        /**
         * @brief Starts a Wi-Fi scan.
         *
         * @return Returns {@link WIFI_SUCCESS} if the Wi-Fi scan is started; returns an error code defined in
         * {@link WifiErrorCode} otherwise.
         * @since 1.0
         * @version 1.0
        */
        // WifiErrorCode Scan(void);
        Scan();

        // 等待扫描结果
        waitScanResult();

        // 获取扫描结果列表
        /**
         * @brief Obtains an array of hotspots detected in a Wi-Fi scan.
         *
         * The array of hotspots can be obtained only after the Wi-Fi scan is complete. \n
         *
         * @param result Indicates the array of hotspots detected in a Wi-Fi scan. The array is requested and released by the
         * caller. The value must be greater than or equal to {@link WIFI_SCAN_HOTSPOT_LIMIT}.
         * @param size Indicates the size of the array.
         * @return Returns {@link WIFI_SUCCESS} if the array of hotspots detected in the Wi-Fi scan is obtained; returns an
         * error code defined in {@link WifiErrorCode} otherwise.
         * @since 1.0
         * @version 1.0
        */
        // WifiErrorCode GetScanInfoList(WifiScanInfo* result, unsigned int* size);
        errorCode = GetScanInfoList(info, &size);
    } while (staScanSuccess != 1);

    // 打印WiFi信息列表
    printf("**********YuchuanHuaying WiFi Info List Start**********\r\n");
    for (uint8_t i = 0; i < ssid_count; i++)
    {
        printf("no:%03d, ssid:%-30s, rssi:%5d\r\n", i + 1, info[i].ssid, info[i].rssi / 100);
    }
    printf("******************YuchuanHuaying End*******************\r\n");

    // 连接指定的WiFi热点
    for (uint8_t i = 0; i < ssid_count; i++)
    {
        if (strcmp(ssid, info[i].ssid) == 0)
        {
            int result;
            printf("Select:%3d wireless, Waiting...\r\n", i + 1);

            // 拷贝要连接的热点信息
            WifiDeviceConfig selectWiFiAPConfig = {0};
            strcpy(selectWiFiAPConfig.ssid, info[i].ssid);
            strcpy(selectWiFiAPConfig.preSharedKey, psk);
            selectWiFiAPConfig.securityType = SELECT_WIFI_SECURITYTYPE;
            /**
             * @brief Adds a specified hotspot configuration for connecting to a hotspot.
             *
             * This function generates a <b>networkId</b>. \n
             *
             * @param config Indicates the hotspot configuration to add.
             * @param result Indicates the generated <b>networkId</b>. Each <b>networkId</b> matches a hotspot configuration.
             * @return Returns {@link WIFI_SUCCESS} if the specified hotspot configuration is added; returns an error code defined
             * in {@link WifiErrorCode} otherwise.
             * @since 1.0
             * @version 1.0
            */
            // WifiErrorCode AddDeviceConfig(const WifiDeviceConfig* config, int* result);
            if (AddDeviceConfig(&selectWiFiAPConfig, &result) == WIFI_SUCCESS)
            {
                /**
                 * @brief Connects to a hotspot matching a specified <b>networkId</b>.
                 *
                 * Before calling this function, call {@link AddDeviceConfig} to add a hotspot configuration. \n
                 *
                 * @param networkId Indicates the <b>networkId</b> matching the target hotspot.
                 * @return Returns {@link WIFI_SUCCESS} if the hotspot is connected; returns an error code defined in
                 * {@link WifiErrorCode} otherwise.
                 * @since 1.0
                 * @version 1.0
                */
                // WifiErrorCode ConnectTo(int networkId);
                if (ConnectTo(result) == WIFI_SUCCESS && waitConnectResult() == 1)
                {
                    printf("WiFi Connect Successed.\r\n");
                    /**
                    * @ingroup Threadsafe_Network_Interfaces
                    * @brief
                    *  This thread-safe interface is called when to find interface
                    */
                    // struct netif *netifapi_netif_find(const char *name);
                    lwipNetIf = netifapi_netif_find(SELECT_WLAN_PORT);
                    break;
                }
            }
        }
        if (i == ssid_count - 1)
        {
            printf("Error: No WiFi as expected.\r\n");
            while (1)
                osDelay(100);
        }
    }

    // 启动DHCP
    if (lwipNetIf)
    {
        dhcp_start(lwipNetIf);
        printf("Begain To DHCP.\r\n");
    }

    // 等待DHCP
    for (;;)
    {
        if (dhcp_is_bound(lwipNetIf) == ERR_OK)
        {
            printf("<--YuchuanHuaying DHCP state:OK -->\r\n");

            // 打印获取到的IP信息
            /**
             * @ingroup Threadsafe_Network_Interfaces
             * @brief
             *  This API is used to call all netif related APIs in a thread safe manner. The netif related APIs must be
             *  of prototype to receive only struct netif* as argument and return type can of type err_t or void. You
             *  must pass either viodfunc or errtfunc.
             *
             * @param[in]    netif          Indicates the network interface to be passed as argument.
             * @param[in]    voidfunc       Callback with return type of void, will be called if errtfunc is NULL.
             * @param[in]    errtfunc       Callback with return type of err_t.
             *
             * @returns
             *  0 : On success. \n
             *  Negative value : On failure.
             *
             * @note
             * The prototype for netifapi_void_fn and netifapi_errt_fn are as follows:\n
             * typedef void (*netifapi_void_fn)(struct netif *netif); \n
             * typedef err_t (*netifapi_errt_fn)(struct netif *netif); \n
            */
            // err_t netifapi_netif_common(struct netif *netif, netifapi_void_fn voidfunc,netifapi_errt_fn errtfunc);
            netifapi_netif_common(lwipNetIf, dhcp_clients_info_show, NULL);
            break;
        }
        printf("<--YuchuanHuaying DHCP state:Inprogress -->\r\n");
        osDelay(100);
    }

    osDelay(100);

    return 0;
}

编译调试

//applications/sample/BearPi/BearPi-HM_Nano/sample/D3_YuchuanIoTUDPClient/BUILD.gn

static_library("yuchuanUDPClient"){
    sources = [
        "yuchuan_udp_client.c",
        "src/wifi_connect.c",
    ]

    include_dirs = [
        "//utils/native/lite/include",
        "//kernel/liteos_m/kal/cmsis",
        "//foundation/communication/wifi_lite/interfaces/wifiservice",
        "//device/bearpi/bearpi_hm_nano/sdk_liteos/third_party/lwip_sack/include",
        "include",
    ]
}

修改 BUILD.gn 文件

修改 applications\BearPi\BearPi-HM_Nano\sample 路径下 BUILD.gn 文件,指定 yuchuanUDPClient 参与编译。

# Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build/lite/config/component/lite_component.gni")

lite_component("sample") {
    features = [
        #"A1_kernal_thread:thread_example",
        #"A2_kernel_timer:timer_example",
        #"A3_kernel_event:event_example",
        #"A4_kernel_mutex:mutex_example",
        #"A5_kernel_semaphore:semaphore_example",
        #"A6_kernel_message:message_example",

        #"B1_basic_led_blink:led_example",
        #"B2_basic_button:button_example",
        #"B3_basic_pwm_led:pwm_example",
        #"B4_basic_adc:adc_example", 
        #"B5_basic_i2c_nfc:i2c_example",
        #"B6_basic_uart:uart_example",
        
        #"C1_e53_sf1_mq2:e53_sf1_example",
        #"C2_e53_ia1_temp_humi_pls:e53_ia1_example",
        #"C3_e53_sc1_pls:e53_sc1_example",
        #"C4_e53_sc2_axis:e53_sc2_example",
        #"C5_e53_is1_infrared:e53_is1_example",

        #"D1_iot_wifi_ap:wifi_ap",
        #"D2_iot_wifi_sta_connect:wifi_sta_connect",        
        #"D3_iot_udp_client:udp_client",
        #"D4_iot_tcp_server:tcp_server",
        #"D5_iot_mqtt:iot_mqtt",        
        #"D6_iot_cloud_oc:oc_mqtt",
        #"D7_iot_cloud_onenet:onenet_mqtt",
        #"D8_iot_cloud_oc_smoke:cloud_oc_smoke",
        #"D9_iot_cloud_oc_light:cloud_oc_light",
        #"D10_iot_cloud_oc_manhole_cover:cloud_oc_manhole_cover",
        #"D11_iot_cloud_oc_infrared:cloud_oc_infrared",
        #"D12_iot_cloud_oc_agriculture:cloud_oc_agriculture",
        #"D13_iot_cloud_oc_gps:cloud_oc_gps",
        "D3_YuchuanIoTUDPClient:yuchuanUDPClient",
    ]
}

运行结果

使用 Socket tool 创建UDP服务端用于测试。

ready to OS start
sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
FileSystem mount ok.
wifi init success!

hiview init success.00 00:00:00 0 132 D 0/HIVIEW: log limit init success.
00 00:00:00 0 132 I 1/SAMGR: Bootstrap core services(count:3).
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b1248 TaskPool:0xe503c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b1254 TaskPool:0xe505c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b1720 TaskPool:0xe507c
00 00:00:00 0 164 I 1/SAMGR: Init service 0x4b1254 <time: 0ms> success!
00 00:00:00 0 64 I 1/SAMGR: Init service 0x4b1248 <time: 10ms> success!
00 00:00:00 0 8 I 1/SAMGR: Init service 0x4b1720 <time: 10ms> success!
00 00:00:00 0 8 I 1/SAMGR: Initialized all core system services!
00 00:00:00 0 64 I 1/SAMGR: Bootstrap system and application services(count:0).
00 00:00:00 0 64 I 1/SAMGR: Initialized all system and application services!
00 00:00:00 0 64 I 1/SAMGR: Bootstrap dynamic registered services(count:0).
>>>System Init WiFi ......
>>>WiFi Init Start ......
Register WiFi Event Successed!!!
WiFi CallBack Function Is Success Scan:0 ,0.
+NOTICE:SCANFINISH
WiFi CallBack Function Is Success Scan:1 ,24.
waitScanResult: WiFi Scan Result Is Success[1]s.
**********YuchuanHuaying WiFi Info List Start**********
no:001, ssid:886699                        , rssi:  -36
no:002, ssid:DIRECT-hnB63K7H2msYI          , rssi:  -50
no:003, ssid:Warehouse                     , rssi:  -66
no:004, ssid:Warehouse                     , rssi:  -67
no:005, ssid:Warehouse                     , rssi:  -77
no:006, ssid:Packing                       , rssi:  -77
no:007, ssid:Warehouse                     , rssi:  -80
no:008, ssid:Voyager                       , rssi:  -80
no:009, ssid:Warehouse                     , rssi:  -80
no:010, ssid:WIFI_Network                  , rssi:  -81
no:011, ssid:Mobile-WiFi                   , rssi:  -82
no:012, ssid:Mobile-WiFi                   , rssi:  -83
no:013, ssid:WIFI_Network                  , rssi:  -84
no:014, ssid:Warehouse                     , rssi:  -85
no:015, ssid:Voyager                       , rssi:  -87
no:016, ssid:Warehouse                     , rssi:  -88
no:017, ssid:We.Connect                    , rssi:  -88
no:018, ssid:WIFI_Network                  , rssi:  -90
no:019, ssid:Mobile-WiFi                   , rssi:  -91
no:020, ssid:306C2                         , rssi:  -95
no:021, ssid:LightTouch                    , rssi:  -80
no:022, ssid:Openspace                     , rssi:  -82
no:023, ssid:                              , rssi:  -86
no:024, ssid:Openspace                     , rssi:  -88
******************YuchuanHuaying End*******************
Select:  1 wireless, Waiting...
+NOTICE:CONNECTED
waitConnectResult: WiFi Connect Result Is Success[1]s.
WiFi Connect Successed.
Begain To DHCP.
<--YuchuanHuaying DHCP state:Inprogress -->
<--YuchuanHuaying DHCP state:OK -->
server :
	server_id : 192.168.137.1
	mask : 255.255.255.0, 1
	gw : 192.168.137.1
	T0 : 604800
	T1 : 302400
	T2 : 453600
clients <1> :
	mac_idx mac             addr            state   lease   tries   rto     
	0       6c11318b19c8    192.168.137.11  10      0       1       4       
101.11.233.133:8888=>YuchuanHuaying Hello Word!!!

创建UDP服务端

示例代码编译烧录代码后,按下开发板的RESET按键,在数据发送窗口输入要发送的数据,点击发送后开发板会回复固定消息,且开发板收到消息后会通过日志打印出来。

101.11.233.133:8888=>YuchuanHuaying Hello Word!!!

(完)