stm32+lwip(四):網頁服務器測試


我是卓波,很高興你來看我的博客。

 

系列文章:

stm32+lwip(一):使用STM32CubeMX生成項目

stm32+lwip(二):UDP測試

stm32+lwip(三):TCP測試

stm32+lwip(四):網頁服務器測試

stm32+lwip(五):以太網幀發送測試

 

ST官方有lwip的例程,下載地址如下:

https://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32-standard-peripheral-library-expansion/stsw-stm32070.html

本文例子參考ST官方給出的例程。

一、准備

ST例程文檔關於lwip的介紹如下:

 

由此可以看到LWIP有三種API,在本文中,使用Raw API。

HTTP協議是建立在TCP協議之上的一種應用,本文用到的TCP Raw API如下

二、移植官方例程

官方例程在stsw-stm32070\STM32F4x7_ETH_LwIP_V1.1.1\Project\Standalone\httpserver目錄下,拷貝以下文件到我們工程:

 

fs.c/h是文件的操作

fs.data.c/h存放了網頁的數據

httpd.c/h是網頁服務器

httpd_cgi_ssi.c cgissi的處理

httpd_cgi_ssi.c中關於硬件操作的代碼刪掉,或者找到宏LWIP_HTTPD_SSI和宏LWIP_HTTPD_CGISSICGI功能禁掉,然后在主程序中增加httpd_init()

上電運行后可以看到ST官方例子的網頁:

 

三、打印網頁接收數據

在httpd.c中的http_recv函數中增加以下黃色部分代碼:

 

然后當在瀏覽器打開192.168.2.8時,串口輸出以下數據:

get msg from 192:168:2:194 port:55329:
GET / HTTP/1.1
Host: 192.168.2.8
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9


get msg from 192:168:2:194 port:55330:
GET /STM32F4x7_files/ST.gif HTTP/1.1
Host: 192.168.2.8
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Referer: http://192.168.2.8/
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9


get msg from 192:168:2:194 port:55331:
GET /STM32F4x7_files/stm32.jpg HTTP/1.1
Host: 192.168.2.8
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Referer: http://192.168.2.8/
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9


get msg from 192:168:2:194 port:55332:
GET /inchtml-pages-stm32_connectivity_files/pixel.gif HTTP/1.1
Host: 192.168.2.8
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Referer: http://192.168.2.8/
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9


get msg from 192:168:2:194 port:55334:
GET /favicon.ico HTTP/1.1
Host: 192.168.2.8
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Referer: http://192.168.2.8/
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9

由此可以看出,打開了網頁服務器地址,會給網頁服務器發送GET / HTTP/1.1命令,網頁服務器會根據這個命令找到index.html,然后給瀏覽器返回網頁數據。由於網頁數據中又包含了一些照片資源,因此瀏覽器再給網頁服務器發送

GET /STM32F4x7_files/ST.gif HTTP/1.1
GET /STM32F4x7_files/stm32.jpg HTTP/1.1
GET /inchtml-pages-stm32_connectivity_files/pixel.gif HTTP/1.1
GET /favicon.ico HTTP/1.1

以獲取照片資源。

四、最后

本文測試了網頁服務器功能,能正常打開預置網頁,后續開發嵌入式網頁可以根據ST例程進行修改,同時加上了SSICGI可以做到一些簡單的交互。

 

github:https://github.com/54zorb/stm32-lwip

版權所有,轉載請打賞喲

 

如果你喜歡我的文章,可以通過微信掃一掃給我打賞喲


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM