cancel
Showing results for 
Search instead for 
Did you mean: 

HTTP web server handling in NUCLEO-F439ZI

Ramachandran1992
Associate III

Hi team.

I develop a code, employs read the ADC value from the LDR and transmit the ADC value serially by UART, and glow the LED if LDR value goes above 1400. It is work fine up to hardware level. Now I update the value in the HTML page using Ethernet. I will be able to send only the ADC value to web page, I unable to send the LED STATUS to the web page. In the ssi_Handler function the iIndex value execute only the Case 0:(ADC_Value update), it does not update the case 1:(LED_Status) value. I attached my code below, please guide me to what type of error I was made. the code has 3 parts

1. main.c

2.ssi.c

3.shtml code

the first part of the code 

1. main.c

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "lwip.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "string.h"
#include "lwip/apps/httpd.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;

UART_HandleTypeDef huart3;

/* USER CODE BEGIN PV */
uint16_t adc_data =0;
char msg[20];
extern uint16_t adc_data;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART3_UART_Init(void);
static void MX_ADC1_Init(void);
/* USER CODE BEGIN PFP */
extern struct netif gnetif;
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART3_UART_Init();
  MX_ADC1_Init();
  MX_LWIP_Init();
  /* USER CODE BEGIN 2 */
  http_server_init();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  HAL_ADC_Start(&hadc1);
	  	  HAL_ADC_PollForConversion(&hadc1, 20);
	  	  adc_data = HAL_ADC_GetValue(&hadc1);
	  	  sprintf(msg,"LDR Value: %hu \r\n",adc_data);
	  	  HAL_UART_Transmit(&huart3, (uint8_t *)msg, strlen(msg), HAL_MAX_DELAY);
	  	  if (adc_data > 1400)
	  	  {
	  		  HAL_GPIO_WritePin(GPIOG, GPIO_PIN_2, SET);
	  	  }
	  	  else
	  	  {
	  		  HAL_GPIO_WritePin(GPIOG, GPIO_PIN_2, RESET);
	  	  }
	  	  HAL_Delay(500);
	  ethernetif_input(&gnetif);
	  sys_check_timeouts();
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief ADC1 Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_ADC1_Init(void)
{

  /* USER CODE BEGIN ADC1_Init 0 */

  /* USER CODE END ADC1_Init 0 */

  ADC_ChannelConfTypeDef sConfig = {0};

  /* USER CODE BEGIN ADC1_Init 1 */

  /* USER CODE END ADC1_Init 1 */

  /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  */
  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  hadc1.Init.ScanConvMode = DISABLE;
  hadc1.Init.ContinuousConvMode = DISABLE;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = 1;
  hadc1.Init.DMAContinuousRequests = DISABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  */
  sConfig.Channel = ADC_CHANNEL_3;
  sConfig.Rank = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN ADC1_Init 2 */

  /* USER CODE END ADC1_Init 2 */

}

/**
  * @brief USART3 Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_USART3_UART_Init(void)
{

  /* USER CODE BEGIN USART3_Init 0 */

  /* USER CODE END USART3_Init 0 */

  /* USER CODE BEGIN USART3_Init 1 */

  /* USER CODE END USART3_Init 1 */
  huart3.Instance = USART3;
  huart3.Init.BaudRate = 115200;
  huart3.Init.WordLength = UART_WORDLENGTH_8B;
  huart3.Init.StopBits = UART_STOPBITS_1;
  huart3.Init.Parity = UART_PARITY_NONE;
  huart3.Init.Mode = UART_MODE_TX_RX;
  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart3) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART3_Init 2 */

  /* USER CODE END USART3_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOG_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOG, GPIO_PIN_2, GPIO_PIN_RESET);

  /*Configure GPIO pin : PG2 */
  GPIO_InitStruct.Pin = GPIO_PIN_2;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @PAram  file: pointer to the source file name
  * @PAram  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

2. ssi.c

/*
 * ssi.c
 *
 *  Created on: May 17, 2024
 *      Author: BLR-PC06
 */
#include "main.h"
#include "ssi.h"
#include "stdio.h"
#include "string.h"
#include "lwip/tcp.h"
#include "lwip/apps/httpd.h"
#include "stm32f4xx_hal.h"

extern uint16_t adc_data;
char const* TAG_CHAR[] = {"adc_data","led_status"};//, "adc_data"};
char const** TAGS = TAG_CHAR;


uint16_t ssi_handler(int iIndex, char *pcInsert, int iInsertLen)
{
	switch(iIndex)
	{
	case 0:
		sprintf(pcInsert,"%d",adc_data);
		return strlen(pcInsert);

		break;
	case 1:
		if (HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_2) == GPIO_PIN_SET)
		{
			sprintf(pcInsert,"ON");
		}
		else
		{
			sprintf(pcInsert,"OFF");
		}
		return strlen(pcInsert);
		break;
	default:
		break;
	}
	return 0;
}

void http_server_init(void)
{
	httpd_init();
	http_set_ssi_handler(ssi_handler,(char const**)TAGS,2);
}

3.shtml file

<!DOCTYPE html>
<html>
<head>
<title> SSI_ADC_TEST </title>
<meta http-equiv = "refresh" content = "1">
</head>
<style>
table, th, td {
  border:1px solid black;
}
</style>
<body>

<h2>Status Monitor</h2>

<table style="width:100%">
  <tr>ADC_DATA1
   		<td>ADC_Value</td>
		<td>LED_STATUS</td>
	</tr>
	<tr>
		<td><!--#adc_data--></td>
		<td><!--#led_status--></td>
  </tr>
</table>

<p>update the data every second.</p>

</body>
</html>

the below image shows the output for the above coding in the web page

Ramachandran1992_0-1716266282993.png

Thank you 

with regards 

Ramachandran.S

 

1 ACCEPTED SOLUTION

Accepted Solutions
Muhammed Güler
Senior III

Your tag name is too long, you need to shorten it.
relevant section on the page below

https://www.nongnu.org/lwip/2_1_x/group__httpd.html

 

Notes about valid SSI tags

The following assumptions are made about tags used in SSI markers:

  1. No tag may contain '-' or whitespace characters within the tag name.
  2. Whitespace is allowed between the tag leadin "<!--#" and the start of the tag name and between the tag name and the leadout string "-->".
  3. The maximum tag name length is LWIP_HTTPD_MAX_TAG_NAME_LEN, currently 8 characters.

 

View solution in original post

6 REPLIES 6
Muhammed Güler
Senior III

Your tag name is too long, you need to shorten it.
relevant section on the page below

https://www.nongnu.org/lwip/2_1_x/group__httpd.html

 

Notes about valid SSI tags

The following assumptions are made about tags used in SSI markers:

  1. No tag may contain '-' or whitespace characters within the tag name.
  2. Whitespace is allowed between the tag leadin "<!--#" and the start of the tag name and between the tag name and the leadout string "-->".
  3. The maximum tag name length is LWIP_HTTPD_MAX_TAG_NAME_LEN, currently 8 characters.

 

Ramachandran1992
Associate III

Hi Muhammed,

Thanks for your quick reply, its works fine when the tag length was reduced to below 8 characters. The link sent by you for reference is help a lot. If any other doubts in future I refer this page for SSI. If I have doubts in any other topics where I found this type of links for reference. In which website I refer this type of reference, is there any special link or web page is there for reference. where I will get that webpage

Thank you

regards

Ramachandran.S


@Ramachandran1992 wrote:

In which website I refer this type of reference, is there any special link or web page is there for reference. where I will get that webpage


This is the LwIP Project page: https://savannah.nongnu.org/ - that's the place to start for all LwIP details ...

 

Hi Andrew 

I am not only asking for LWIP, but I also need this for all peripherals like UART, I2C, SPI etc... this page help to give information regarding programing structure, module, data structure and file. I need that page for every protocols. I need the reference link for the above protocols
thank you

Ramachandran.S


@Ramachandran1992 wrote:

I also need this for all peripherals like UART, I2C, SPI etc...


Those are all part of the STM32 - entirely independent of LwIP or HTTP.

For those, your need the Reference Manual for your particular STM32.

Find it on the Product Page for that part:

https://www.st.com/en/microcontrollers-microprocessors/stm32f439zi.html#documentation 

Ramachandran1992
Associate III

Hi Andrew,

Thank you for your quick reply and great information.

Regards

Ramachandran S