非常惭愧由于前段时间太忙到最近才开始鼓捣Hi3861,首先感谢下乔帮主、连老师等大牛的优质输出,真乃大神种树后人乘凉啊。
简要记录一下搭建过程的注意事项和tcp client demo。
编译环境的搭建,在WIN10上,利用WSL,配合docker简直不要太爽。具体安装方式官方有详细步骤,这里不再赘述,docker环境可以参考docker无忧包安装,非常便捷。
编译环境和代码环境准备好后,用 python build.py wifiiot 就可执行代码编译。docker环境下非首次编译基本就几十秒就编完。
烧录环境需要注意的几个事项
一、hpm无法加载文件问题:
以管理员身份运行vscode;
执行:get-ExecutionPolicy,显示Restricted,表示状态是禁止的;
执行:set-ExecutionPolicy RemoteSigned;
这时再执行get-ExecutionPolicy,就显示RemoteSigned;
二、MODULE_NOT_FOUND问题:
更改bundle.json中的%UPLOAD_SCRIPT%
三、固件不存在问题:
修改配置中烧录文件路径和烧录方式
等到下面打印出现说明烧录完成
可以使用串口工具看下Hi3861的启动是否正常
在看到wifi init success打印后在串口使用AT指令测试连通性
到此,所有环境搭建验证完成。可以愉快的撸代码了。
接下来简单的用python在PC端搭建了一个tcp server,在Hi3861上搭建了tcp client,PC上通过socket发送指令给Hi3861,控制LED灯的亮灭。下面是简单的代码摘要。
- int tcp_client_demo(void)
-
- {
-
-
-
- struct sockaddr_in server_addr;
-
- ssize_t ret;
-
- printf("%s %d \r\n", __FILE__, __LINE__);
-
- sleep(10);
-
-
-
- if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
-
- {
-
- printf("%s %d \r\n", __FILE__, __LINE__);
-
- perror("socket is error\r\n");
-
- exit(1);
-
- }
-
-
-
- server_addr.sin_family = AF_INET;
-
- server_addr.sin_port = htons(50007);
-
- server_addr.sin_addr.s_addr = inet_addr("192.168.1.101");
-
- printf("%s %d \r\n", __FILE__, __LINE__);
-
-
-
- if(connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
-
- {
-
- printf("%s %d \r\n", __FILE__, __LINE__);
-
- perror("connect is error\r\n");
-
- exit(1);
-
- }
-
- printf("%s %d \r\n", __FILE__, __LINE__);
-
-
-
- while(1)
-
- {
-
- if((ret = recv(sock_fd, recvbuf, sizeof(recvbuf), 0)) == -1){
-
- printf("recv error \r\n");
-
- return -1;
-
- }
-
- printf("recv :\r\n");
-
- printf("%s", recvbuf);
-
- printf("\r\n");
-
- if(recvbuf[0] == '1')
-
- {
-
-
-
- g_ledState = LED_ON;
-
- printf("LED turned ON\n");
-
- sprintf(sendbuf, "LED turned ON");
-
- }
-
- else if(recvbuf[0] == '0')
-
- {
-
-
-
- g_ledState = LED_OFF;
-
- printf("LED turned OFF\n");
-
- sprintf(sendbuf, "LED turned OFF");
-
- }
-
- else
-
- {
-
- printf("Invalid command\n");
-
- sprintf(sendbuf, "Invalid command");
-
- }
-
- if((ret = send(sock_fd, sendbuf, strlen(sendbuf) + 1, 0)) == -1)
-
- {
-
- perror("send : ");
-
- }
-
- sleep(2);
-
- }
-
- close(sock_fd);
-
- return 0;
-
- }
下面是python server的相关代码摘要。
- def echo_server(host, port):
-
- print("==========================")
-
- print("TCP Server")
-
- print("==========================")
-
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-
- s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
-
- try:
-
- s.bind((host, port))
-
- s.listen(1)
-
- except socket.error as msg:
-
- print("ERROR: ", msg)
-
- s.close()
-
- s = None
-
- if s is None:
-
- sys.exit(1)
-
- while 1:
-
- print("Listening on: %s:%d"%(host, port))
-
- data_len = 0
-
- try:
-
- conn, addr = s.accept()
-
- except KeyboardInterrupt:
-
- print("Closing Connection")
-
- s.close()
-
- s = None
-
- sys.exit(1)
-
- print('Incoming connection accepted: ', addr)
-
- try:
-
- while 1:
-
- data = input("Enter your option: '1' to turn ON LED, '0' to trun OFF LED and Press the 'Enter' key: ")
-
- conn.send(data.encode())
-
- data = conn.recv(4096)
-
- if not data: break
-
- print("Acknowledgement from TCP Client:", data.decode('utf-8'))
-
- print("")
-
- except KeyboardInterrupt:
-
- print("Closing Connection")
-
- s.close()
-
- s = None
-
- sys.exit(1)
-
- conn.close()
最终实现的效果如下:
相关代码已经打包上传,希望接下来能继续用鸿蒙做出一些有趣的事情!
©著作权归作者和HarmonyOS技术社区共同所有,如需转载,请注明出处,否则将追究法律责任。