1.简介
在#开发板漂流计划#小车控制由简入繁之按键控制的基础上,实现小车上电后自动连接到指定WIFI,并建立UDP Server监听指定端口数据。电脑端作为UDP Client 连接到小车对应的端口,通过发送字符串来控制小车状态。
以下代码基于OpenHarmony-v3.0-LTS编译测试。
2.WIFI连接的实现
wifi连接的代码使用了润和Gitee中的wifi_connecter.c和wifi_connecter.h将这两个文件分别放到car目录下的src和include中,修改Car 目录下BUILD.gn和car_main.c添加如下代码,详细修改说明如下:
2.1.在applications\sample\wifi-iot\app\car\BUILD.gn中添加sources中添加编译wifi_connecter.c
- static_library("car") {
- sources = [
- ......
- "src/wifi_connecter.c",
- ]
- }
2.2.在applications\sample\wifi-iot\app\car\BUILD.gn中include_dirs 里面添加//applications/sample/wifi-iot/app/car/include这样wifi_connecter.h等就可以包到了,另外因为wifi_connecter.h中有用到wifi_device.h所以需要再把//foundation/communication/wifi_lite/interfaces/wifiservice加入。wifi_connecter.h中的"lwip/netifapi.h"和 “lwip/api_shell.h” 是在"//device/hisilicon/hispark_pegasus/sdk_liteos/third_party/lwip_sack/include"中也需要,所以需要添加以下路径。
- include_dirs = [
- ......
- "//applications/sample/wifi-iot/app/car/include",
- "//foundation/communication/wifi_lite/interfaces/wifiservice",
- "//device/hisilicon/hispark_pegasus/sdk_liteos/third_party/lwip_sack/include",
- ]
2.3 在car_main.c中参考如下修改,将wifi_connecter.h加入include,"SSIDABCD"改成你要连接的WIFI SSID, "MIMA1234"填入WIFI密码。
- #include "wifi_connecter.h"
- ......
- static void CarDemoTask(void *arg)
- {
- ......
- // setup your AP params
- WifiDeviceConfig apConfig = {0};
- strcpy(apConfig.ssid, "SSIDABCD");
- strcpy(apConfig.preSharedKey, "MIMA1234");
- apConfig.securityType = WIFI_SEC_TYPE_PSK;
- int netId = ConnectToHotspot(&apConfig);
- printf("[CarDemo] ConnectToHotspot done netId=%d!\n",netId);
- }
securityType 的enum 如下按照你的WIFI设定的加密方式来选择,
- typedef enum {
- /** Invalid security type */
- WIFI_SEC_TYPE_INVALID = -1,
- /** Open */
- WIFI_SEC_TYPE_OPEN,
- /** Wired Equivalent Privacy (WEP) */
- WIFI_SEC_TYPE_WEP,
- /** Pre-shared key (PSK) */
- WIFI_SEC_TYPE_PSK,
- /** Simultaneous Authentication of Equals (SAE) */
- WIFI_SEC_TYPE_SAE,
- } WifiSecurityType;
通过ConnectToHotspot()就可以轻松的连接到指定WIFI了。
3.UDP Server的实现
3.1 UDP Server的实现代码
在hispark_pegasus中可以使用"//device/hisilicon/hispark_pegasus/sdk_liteos/third_party/lwip_sack/include"(该目录在WIFI连接的实现中已添加)中的"lwip/sockets.h"来实现,代码如下
- #include "lwip/sockets.h"
- static char response[] = "\nSucess.\n";
- static char message[128] = "";
- void UdpServer(unsigned short port)
- {
- ssize_t retval = 0;
- int needfb = 0;
- int sockfd = socket(AF_INET, SOCK_DGRAM, 0); // UDP socket
- struct sockaddr_in clientAddr = {0};
- socklen_t clientAddrLen = sizeof(clientAddr);
- struct sockaddr_in serverAddr = {0};
- serverAddr.sin_family = AF_INET;
- serverAddr.sin_port = htons(port);
- serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- retval = bind(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
- if (retval < 0) {
- printf("bind failed, %ld!\r\n", retval);
- goto do_cleanup;
- }
- printf("bind to port %d success!\r\n", port);
- while(1)
- {
- needfb = 0;
- memset(message, 0, sizeof(message));
- retval = recvfrom(sockfd, message, sizeof(message), 0, (struct sockaddr *)&clientAddr, &clientAddrLen);
- if (retval > 0) {
- printf("recv message {%s} %ld done!\r\n", message, retval);
- printf("peer info: ipaddr = %s, port = %d\r\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));
- if(strncmp("forward", message, 7)== 0) {
- needfb = 1;
- car_go_forward();
- }
- if(strncmp("back", message, 4) == 0) {
- needfb = 1;
- car_go_back();
- }
- if(strncmp("left", message, 4) == 0) {
- needfb = 1;
- car_turn_left();
- }
- if(strncmp("right", message, 5) == 0) {
- needfb = 1;
- car_turn_right();
- }
- if(strncmp("stop", message, 4) == 0) {
- needfb = 1;
- car_stop();
- }
- if(needfb == 1) {
- retval = sendto(sockfd, response, strlen(response), 0, (struct sockaddr *)&clientAddr, sizeof(clientAddr));
- if (retval > 0) {
- printf("send response {%s} %ld done!\r\n", response, retval);
- } else {
- printf("send failed, %ld!\r\n", retval);
- }
- }
- }
- }
- do_cleanup:
- printf("do_cleanup...\r\n");
- close(sockfd);
- }
需要注意的是在每次检查数据recvfrom()前memset(message, 0, sizeof(message))清一下之前的数据。
在收到"forward"、“back”、“left”、“right”、“stop"后会执行相应的小车控制函数,并回复"Sucess”。
3.2 UDP Server的调用
在Task 最后面调用UdpServer()传入端口函数即可,这里端口使用62021
- static void CarDemoTask(void *arg)
- {
- ......
- UdpServer(62021);
- printf("[CarDemo] create CarDemoTask!\n");
- }
4.编译
4.1 将附件car.zip 解压后放到applications\sample\wifi-iot\app\下,如下图
4.2 修改applications/sample/wifi-iot/app/BUILD.gn
- import("//build/lite/config/component/lite_component.gni")
- lite_component("app") {
- features = [
- "car",
- ]
- }
4.3 电机的控制需要用到PWM,所以需要先将PWM 功能开启,开启方式如下
device/hisilicon/hispark_pegasus/sdk_liteos/build/config/usr_config.mk
- # CONFIG_PWM_SUPPORT is not set
改为
- CONFIG_PWM_SUPPORT=y
4.4 进入代码根目录执行hb set输入.(当前目录)并选择wifiiot_hispark_pegasus,执行 hb build -b release -f
- soon@soon-u20:~/ohos300_iot $ hb set
- [OHOS INFO] Input code path: .
- OHOS Which product do you need? wifiiot_hispark_pegasus
- soon@soon-u20:~/ohos300_iot $ hb build -b release -f
4.5 使用HiBurn或者Visual Studio Code烧录,可参考
5.功能测试
5.1 从串口日志获取小车IP,如下图成功连接WIFI获取IP 打印如下,如我这边获取的IP为192.168.123.247
5.2 电脑端测试软件如附件SocketTool2.zip ,解压后直接运行按下图步骤创建UDP Client,
建立连接后就可以在数据发送窗口中发送文本数据"forward"、“back”、“left”、“right”、"stop"来控制小车了,小车接受成功后回复Sucess如下图。
6.总结
本案例实现了一个简单的UDP控制小车的Demo,但是缺少状态反馈,如WIFI是否连接成功,连接成功后IP的显示,这一部分读者可以再利用OLED屏幕来完善。
文中相关设备来源于51CTO 鸿蒙技术社区【开发板漂流计划】
https://harmonyos.51cto.com/resource/1289
https://harmonyos.51cto.com/resource/1290