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

描述

本文来源电子发烧友社区,作者:soon顺soon, 帖子地址:https://bbs.elecfans.com/jishu_2018138_1_1.html

硬件模块:

WF-H861-SSA1 WiFi 模组

实现功能:在上电后进入AP 模式,热点名称:Soon-WiFi-IoT,连接上热点后用浏览器访问http://192.168.10.1/
显示“Hello Wifi IoT”
HarmonyOS
http://192.168.10.1/wifiiot
显示“欢迎访问wifi-iot页面”
HarmonyOS
http://192.168.10.1/error
显示“404-Page not found”
HarmonyOS

主要代码如下
  1. #include
  2. #include
  3. #include
  4. #include
  5. #include "ohos_init.h"
  6. #include "cmsis_os2.h"
  7. #include "wifi/ap_mode.h"
  8. #include "lwip/sockets.h"
  9.  
  10. const unsigned char htmldata[] = "
  11.       
  12.             
  13.             
  14.                <title>Wifi-iot
  15.             
  16.             
  17.                

    欢迎访问wifi-iot页面

  18.             
  19.      ";
  20. const unsigned char hellowifiiot[] = "
  21.         
  22.             
  23.                
  24.             
  25.             
  26.                

    Hello Wifi IoT

  27.             
  28.         ";
  29. const unsigned char errhtml[] = "
  30.         
  31.             
  32.                
  33.             
  34.             
  35.                

    404-Page not found

  36.             
  37.         ";
  38.  
  39. /**
  40.   * [url=home.php?mod=space&uid=2666770]@Brief[/url] serve tcp connection  
  41.   * [url=home.php?mod=space&uid=3142012]@param[/url] conn: connection socket
  42.   * @retval None
  43.   */
  44. void http_server(int conn)
  45. {
  46.     int buflen = 1500;
  47.     int ret;
  48.     unsigned char recv_buffer[1500];
  49.  
  50.     /* Read in the request */
  51.     ret = read(conn, recv_buffer, buflen);
  52.     if (ret <= 0)
  53.     {
  54.         close(conn);
  55.         printf("read failedrn");
  56.         return;
  57.     }
  58.  
  59.     if (strncmp((char *)recv_buffer, "GET /wifiiot", 9) == 0)
  60.     {
  61.         write(conn, htmldata, sizeof(htmldata) - 1);
  62.     }
  63.     else if (strncmp((char *)recv_buffer, "GET /error", 9) == 0)
  64.     {
  65.         write(conn, errhtml, sizeof(errhtml) - 1);
  66.     }
  67.     else
  68.     {
  69.         write(conn, hellowifiiot, sizeof(hellowifiiot) - 1);
  70.     }
  71.     /* Close connection socket */
  72.     close(conn);
  73. }
  74.  
  75. /**
  76.   * @brief  http_task
  77.   * @param None
  78.   * @retval None
  79.   */
  80. static void http_task(void)
  81. {
  82.     int sock, newconn, size;
  83.     struct sockaddr_in address, remotehost;
  84.  
  85.     /* create a TCP socket */
  86.     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  87.     {
  88.         printf("can not create socket");
  89.         return;
  90.     }
  91.  
  92.     /* bind to port 80 at any interface */
  93.     address.sin_family = AF_INET;
  94.     address.sin_port = htons(80);
  95.     address.sin_addr.s_addr = INADDR_ANY;
  96.     if (bind(sock, (struct sockaddr *)&address, sizeof(address)) < 0)
  97.     {
  98.         printf("can not bind socket");
  99.         close(sock);
  100.         return;
  101.     }
  102.  
  103.     /* listen for connections (TCP listen backlog = 1) */
  104.     listen(sock, 1);
  105.     size = sizeof(remotehost);
  106.     while (1)
  107.     {
  108.         newconn = accept(sock, (struct sockaddr *)&remotehost, (socklen_t *)&size);
  109.         if (newconn >= 0)
  110.         {
  111.             printf("newconn");
  112.             http_server(newconn);
  113.         }
  114.         else
  115.         {
  116.             close(newconn);
  117.         }
  118.     }
  119. }
  120.  
  121. static void *Lwip_Web_Task(const char *arg)
  122. {
  123.     (void)arg;
  124.     wifi_start_softap();
  125.     http_task();
  126.     return NULL;
  127. }
  128.  
  129. static void Lwip_Web_Entry(void)
  130. {
  131.     osThreadAttr_t attr = {0};
  132.  
  133.     attr.name = "Lwip_Web_Task";
  134.     attr.attr_bits = 0U;
  135.     attr.cb_mem = NULL;
  136.     attr.cb_size = 0U;
  137.     attr.stack_mem = NULL;
  138.     attr.stack_size = 8192;
  139.     attr.priority = osPriorityNormal;
  140.  
  141.     if (osThreadNew((osThreadFunc_t)Lwip_Web_Task, NULL, &attr) == NULL)
  142.     {
  143.         printf("Falied to create Lwip_Web_Entry!n");
  144.     }
  145. }
  146.  
  147. SYS_RUN(Lwip_Web_Entry);
复制代码


完整代码,如附件
HarmonyOS lwip_webserver.7z (2.69 KB, 下载次数: 3)

编译好的测试bin
HarmonyOS Hi3861_wifiiot_app_allinone.bin (761.04 KB, 下载次数: 5)

参考文章
LwIP之socket应用--WebServer和Modbus TCP
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分