电子产业一站式赋能平台

PCB联盟网

搜索
查看: 743|回复: 0
收起左侧

【HarmonyOS HiSpark Wi-Fi IoT 套件试用连载】基于LwIP的Web Server 的演示与代码分享

[复制链接]

2607

主题

2607

帖子

7472

积分

高级会员

Rank: 5Rank: 5

积分
7472
发表于 2020-12-12 09:56:07 | 显示全部楼层 |阅读模式
【HarmonyOS HiSpark Wi-Fi IoT 套件试用连载】基于LwIP的Web Server 的演示与代码分享,   
本帖最后由 soon顺soon 于 2020-12-12 09:55 编辑



硬件模块:

WF-H861-SSA1 WiFi 模组



实现功能:在上电后进入AP 模式,热点名称:Soon-WiFi-IoT,连接上热点后用浏览器访问 http://192.168.10.1/

显示“Hello Wifi IoT”



1.png (24.64 KB, 下载次数: 0)

下载附件  保存到相册  

1 分钟前 上传

http://192.168.10.1/ wifiiot

显示“欢迎访问wifi-iot页面”



2.png (28.21 KB, 下载次数: 0)

下载附件  保存到相册  

1 分钟前 上传

http://192.168.10.1/ error

显示“404-Page not found”



3.png (26.42 KB, 下载次数: 0)

下载附件  保存到相册  

1 分钟前 上传



主要代码如下



  • #include <stdio.h>
      
  • #include <unistd.h>
      
  • #include <string.h>
      
  • #include <hi_i2c.h>
      
  • #include “ohos_init.h“
      
  • #include “cmsis_os2.h“
      
  • #include “wifi/ap_mode.h“
      
  • #include “lwip/sockets.h“
      

  •   
  • const unsigned char htmldata[] = “\
      
  •       <html>\
      
  •             <meta charset=\“utf-8 \“>\
      
  •             <head>\
      
  •                <title>Wifi-iot</title>\
      
  •             </head>\
      
  •             <body>\
      
  •                <h1>欢迎访问wifi-iot页面</h1>\
      
  •             </body>\
      
  •      </html>“;
      
  • const unsigned char hellowifiiot[] = “\
      
  •         <html>\
      
  •             <head>\
      
  •                <title>Hello Wifi IoT</title>\
      
  •             </head>\
      
  •             <body>\
      
  •                <h1>Hello Wifi IoT</h1>\
      
  •             </body>\
      
  •         </html>“;
      
  • const unsigned char errhtml[] = “\
      
  •         <html>\
      
  •             <head>\
      
  •                <title>Error!</title>\
      
  •             </head>\
      
  •             <body>\
      
  •                <h1>404-Page not found</h1>\
      
  •             </body>\
      
  •         </html>“;
      

  •   
  • /**
      
  •   * @Brief serve tcp connection  
      
  •   * @param conn: connection socket
      
  •   * @retval None
      
  •   */
      
  • void http_server(int conn)
      
  • {
      
  •     int buflen = 1500;
      
  •     int ret;
      
  •     unsigned char recv_buffer[1500];
      

  •   
  •     /* Read in the request */
      
  •     ret = read(conn, recv_buffer, buflen);
      
  •     if (ret <= 0)
      
  •     {
      
  •         close(conn);
      
  •         printf(“read faiLED\r\n“);
      
  •         return;
      
  •     }
      

  •   
  •     if (strncmp((char *)recv_buffer, “GET /wifiiot“, 9) == 0)
      
  •     {
      
  •         write(conn, htmldata, sizeof(htmldata) - 1);
      
  •     }
      
  •     else if (strncmp((char *)recv_buffer, “GET /error“, 9) == 0)
      
  •     {
      
  •         write(conn, errhtml, sizeof(errhtml) - 1);
      
  •     }
      
  •     else
      
  •     {
      
  •         write(conn, hellowifiiot, sizeof(hellowifiiot) - 1);
      
  •     }
      
  •     /* Close connection socket */
      
  •     close(conn);
      
  • }
      

  •   
  • /**
      
  •   * @brief  http_task
      
  •   * @param None
      
  •   * @retval None
      
  •   */
      
  • static void http_task(void)
      
  • {
      
  •     int sock, newconn, size;
      
  •     struct sockaddr_in address, remotehost;
      

  •   
  •     /* create a TCP socket */
      
  •     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
      
  •     {
      
  •         printf(“can not create socket“);
      
  •         return;
      
  •     }
      

  •   
  •     /* bind to port 80 at any inteRFace */
      
  •     address.sin_family = AF_INET;
      
  •     address.sin_port = htons(80);
      
  •     address.sin_addr.s_addr = INADDR_ANY;
      
  •     if (bind(sock, (struct sockaddr *)&address, sizeof(address)) < 0)
      
  •     {
      
  •         printf(“can not bind socket“);
      
  •         close(sock);
      
  •         return;
      
  •     }
      

  •   
  •     /* listen for connections (TCP listen backlog = 1) */
      
  •     listen(sock, 1);
      
  •     size = sizeof(remotehost);
      
  •     while (1)
      
  •     {
      
  •         newconn = accept(sock, (struct sockaddr *)&remotehost, (socklen_t *)&size);
      
  •         if (newconn >= 0)
      
  •         {
      
  •             printf(“newconn“);
      
  •             http_server(newconn);
      
  •         }
      
  •         else
      
  •         {
      
  •             close(newconn);
      
  •         }
      
  •     }
      
  • }
      

  •   
  • static void *Lwip_Web_Task(const char *arg)
      
  • {
      
  •     (void)arg;
      
  •     wifi_start_softap();
      
  •     http_task();
      
  •     return NULL;
      
  • }
      

  •   
  • static void Lwip_Web_Entry(void)
      
  • {
      
  •     osThreadAttr_t attr = {0};
      

  •   
  •     attr.name = “Lwip_Web_Task“;
      
  •     attr.attr_bits = 0U;
      
  •     attr.cb_mem = NULL;
      
  •     attr.cb_size = 0U;
      
  •     attr.stack_mem = NULL;
      
  •     attr.stack_size = 8192;
      
  •     attr.priority = osPriorityNormal;
      

  •   
  •     if (osThreadNew((osThreadFunc_t)Lwip_Web_Task, NULL, &attr) == NULL)
      
  •     {
      
  •         printf(“Falied to create Lwip_Web_Entry!\n“);
      
  •     }
      
  • }
      

  •   
  • SYS_RUN(Lwip_Web_Entry);

复制代码



完整代码,如附件

lwip_webserver.7z
(2.69 KB, 下载次数: 0, 售价: 1 分积分) 26 分钟前 上传 点击文件名下载附件

阅读权限: 1

售价: 1 分积分  [记录]  [购买]

下载积分: 积分 -1 分



编译好的测试bin

Hi3861_wifiiot_app_allinone.bin
(761.04 KB, 下载次数: 0) 26 分钟前 上传 点击文件名下载附件

下载积分: 积分 -1 分



参考文章


LwIP之socket应用--WebServer和Modbus TCP
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则


联系客服 关注微信 下载APP 返回顶部 返回列表