文中相关设备来源于51CTO 鸿蒙技术社区【开发板漂流计划】
开发板型号为[润和蓝牙开发板]
,搭载Neptune模组
,w800芯片
,开发板相关介绍略。
感谢51cto |
和润和的支持~ |
 |
 |
注:本文只进行功能介绍与代码实现,具体原理见设计文档(点此跳转)
(:使用neptune开发板参加了一个小比赛
效果演示


视频见附件
实现了语音操控开、关灯
实现方法简介
我们知道浏览器是http的客户端,目的是连接远程的http服务器,然后服务器返回浏览器数据.浏览器接收数据解析数据之后展现出来.我们看到的外在表现就是,浏览器访问一个url,然后就得到相应的web页面.
所以我们需要在Neptune上部署一个http服务器。
为了实现tcp通信,这里使用socket库搭建,代码如下:
文件见附件(可直接编译,由于某些原因智能在ohos1.1版本下运行,后续会更新ohos3.x版本)
代码实现
tcp_service_demo1
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "net_demo_common.h"
#include "net_common.h"
#include "cmsis_os2.h"
#include "ohos_init.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include "wifiiot_gpio_w800.h"
#define LED_TASK_STACK_SIZE 1024
#define LED_TASK_PRIO 25
static void my_demo()
{
printf("\n\n\nhello!\n\n\n\n");
GpioInit();
GpioSetDir(WIFI_IOT_GPIO_PB_08, WIFI_IOT_GPIO_DIR_OUTPUT);
GpioSetDir(WIFI_IOT_GPIO_PB_09, WIFI_IOT_GPIO_DIR_INPUT);
IoSetPull(WIFI_IOT_GPIO_PB_09, WIFI_IOT_GPIO_ATTR_PULLHIGH);
GpioRegisterIsrFunc(WIFI_IOT_GPIO_PB_09, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW, NULL, NULL);
GpioSetOutputVal(WIFI_IOT_GPIO_PB_08, WIFI_IOT_GPIO_VALUE0);
printf("\n\ndone\n\n\n\n");
}
static char request[128] = "";
void TcpServerTest(unsigned short port)
{
printf("*********************TCP server test *****************\n");
int retval = 0;
int backlog = 1;
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
int connfd = -1;
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);
retval = listen(sockfd, backlog);
if (retval < 0) {
printf("listen failed!\r\n");
goto do_cleanup;
}
printf("listen with %d backlog success!\r\n", backlog);
connfd = accept(sockfd, (struct sockaddr *)&clientAddr, &clientAddrLen);
if (connfd < 0) {
printf("accept failed, %d, %d\r\n", connfd, errno);
goto do_cleanup;
}
printf("accept success, connfd = %d!\r\n", connfd);
printf("client addr info: host = %s, port = %d\r\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));
my_demo();
const char buffer[9162]="HTTP/1.1 200 OK\r\nDate: Fri, 22 May 2009 06:07:21 GMT\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n<html><head></head><body style="text-align:center;font-size: larger;"><h1>Hello Openharmony!</h1><div style="background-color:green ;height: 20%;color:white;">开灯</div><div style="background-color:red ;height: 20%;">关灯</div></body></html>";
send(connfd,buffer,sizeof(buffer),0);
retval = recv(connfd, request, sizeof(request), 0);
if (retval < 0) {
printf("recv request failed, %ld!\r\n", retval);
goto do_disconnect;
}
printf("recv request{%s} from client done!\r\n", request);
retval = send(connfd, request, strlen(request), 0);
if (retval <= 0) {
printf("send response failed, %ld!\r\n", retval);
goto do_disconnect;
}
printf("send response{%s} to client done!\r\n", request);
do_disconnect:
osDelay(500);
close(connfd);
osDelay(500);;
do_cleanup:
printf("do_cleanup...\r\n");
close(sockfd);
}
SERVER_TEST_DEMO(TcpServerTest);
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
要让开发板连接网络,需要在 net_params.h里修改配网参数
修改my_demo/Build.gn
修改build.gn:
666,给你点赞