1,首先要获得hisi3861芯片的wifi开发的相关接口和文档:
https://bbs.elecfans.com/jishu_2028821_1_1.html
这里就有相关的资料
2.liteos_m文档库里面也有好多资料,在doc文件夹里
3.首先需要连接wifi,可以参考文档:Hi3861V100/Hi3861LV100 Wi-Fi软件 开发指南.pdf
4.找到sta模式,里面讲的很清楚,基于实际需求,把流程进行简化:
STA功能开发的典型流程:
步骤1 调用hi_wifi_sta_start,启动STA。
步骤2 调用hi_wifi_sta_scan,hi_wifi_sta_scan_results,获取扫描结果。
步骤3 根据接入网络需求,自定义筛选扫描结果,调用hi_wifi_sta_connect,进行连接。
步骤4 调用hi_wifi_sta_get_connect_info,查询Wi-Fi连接状态。
步骤5 连接成功后,调用ne
tifapi_dhcp_start,启动DHCP客户端,获取IP地址。
步骤6 调用hi_wifi_sta_disconnect ,离开当前连接的网络。
步骤7 调用netifapi_dhcps_stop,停止DHCP客户端。
步骤8 调用hi_wifi_sta_stop ,关闭STA。
----结束,注意5,6,7步骤不是必须的
5.在连接成功的毁掉开启dhcp获取ip地址之后并不会立马得到ip地址,所以我们开启一个线程不停得在等待拿到ip地址:
- case HI_WIFI_EVT_CONNECTED:
- printf("WiFi: Connectedn");
- printf("connected info: %s %sn", hisi_event->info.wifi_connected.ssid, hisi_event->info.wifi_connected.ifname);
- netifapi_dhcp_start(g_lwip_netif);
- {
- osThreadAttr_t attr;
- attr.name = "netTask";
- attr.attr_bits = 0U;
- attr.cb_mem = NULL;
- attr.cb_size = 0U;
- attr.stack_mem = NULL;
- attr.stack_size = 2048;
- attr.priority = 24;
- if (osThreadNew((osThreadFunc_t)netTask, NULL, &attr) == NULL)
- {
- printf("[LedExample] Falied to create LedTask!n");
- }
- }
- break;
- static void *netTask(const char *arg)
- {
- printf("netTask:%pn", arg);
- u8_t ip_arr[4] = {0};
- u8_t ip_zero[4] = {0};
- // 下面这种方式也可以打印 IP、网关、子网掩码信息
- ip4_addr_t ip = {0};
- ip4_addr_t netmask = {0};
- ip4_addr_t gw = {0};
- int ret = 0;
- do
- {
- sleep(1);
- ret = netifapi_netif_get_addr(g_lwip_netif, &ip, &netmask, &gw);
- printf("netifapi_netif_get_addr: %drn", ret);
- if (ret == ERR_OK)
- {
- printf("ip = %srn", ip4addr_ntoa(&ip));
- printf("netmask = %srn", ip4addr_ntoa(&netmask));
- printf("gw = %srn", ip4addr_ntoa(&gw));
- memcpy(ip_arr, &ip.addr, sizeof(ip_arr));
- }
- } while (memcmp(ip_arr, ip_zero, sizeof(ip_arr)) == 0 || (ip_arr[0] != 192));
- printf("got ip :%d.%d.%d.%dn", ip_arr[0], ip_arr[1], ip_arr[2], ip_arr[3]);
- sample_tcp_client();
- return NULL;
- }
拿到ip地址之后我们的wifi才能段真正连接成功
6.之后就开启tcp客户端,相关代码 在demo_app里有,在文档:Hi3861V100/Hi3861LV100 lwIP 开发指南.pdf 里面也有,直接拷贝使用:
- int sample_tcp_client(void)
- {
- s32_t sfd = -1;
- struct sockaddr_in srv_addr = {0};
- // struct sockaddr_in cln_addr = {0};
- // socklen_t cln_addr_len = sizeof(cln_addr);
- s32_t ret = 0, i = 0;
- /* tcp client connection */
- printf("going to call socketn");
- sfd = socket(AF_INET, SOCK_STREAM, 0);
- if (sfd == -1)
- {
- printf("socket failed, return is %dn", sfd);
- goto FAILURE;
- }
- printf("socket succeeded, sfd %dn", sfd);
- srv_addr.sin_family = AF_INET;
- srv_addr.sin_addr.s_addr = inet_addr(PEER_IP);
- srv_addr.sin_port = htons(PEER_PORT);
- printf("going to call connectn");
- ret = connect(sfd, (struct sockaddr *)&srv_addr, sizeof(srv_addr));
- if (ret != 0)
- {
- printf("connect failed, return is %dn", ret);
- goto FAILURE;
- }
- printf("connec succeeded, return is %dn", ret);
- /* tcp client connection */
- /* send */
- memset(g_buf, 0, BUF_SIZE);
- strcpy((char *)g_buf, MSG);
- printf("calling send...n");
- ret = send(sfd, g_buf, sizeof(MSG), 0);
- if (ret <= 0)
- {
- printf("send failed, return is %d,i is %dn", ret, i);
- goto FAILURE;
- }
- printf("send finished ret is %dn", ret);
- /* send */
- /* recv */
- memset(g_buf, 0, BUF_SIZE);
- printf("going to call recvn");
- ret = recv(sfd, g_buf, sizeof(g_buf), 0);
- if (ret <= 0)
- {
- printf("recv failed, return is %dn", ret);
- goto FAILURE;
- }
- printf("recv succeeded, return is %dn", ret);
- printf("received msg is : %sn", g_buf);
- /* recv */
- lwip_close(sfd);
- return 0;
- FAILURE:
- lwip_close(sfd);
- // printf("errno is %dn", errno);
- return -1;
- }
7.在电脑上用一个tcp工具,开启一个tcp的服务器,如图:
8.把
开发板上电,程序下载进去,经过调试之后,正常就应该会连到电脑上的tcp服务器,然后并自动发送14个字节的数据,如图:
9.在电脑tcp服务器回复对应数据,可以在开发板终端上看到对应的数据,到此,本阶段实验结束,为后面的实验做铺垫.