|
【HarmonyOS HiSpark Wi-Fi IoT 套件試用連載】基于LwIP的Web Server 的演示與代碼分享,
本帖最后由 soon順soon 于 2020-12-12 09:55 編輯
硬件模塊:
WF-H861-SSA1 WiFi 模組
實(shí)現(xiàn)功能:在上電后進(jìn)入AP 模式,熱點(diǎn)名稱:Soon-WiFi-IoT,連接上熱點(diǎn)后用瀏覽器訪問(wèn) http://192.168.10.1/
顯示“Hello Wifi IoT”
1.png (24.64 KB, 下載次數(shù): 0)
下載附件 保存到相冊(cè)
1 分鐘前 上傳
http://192.168.10.1/ wifiiot
顯示“歡迎訪問(wèn)wifi-iot頁(yè)面”
2.png (28.21 KB, 下載次數(shù): 0)
下載附件 保存到相冊(cè)
1 分鐘前 上傳
http://192.168.10.1/ error
顯示“404-Page not found”
3.png (26.42 KB, 下載次數(shù): 0)
下載附件 保存到相冊(cè)
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 “l(fā)wip/sockets.h“
- const unsigned char htmldata[] = “\
- <html>\
- <meta charset=\“utf-8 \“>\
- <head>\
- <title>Wifi-iot</title>\
- </head>\
- <body>\
- <h1>歡迎訪問(wèn)wifi-iot頁(yè)面</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);
復(fù)制代碼
完整代碼,如附件
lwip_webserver.7z
(2.69 KB, 下載次數(shù): 0, 售價(jià): 1 分積分) 26 分鐘前 上傳 點(diǎn)擊文件名下載附件
閱讀權(quán)限: 1
售價(jià): 1 分積分 [記錄](méi) [購(gòu)買]
下載積分: 積分 -1 分
編譯好的測(cè)試bin
Hi3861_wifiiot_app_allinone.bin
(761.04 KB, 下載次數(shù): 0) 26 分鐘前 上傳 點(diǎn)擊文件名下載附件
下載積分: 積分 -1 分
參考文章
LwIP之socket應(yīng)用--WebServer和Modbus TCP |
|