【HarmonyOS开发板试用】在Neptune上搭建http服务器【FFH】 原创 精华

dancehole
发布于 2022-5-28 10:49
浏览
2收藏

文中相关设备来源于51CTO 鸿蒙技术社区【开发板漂流计划】
开发板型号为[润和蓝牙开发板],搭载Neptune模组,w800芯片,开发板相关介绍略。

感谢51cto 和润和的支持~
【HarmonyOS开发板试用】在Neptune上搭建http服务器【FFH】-鸿蒙开发者社区 【HarmonyOS开发板试用】在Neptune上搭建http服务器【FFH】-鸿蒙开发者社区

注:本文只进行功能介绍代码实现,具体原理见设计文档(点此跳转)
(:使用neptune开发板参加了一个小比赛

效果演示

【HarmonyOS开发板试用】在Neptune上搭建http服务器【FFH】-鸿蒙开发者社区
【HarmonyOS开发板试用】在Neptune上搭建http服务器【FFH】-鸿蒙开发者社区
视频见附件
实现了语音操控开、关灯

实现方法简介

我们知道浏览器是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);// output is 0 PB08 control led
    GpioSetDir(WIFI_IOT_GPIO_PB_09, WIFI_IOT_GPIO_DIR_INPUT);// input is PB09
        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); // TCP socket
    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); // 允许任意主机接入, 0.0.0.0

    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);

    // 接受客户端连接,成功会返回一个表示连接的 socket , clientAddr 参数将会携带客户端主机和端口信息 ;失败返回 -1
    // 此后的 收、发 都在 表示连接的 socket 上进行;之后 sockfd 依然可以继续接受其他客户端的连接,
    //  UNIX系统上经典的并发模型是“每个连接一个进程”——创建子进程处理连接,父进程继续接受其他客户端的连接
    //  鸿蒙liteos-a内核之上,可以使用UNIX的“每个连接一个进程”的并发模型
    //     liteos-m内核之上,可以使用“每个连接一个线程”的并发模型
    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();

    //send http pages
      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);

    // 后续 收、发 都在 表示连接的 socket 上进行;
    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);; // for debug

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里修改配网参数

#ifndef NET_PARAMS_H
#define NET_PARAMS_H

#define PARAM_HOTSPOT_SSID "FsrLab"  // your AP SSID
#define PARAM_HOTSPOT_PSK  "12345678"  // your AP PSK

#define PARAM_HOTSPOT_TYPE WIFI_SEC_TYPE_PSK // defined in wifi_device_config.h

#define PARAM_SERVER_ADDR "192.168.0.113" // your PC IP address
#define PARAM_SERVER_PORT 5678		//设置端口号

#endif  // NET_PARAMS_H
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

修改my_demo/Build.gn

static_library("demo") {
    sources = [
       "tcp_service_test.c"
    ]

    include_dirs = [
        "//utils/native/lite/include",
        "//kernel/liteos_m/components/cmsis/2.0",
        "//foundation/communication/interfaces/kits/wifi_lite/wifiservice",
                "//base/iot_hardware/interfaces/kits/wifiiot_lite",
    ]

    if (board_name == "w800" || board_name == "hi3861v100") {
        sources += ["wifi_connecter.c", "net_demo_ohos.c"]
    }

    if (board_name == "w800") {
        include_dirs += [
            "//vendor/winnermicro/w800/src/network/lwip2.0.3/include/",
            "//vendor/winnermicro/w800/include/arch/xt804/csi_core",
            "//vendor/winnermicro/w800/include/arch/xt804",
            "//vendor/winnermicro/w800/include/platform",
            "//vendor/winnermicro/w800/include/os",
            "//vendor/winnermicro/w800/include/net",
            "//vendor/winnermicro/w800/include/app",
            "//vendor/winnermicro/w800/include/wifi",
            "//vendor/winnermicro/w800/include",
        ]
    }
}

  • 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.

修改build.gn:

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

lite_component("app") {
    features = [
        "my_demo:demo",
    ]
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
标签
51cto_dancehole_demo.zip 3.51M 21次下载
已于2022-5-28 11:04:46修改
3
收藏 2
回复
举报
3
1
2
1条回复
按时间正序
/
按时间倒序
物联风景
物联风景

666,给你点赞

回复
2022-5-29 20:16:42
回复
    相关推荐