IoT : WiFi Load Control

char display[140],wifi_send[50],RX_ST[140];
void ESP8266_RX(char *b_send)
{
	sprintf(wifi_send,"%s",b_send);
	HAL_UART_Transmit(&huart1,(uint8_t*)wifi_send,strlen(wifi_send),1000);
	while(1)
	{
		sprintf(RX_ST,"%s",display);
		if(strstr(RX_ST,"OK")) break;
	}
	memset (wifi_send, '\0', 50);  // clear the buffer
	memset (RX_ST, '\0', 140);     // clear the buffer
	count=0;
	HAL_Delay(2000);
}
uint8_t rx_char,count;
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  if(huart->Instance==USART1 )
  {
    display[count++]=rx_char;
    HAL_UART_Receive_IT(&huart1,&rx_char,1);
  }
} 
ESP8266_RX("AT\r\n");                           // test function
ESP8266_RX("ATE0\r\n");                      // echo off
ESP8266_RX("AT+CWMODE=3\r\n");  // both softAP + station mode
char WIFI_NAME[]="WiFi_Name";
char WIFI_PASS[]="WiFi_Password";
ESP8266_Connect(WIFI_NAME,WIFI_PASS);
void ESP8266_Connect(char *wifi_name,char *wifi_pass)
{
	sprintf(wifi_send,"AT+CWJAP=\"%s\",\"%s\"\r\n", wifi_name, wifi_pass);
	HAL_UART_Transmit(&huart1,(uint8_t*)wifi_send,strlen(wifi_send),1000);
	while(1)
	{
		sprintf(RX_ST,"%s",display);
		if(strstr(RX_ST,"GOT IP")) break;
	}
	memset (wifi_send, '\0', 50);  // clear the buffer
	memset (RX_ST, '\0', 140);     // clear the buffer
	count=0;
	HAL_Delay(8000);
}
ESP8266_RX("AT+CIPMUX=1\r\n");       // multiple connection
ESP8266_RX("AT+CIPSERVER=1,80\r\n"); // Create server with port 80
char IPV4[16];
ESP8266_IP("AT+CIFSR\r\n");
void ESP8266_IP(char *getIP)
{
	sprintf(wifi_send,"%s",getIP);
	HAL_UART_Transmit(&huart1,(uint8_t*)wifi_send,strlen(wifi_send),1000);
	while(1)
	{
		sprintf(RX_ST,"%s",display);
		if(strstr(RX_ST,"STAMAC")) break;
	}
	for(uint8_t i=0;i<140;i++)
	{
	if(RX_ST[i]=='S'&& RX_ST[i+1]=='T' && RX_ST[i+2]=='A'&& RX_ST[i+3]=='I' && RX_ST[i+4]=='P')
     {
		for(uint8_t j=0;j<16;j++)
		{
			IPV4[j]=RX_ST[i+j+7];
			if(IPV4[j] == '"') 
			{	
				IPV4[j]='\0'; // filter
			  break;
			}
		}
	  }	
	}
	memset (wifi_send, '\0', 50);  // clear the buffer
	memset (display, '\0', 140);     // clear the buffer
	count=0;
}
if(strstr(RX_ST,"load1"))
    {
      HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_1);
      if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_1))
      OLCD_write_string(5,1,"LOAD 1 ON      ");
      else OLCD_write_string(5,1,"LOAD 1 OFF     ");
	  count=0;
      memset (RX_ST, '\0', 140);     // clear the buffer
	  memset (display, '\0', 140);     // clear the buffer
    }
char fan_display[16];
uint8_t b_fan,duty=50;
if(strstr(RX_ST,"fan"))
    {
      b_fan=b_fan^0x01;
      if(b_fan)
      {
        HAL_TIM_PWM_Start(&htim15,TIM_CHANNEL_1);
        sprintf(fan_display,"FAN Sp:%i    ",duty);
        OLCD_write_string(6,1,fan_display);
      }else{
        HAL_TIM_PWM_Stop(&htim15,TIM_CHANNEL_1);
        OLCD_write_string(6,1,"FAN OFF        ");
      }
	  count=0;
      memset (RX_ST, '\0', 140);     // clear the buffer
	  memset (display, '\0', 140);     // clear the buffer
    }

For changing duty cycle, the code will be-

if(strstr(RX_ST,"increment"))
    {
     int speed=2;
      if(b_fan)
      {
       speed++;
       if(speed>=5)speed=4;
       switch(speed)
       {
       case 0: TIM15->CCR1=4999; //for 25% duty cycle
       OLCD_write_string(6,1,"FAN ON SP:25% ");
       duty=25;
       break;
       case 1: TIM15->CCR1=9999; //for 50% duty cycle
       OLCD_write_string(6,1,"FAN ON SP:50% ");
       duty=50;
       break;
       case 2: TIM15->CCR1=14999;//for 75% duty cycle
       OLCD_write_string(6,1,"FAN ON SP:75% ");
       duty=75;
       break;
       case 3: TIM15->CCR1=17999;//for 90% duty cycle
       OLCD_write_string(6,1,"FAN ON SP:90%  ");
       duty=90;
       break;
			 case 4: TIM15->CCR1=19998;//for 100% duty cycle
       OLCD_write_string(6,1,"FAN ON SP:100%  ");
       duty=100;
       break;
       }
      }
	  count=0;
      memset (RX_ST, '\0', 140);     // clear the buffer
	  memset (display, '\0', 140);     // clear the buffer
    }
if(strstr(RX_ST,"decrement"))
    {
     int speed=2;
      if(b_fan)
      {
       speed--;
       if(speed<=0)speed=0;
       switch(speed)
       {
       case 0: TIM15->CCR1=4999; //for 25% duty cycle
       OLCD_write_string(6,1,"FAN ON SP:25% ");
       duty=25;
       break;
       case 1: TIM15->CCR1=9999; //for 50% duty cycle
       OLCD_write_string(6,1,"FAN ON SP:50% ");
       duty=50;
       break;
       case 2: TIM15->CCR1=14999;//for 75% duty cycle
       OLCD_write_string(6,1,"FAN ON SP:75% ");
       duty=75;
       break;
       case 3: TIM15->CCR1=17999;//for 90% duty cycle
       OLCD_write_string(6,1,"FAN ON SP:90% ");
       duty=90;
       break;
	   case 4: TIM15->CCR1=19998;//for 100% duty cycle
       OLCD_write_string(6,1,"FAN ON SP:100%  ");
       duty=100;
       break;
       }
      }
	  count=0;
      memset (RX_ST, '\0', 140);     // clear the buffer
	  memset (display, '\0', 140);     // clear the buffer
		}