0


STM32F407 LAN8720A 嵌入式WebServer:一个简单的登录页面

硬件:野火stm32f407开发板(带网络功能)

lan芯片:lan8720A

系统:rt-thread

http服务的功能:野火stm32f407开发板作为服务器,PC机作为客户端,在浏览器中访问STM32中的网页,具有登录功能。

网页截图如下:

登录成功

前端页面代码如下:

index.shtml

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>用户登录</title>
    <style>
      body {
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .form-group {
        margin-bottom: 20px;
      }
      .form-group label {
        display: inline-block;
        min-width: 70px;
      }
      .form-group input {
        height: 32px;
        line-height: 32px;
      }
    </style>
  </head>
  <body>
    <div class="container" style="width: 330px; padding-top: 130px">
      <h2 style="text-align: center">用户登录</h2>
      <form id="loginForm" method="get" action="/register.cgi">
        <div class="form-group">
          <label for="username">用户名:</label>
          <input
            type="text"
            name="username"
            class="form-control"
            id="username"
            placeholder="请输入用户名"
            autocomplete="off"
          />
        </div>

        <div class="form-group">
          <label for="password">密码:</label>
          <input
            type="password"
            name="password"
            class="form-control"
            id="password"
            placeholder="请输入密码"
            autocomplete="off"
          />
        </div>

        <div class="form-group" style="text-align: center">
          <button
            class="btn btn-lg btn-primary btn-block"
            type="submit"
            value="登录"
          >
            用户登录
          </button>
        </div>
      </form>

    </div>

  </body>
</html>

helloa.html


<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body>

Hello World

</body> </html>

将index.shtml,helloa.html拷贝到具有fsdata.h文件的fs文件夹内。然后点击makefsdata.exe,生成网页数据文件fsdata.c

httpd_cgi_ssi.c

/**
  ******************************************************************************
  * @file    httpd_cg_ssi.c
  * @author  MCD Application Team
  * @version V1.1.0
  * @date    31-July-2013
  * @brief   Webserver SSI and CGI handlers
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/

#include "lwip/debug.h"
#include "httpd.h"
#include "lwip/tcp.h"
#include "fs.h"
#include "netconf.h"
#include "LAN8742A.h"
#include <string.h>
#include <stdlib.h>
#include "./Bsp/led/bsp_led.h" 
char myLocalIp[15],myNetMask[15],myGW[15];
char myRemoteIp[15],myLocalPort[4],myRemotePort[4];
#define NUM_CONFIG_CGI_URIS    1  //CGI的URI数量 发送的个数
#define NUM_CONFIG_SSI_TAGS    7  //SSI的TAG数量 发送的个数
//#define Device_Setting_CGI_Handler 16 
/*SSI的Tag 长度由LWIP_HTTPD_MAX_TAG_NAME_LEN宏定义确定*/
static const char *ppcTAGs[]=  
{
    "IPad",//IP地址  //长度4字节
    "coIP",//计算机IP地址
    "ponu",//端口号
    "eqnu",//设备编号
    "sosi",//软件版本
    "upti",//更新时间间隔
    "late",//车道检测方式
};

tSSIHandler SSIHandler;
//uint32_t ADC_not_configured=1;
//tCGIHandler Device_Setting_CGI_Handler;

/* we will use character "t" as tag for CGI */

//char const** TAGS=&TAGCHAR;
const char * Register_CGI_Handler (int iIndex, int iNumParams, char *pcParam[], char *pcValue[]);

 const tCGI ppcURLs[]= 
{

    {"/register.cgi", Register_CGI_Handler},
};
/* Cgi call table, only one CGI used */
tCGI CGI_TAB[1];

/**
  * @brief  Configures the ADC.
  * @param  None
  * @retval None
  */

/**
  * @brief  ADC_Handler : SSI handler for ADC page 
  */

/**
  * @brief  CGI handler for LEDs control 
  */
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);  
    } 
    }   
    return(-1);
    }

 const char* Register_CGI_Handler(int iIndex,int iNumParams,char *pcParam[],char *pcValue[]){
    u16 i=0,passchack=0;
    iIndex = FindCGIParameter("username",pcParam,iNumParams);  //找到登录页面 索引号
    /*    打印接受的数据
    printf("index:\r\n");
    for(i =0 ;i < iNumParams; i++){
        printf("pcParam[%d]:%s\r\n",i,pcParam[i]);
        printf("pcValue[%d]:%s\r\n",i,pcValue[i]);
    }
    printf("\r\n");*/
    if(iIndex != -1){
        for (i=0; i<iNumParams; i++){ 
            if(strcmp(pcParam[i],"username") == 0){ //检查用户CGI参数
                if(strcmp(pcValue[i],"admin") == 0){ //用户名正确
                        passchack++;
                }
            }else if(strcmp(pcParam[i],"password") == 0){ //检查密码CGI参数
                if(strcmp(pcValue[i],"admin123456") == 0){ //密码正确
                        passchack++;
                }
            }
        }
        if(passchack == 2){
            //用户名和密码正确进入数据显示界面
            return "/helloa.html";//输入正确 跳转到其他页面
        }
    }

        return "/index.shtml";//账号和密码输入错误,返回登录界面
}

/**
 * Initialize SSI handlers
 */
/*void httpd_init(void)
{
      LWIP_DEBUGF(HTTPD_DEBUG, ("httpd_init\n"));

    #if LWIP_HTTPD_SSI
      httpd_ssi_init();
    #endif
  
    #if LWIP_HTTPD_CGI
      httpd_cgi_init();
    #endif

    httpd_init_addr(IP_ADDR_ANY);
}
*/
/*SSI句柄初始化*/
void httpd_ssi_init(void)
{  
    //配置SSI句柄
    http_set_ssi_handler(SSIHandler,ppcTAGs,NUM_CONFIG_SSI_TAGS);
}
/*CGI句柄初始化*/
void httpd_cgi_init(void)
{ 
      //配置CGI句柄
      http_set_cgi_handlers(ppcURLs, NUM_CONFIG_CGI_URIS);
}
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

main.c文件内容如下:

/**
  ******************************************************************************
  * @file    main.c
  * @author  fire
  * @version V1.0
  * @date    2015-xx-xx
  * @brief   HTTP 网页服务器
  ******************************************************************************
  * @attention
  *
  * 实验平台:野火  STM32 F407 开发板 
  * 论坛    :http://www.firebbs.cn
  * 淘宝    :https://fire-stm32.taobao.com
  *
  ******************************************************************************
  */
#include "stm32f4xx.h"
#include "./Bsp/led/bsp_led.h" 
#include "./Bsp/usart/bsp_debug_usart.h"
#include "./Bsp/systick/bsp_SysTick.h"
#include "lwip/tcp.h"
#include "netconf.h"
#include "LAN8742A.h"
#include "httpd.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
extern __IO uint8_t EthLinkStatus;
__IO uint32_t LocalTime = 0; /* this variable is used to create a time reference incremented by 10ms */
/* Private function prototypes -----------------------------------------------*/
static void TIM3_Config(uint16_t period,uint16_t prescaler);
/* Private functions ---------------------------------------------------------*/
/**
  * @brief  主函数
  * @param  无
  * @retval 无
  */
int main(void)
{
    /* 初始化LED */
    LED_GPIO_Config();
    
    /* 初始化调试串口,一般为串口1 */
    Debug_USART_Config();
    
    /* 初始化系统滴答定时器 */    
    SysTick_Init();
    
    TIM3_Config(999,899);//10ms定时器
    printf("LAN8720A Ethernet Demo\n");
    
  printf("在电脑端浏览器直接输入地址:%d.%d.%d.%d,既可访问\n",IP_ADDR0,IP_ADDR1,IP_ADDR2,IP_ADDR3);

    /* Configure ethernet (GPIOs, clocks, MAC, DMA) */
  ETH_BSP_Config();    
  printf("LAN8720A BSP INIT AND COMFIGURE SUCCESS\n");
    
  /* Initilaize the LwIP stack */
  LwIP_Init();    
    
    /* Http webserver Init */
  httpd_init();
    
    while(1)
    {
        /* check if any packet received */
    if (ETH_CheckFrameReceived())
    { 
      /* process received ethernet packet */
      LwIP_Pkt_Handle();
    }
    /* handle periodic timers for LwIP */
    LwIP_Periodic_Handle(LocalTime);
    }
}

/**
  * @brief  通用定时器3中断初始化
  * @param  period : 自动重装值。
  * @param  prescaler : 时钟预分频数
  * @retval 无
  * @note   定时器溢出时间计算方法:Tout=((period+1)*(prescaler+1))/Ft us.
  *          Ft=定时器工作频率,为SystemCoreClock/2=90,单位:Mhz
  */
static void TIM3_Config(uint16_t period,uint16_t prescaler)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);  ///使能TIM3时钟
    
    TIM_TimeBaseInitStructure.TIM_Prescaler=prescaler;  //定时器分频
    TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
    TIM_TimeBaseInitStructure.TIM_Period=period;   //自动重装载值
    TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; 
    
    TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
    
    TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //允许定时器3更新中断
    TIM_Cmd(TIM3,ENABLE); //使能定时器3
    
    NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; //定时器3中断
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //抢占优先级1
    NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //子优先级3
    NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

/**
  * @brief  定时器3中断服务函数
  * @param  无
  * @retval 无
  */
void TIM3_IRQHandler(void)
{
    if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET) //溢出中断
    {
        LocalTime+=10;//10ms增量
    }
    TIM_ClearITPendingBit(TIM3,TIM_IT_Update);  //清除中断标志位
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

烧录程序测试:

在浏览器中输入192.168.1.122就可以访问页面。

CSDN:STM32F407 LAN8720A 嵌入式WebServer实现简单的登录注册功能


本文转载自: https://blog.csdn.net/m0_51385756/article/details/135294158
版权归原作者 m0_51385756 所有, 如有侵权,请联系我们删除。

“STM32F407 LAN8720A 嵌入式WebServer:一个简单的登录页面”的评论:

还没有评论