Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
LWIP
RT-Thread 使用 webserver (lwip协议栈 自带httpd )
发布于 2023-02-22 14:58:04 浏览:1140
订阅该版
参考正点原子的 **网络实验10 NETCONN_WEBserver实验**和**《lwIP开发指南》**。 开发环境:野火的stm32f407,rt-thread studio版本是版本: 2.2.6,stm32f4的资源包为0.2.2,rt-thread版本为4.0.3。 以**RT-Thread中Lan8720和lwip协议栈的使用**文章创建的工程为基础。 httpd(The Apache HTTP Server)的[官方网址](https://httpd.apache.org/)。 在**rtthread工程**中新建文件夹webserver,存放webserver相关文件。 对工程进行编译,正常通过。 需要修改的代码,过程如下: * rt-thread\components\net\lwip-2.0.2\src\include\lwip\apps\httpd_opts.h 文件中的宏定义 * **LWIP_HTTPD_CGI** 默认为0,改为1 * **LWIP_HTTPD_SSI** 默认为0,改为1 * **HTTPD_USE_CUSTOM_FSDATA**默认为0 * **LWIP_HTTPD_DYNAMIC_FILE_READ**默认为0,改为1 * 将 rt-thread\components\net\lwip-2.0.2\src\apps\httpd文件夹 添加构建 * httpd文件夹下的fsdata.c 排除构建。 * 在主函数中增加如下代码 ``` extern void httpd_init(void); httpd_init(); while (count++) { LOG_D("Hello RT-Thread!"); rt_thread_mdelay(10000); } ``` 编译正常,下载到开发板,效果如图1: ![screenshot_1.png](https://oss-club.rt-thread.org/uploads/20230222/431b900f44a5e0bd9cc9310e912dc57d.png) 如何使用自己的网页呢?修改如下: 将rt-thread\components\net\lwip-2.0.2\src\apps\httpd文件夹下的fsdata.c替换成自己的fsdata.c,(使用makefsdata.exe这个软件自动生成即可)。 在webserve文件夹下创建httpd_cgi_ssi.c文件(CGI和SSI句柄函数) 参考原子的代码,修改如下: ```c #include
#include "lwip/tcp.h" #include
#include
#include
int LED1=0; int BEEP=0; #define NUM_CONFIG_CGI_URIS (sizeof(ppcURLs) / sizeof(tCGI)) #define NUM_CONFIG_SSI_TAGS (sizeof(ppcTAGs) / sizeof(char *)) //extern short Get_Temprate(void); //extern void RTC_Get_Time(u8 *hour,u8 *min,u8 *sec,u8 *ampm); //extern void RTC_Get_Date(u8 *year,u8 *month,u8 *date,u8 *week); //控制LED的CGI handler const char* LEDS_CGI_Handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]); const char* BEEP_CGI_Handler(int iIndex,int iNumParams,char *pcParam[],char *pcValue[]); static const char *ppcTAGs[]= //SSI的Tag { "t", //ADC值 "w", //温度值 "h", //时间 "y" //日期 }; static const tCGI ppcURLs[]= //cgi程序 { {"/leds.cgi",LEDS_CGI_Handler}, {"/beep.cgi",BEEP_CGI_Handler}, }; //当web客户端请求浏览器的时候,使用此函数被CGI handler调用 static int FindCGIParameter(const char *pcToFind,char *pcParam[],int iNumParams) { int iLoop; for(iLoop = 0;iLoop < iNumParams;iLoop ++ ) { if(strcmp(pcToFind,pcParam[iLoop]) == 0) { return (iLoop); //返回 iLOOP } } return (-1); } //SSIHandlerÖ中adc处理函数 void ADC_Handler(char *pcInsert) { char Digit1=0, Digit2=0, Digit3=0, Digit4=0; static uint32_t ADCVal = 0; //ADCVal = Get_Adc_Average(5,10);//ADC1_CH5的电压值 ADCVal+=10; ADCVal=ADCVal%3000; //转换为 ADCVval * 0.8mv ADCVal = (uint32_t)(ADCVal * 0.8); Digit1= ADCVal/1000; Digit2= (ADCVal-(Digit1*1000))/100 ; Digit3= (ADCVal-((Digit1*1000)+(Digit2*100)))/10; Digit4= ADCVal -((Digit1*1000)+(Digit2*100)+ (Digit3*10)); /* 准备添加到html中的数据 */ *pcInsert = (char)(Digit1+0x30); *(pcInsert + 1) = (char)(Digit2+0x30); *(pcInsert + 2) = (char)(Digit3+0x30); *(pcInsert + 3) = (char)(Digit4+0x30); } //SSIHandler中需要用到的内部处理温度传感器 void Temperate_Handler(char *pcInsert) { char Digit1=0, Digit2=0, Digit3=0, Digit4=0,Digit5=0; static short Temperate = 0; //Temperate = Get_Temprate(); Temperate+=1.3; Digit1 = Temperate / 10000; Digit2 = (Temperate % 10000)/1000; Digit3 = (Temperate % 1000)/100 ; Digit4 = (Temperate % 100)/10; Digit5 = Temperate % 10; /* 准备添加到html中的数据 */ *pcInsert = (char)(Digit1+0x30); *(pcInsert+1) = (char)(Digit2+0x30); *(pcInsert+2) = (char)(Digit3+0x30); *(pcInsert+3) = '.'; *(pcInsert+4) = (char)(Digit4+0x30); *(pcInsert+5) = (char)(Digit5+0x30); } //SSIHandler中需要用到的处理RTC日时间的函数 void RTCTime_Handler(char *pcInsert) { static uint8_t hour,min,sec,ampm; hour++; min++; sec++; ampm++; //RTC_Get_Time(&hour,&min,&sec,&m); /* 准备添加到html中的数据 */ *pcInsert = (char)((hour/10) + 0x30); *(pcInsert+1) = (char)((hour%10) + 0x30); *(pcInsert+2) = ':'; *(pcInsert+3) = (char)((min/10) + 0x30); *(pcInsert+4) = (char)((min%10) + 0x30); *(pcInsert+5) = ':'; *(pcInsert+6) = (char)((sec/10) + 0x30); *(pcInsert+7) = (char)((sec%10) + 0x30); } //SSIHandler中需要用到的处理RTC日期的函数 void RTCdate_Handler(char *pcInsert) { static uint8_t year,month,date,week; //RTC_Get_Date(&year,&month,&date,&week); year++; month++; date++; week++; /* 准备添加到html中的数据 */ *pcInsert = '2'; *(pcInsert+1) = '0'; *(pcInsert+2) = (char)((year/10) + 0x30); *(pcInsert+3) = (char)((year%10) + 0x30); *(pcInsert+4) = '-'; *(pcInsert+5) = (char)((month/10) + 0x30); *(pcInsert+6) = (char)((month%10) + 0x30); *(pcInsert+7) = '-'; *(pcInsert+8) = (char)((date/10) + 0x30); *(pcInsert+9) = (char)((date%10) + 0x30); *(pcInsert+10) = ' '; *(pcInsert+11) = 'w'; *(pcInsert+12) = 'e'; *(pcInsert+13) = 'e'; *(pcInsert+14) = 'k'; *(pcInsert+15) = ':'; *(pcInsert+16) = (char)(week + 0x30); } //SSI的 Handler 句柄 static u16_t SSIHandler(int iIndex,char *pcInsert,int iInsertLen) { switch(iIndex) { case 0: ADC_Handler(pcInsert); break; case 1: Temperate_Handler(pcInsert); break; case 2: RTCTime_Handler(pcInsert); break; case 3: RTCdate_Handler(pcInsert); break; } return strlen(pcInsert); } //CGI LED控制句柄 const char* LEDS_CGI_Handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]) { uint8_t i=0; //注意根据自己的GET的参数的多少来选择i值范围 iIndex = FindCGIParameter("LED1",pcParam,iNumParams); //找到LED的索引号 //只有一个CGI句柄 iIndex=0 if (iIndex != -1) { LED1=1; for (i=0; i
6
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
YZRD
这家伙很懒,什么也没写!
文章
25
回答
224
被采纳
19
关注TA
发私信
相关文章
1
RT-THREAD在STM32H747平台上移植lwip
2
{lwip}使能RT_LWIP_DHCP时可以获取到ip
3
stm32f103 LWIP 2.0.2 TCP收发问题
4
lwip2.1不重启修改IP
5
关于网络协议栈的测试
6
可否将LWIP升级到2.1.2 和 2.0.3?
7
socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
8
tcpclient 插拔网线问题?
9
两个tcpclient同时通讯可以吗?
10
SO_BINDTODEVICE 未定义该如何解决
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
I2C_IIC
ESP8266
UART
WIZnet_W5500
ota在线升级
cubemx
PWM
flash
freemodbus
BSP
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
SFUD
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
出出啊
1518
个答案
343
次被采纳
小小李sunny
1444
个答案
290
次被采纳
张世争
813
个答案
177
次被采纳
crystal266
547
个答案
161
次被采纳
whj467467222
1222
个答案
149
次被采纳
本月文章贡献
出出啊
1
篇文章
1
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
3
次点赞
crystal266
2
篇文章
2
次点赞
whj467467222
2
篇文章
2
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部