2020-12-12 09:32:59
0
硬件模块:
WF-H861-SSA1 WiFi 模组
实现功能:在上电后进入AP 模式,热点名称:Soon-WiFi-IoT,连接上热点后用浏览器访问http://192.168.10.1/
显示“Hello Wifi IoT”
http://192.168.10.1/wifiiot
显示“欢迎访问wifi-iot页面”
http://192.168.10.1/error
显示“404-Page not found”
主要代码如下
- #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>";
- /**
- * [url=home.php?mod=space&uid=2666770]@Brief[/url] serve tcp connection
- * [url=home.php?mod=space&uid=3142012]@param[/url] 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 failedrn");
- 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);
复制代码
完整代码,如附件
1
编译好的测试bin
参考文章
LwIP之socket应用--WebServer和Modbus TCP
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。
侵权投诉