Problem Regarding the sending of the PING packet

Hello Guys,

I’m using the STM32L073RZ MCU. So basically after the connect packet is established through the AT Commands, I publish every 1 minute to the MQTT broker, while the keep-alive time is 60 seconds. The publish packet executes every one minute according to the RTC timer interrupt. I want to send the PING packet every 15 seconds or 30 seconds. Currently, the PING packet is being executed without any issues in the while loop which results in frequent execution. So when I change the execution of the PING packet to every 15 seconds it doesn’t go through. For example, it doesn’t receive the reply (">") after the CIPSEND AT command is executed.

main.c

  extern volatile int on;
  extern volatile int do_ping;
  extern int wakeup;

  ConnectPacket();

  if (HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 15*2000, RTC_WAKEUPCLOCK_RTCCLK_DIV16) != HAL_OK)
  {
   Error_Handler();
  }


 while(1){

 if(on == 0 && wakeup==6){
      Count();
      temp();
      time();
      mqtt_id =+1;
      sprintf(strl,"ID:%d,0-CT:%u, 1-BT:320, 2-RT:%02d:%02d:%02d, 3-TEMP: %d",mqtt_id,count1, sTime.Hours,sTime.Minutes,sTime.Seconds, temperature3);
      publishPacket(strl);
      wakeup=0;
  }


  if (on ==1 && wakeup == 4){
      Count();
      temp();
      time();
      mqtt_id =+1;
      sprintf(strl,"ID:%d,0-CT:%u, 1-BT:320, 2-RT:%02d:%02d:%02d, 3-TEMP: %d",mqtt_id,count1, sTime.Hours,sTime.Minutes,sTime.Seconds, temperature3);
      publishPacket(strl);
      wakeup=0;
    }
      if(do_ping == 1){
	      do_ping = 0;
  	      mqtt_id =+1;
  	      sprintf(strl,"ID:%d,0-CT:%u,1-BT:320,2-RT:%02d:%02d:%02d",mqtt_id,count1,sTime.Hours,sTime.Minutes,sTime.Seconds);	  
	      pingPacket();

      }
 }

do_ping increments every 15 seconds in the it.c via the rtc interrupt

it.c

int wakeup=0;
volatile int on = 0;
volatile int do_ping = 0;

void RTC_IRQHandler(void)
{
/* USER CODE BEGIN RTC_IRQn 0 */

/* USER CODE END RTC_IRQn 0 */
HAL_RTCEx_WakeUpTimerIRQHandler(&hrtc);
/* USER CODE BEGIN RTC_IRQn 1 */
wakeup = wakeup + 1;

if ((wakeup == 1) || (wakeup == 2) || (wakeup == 3) || (wakeup ==5)){
do_ping = 1;
}

if(wakeup ==4 && on == 0){
do_ping = 1;
}
/* USER CODE END RTC_IRQn 1 */
}

MQTT.c

```
void MQTTdata(uint8_t Raddress, uint16_t Rsize){

    while (ongoing) {
        switch (downloadState)
        {
        case 0:
            AT_Command((uint8_t*)"AT\r\n");
            HAL_UART_Receive(&huart1, gsmreply, sizeof(gsmreply), RX_TIMEOUT);
            if (strstr((char*)gsmreply, "OK"))downloadState = 1;
            else downloadState = 0;
            memset(gsmreply, 0, sizeof(gsmreply));
            break;
        case 1:
            AT_Command((uint8_t*)"AT+CSCLK=0\r\n");
            HAL_UART_Receive(&huart1, gsmreply, sizeof(gsmreply), RX_TIMEOUT);
            if (strstr((char*)gsmreply, "OK"))downloadState = 2;
            else downloadState = 1;
            memset(gsmreply, 0, sizeof(gsmreply));
            break;
        case 2:
            AT_Command((uint8_t*)"AT+CIPSHUT\r\n");
            HAL_UART_Receive(&huart1, gsmreply, sizeof(gsmreply), RX_TIMEOUT);
            if (strstr((char*)gsmreply, "OK"))downloadState = 3;
            else downloadState = 2;
            memset(gsmreply, 0, sizeof(gsmreply));
            break;
        case 3:
            AT_Command((uint8_t*)"AT+CGREG?\r\n");
            HAL_UART_Receive(&huart1, (uint8_t*)gsmreply, sizeof(gsmreply), RX_TIMEOUT);
            if (strstr((char*)gsmreply, "OK"))downloadState = 4;
            else downloadState = 3;
            memset(gsmreply, 0, sizeof(gsmreply));
            break;
        case 4:
            AT_Command((uint8_t*)"AT+CGATT=1\r\n");
            HAL_UART_Receive(&huart1, (uint8_t*)gsmreply, sizeof(gsmreply), RX_TIMEOUT);
            if (strstr((char*)gsmreply, "OK"))downloadState = 5;
            else downloadState = 4;
            memset(gsmreply, 0, sizeof(gsmreply));
            break;
        case 5:
            AT_Command((uint8_t*)"AT+CSTT=\"apn\",\"\",\"\"\r\n");
            HAL_UART_Receive(&huart1, (uint8_t*)gsmreply, sizeof(gsmreply), RX_TIMEOUT);
            if (strstr((char*)gsmreply, "OK"))downloadState = 6;
            else downloadState = 5;
            memset(gsmreply, 0, sizeof(gsmreply));
            break;
        case 6:
            AT_Command((uint8_t*)"AT+CIICR\r\n");
            HAL_UART_Receive(&huart1, (uint8_t*)gsmreply, sizeof(gsmreply), RX_TIMEOUT);
            if (strstr((char*)gsmreply, "OK"))downloadState = 7;
            else downloadState = 6;
            memset(gsmreply, 0, sizeof(gsmreply));
            break;
        case 7:
            AT_Command((uint8_t*)"AT+CIFSR\r\n");
            HAL_UART_Receive(&huart1, (uint8_t*)gsmreply, sizeof(gsmreply), RX_TIMEOUT);
            downloadState = 8;
            memset(gsmreply, 0, sizeof(gsmreply));
            break;
        case 8:
            AT_Command((uint8_t*)"AT+CIPSTART=\"TCP\",\"example\",\"port\"\r\n");
            HAL_UART_Receive(&huart1, (uint8_t*)gsmreply, sizeof(gsmreply), RX_TIMEOUT);
            if (strstr((char*)gsmreply, "OK"))downloadState = 9;
            else downloadState = 8;
            memset(gsmreply, 0, sizeof(gsmreply));
            break;
        case 9:
            AT_Command((uint8_t*)"AT+CIPSEND\r\n");
            HAL_UART_Receive(&huart1, (uint8_t*)gsmreply, sizeof(gsmreply), RX_TIMEOUT);
            if (strstr((char*)gsmreply, ">")){
                downloadState = 10;
            }
            else downloadState = 9;
            memset(gsmreply, 0, sizeof(gsmreply));
            break;
        case 10:
            HAL_UART_Transmit(&huart1, payload, 100, 30000);
            HAL_UART_Receive(&huart1, (uint8_t*)gsmreply, sizeof(gsmreply), 30000);
            //if (strstr((char*)gsmreply, "SEND OK"))downlaodState = 10;
            //else
            downloadState = 0;
            ongoing = 0;
            memset(gsmreply, 0, sizeof(gsmreply));
            break;
        }
    }
}

void Pingdata(uint8_t Raddress, uint16_t Rsize){

    while (ongoing) {
        switch (downloadState)
        {
        case 9:
            AT_Command((uint8_t*)"AT+CIPSEND\r\n");
            HAL_UART_Receive(&huart1, (uint8_t*)gsmreply, sizeof(gsmreply), RX_TIMEOUT);
            if (strstr((char*)gsmreply, ">")){
                downloadState = 10;
            }
            else downloadState = 9;
            memset(gsmreply, 0, sizeof(gsmreply));
            break;
        case 10:
            HAL_UART_Transmit(&huart1, payload, 100, 30000);
            HAL_UART_Receive(&huart1, (uint8_t*)gsmreply, sizeof(gsmreply), 30000);
            downloadState = 0;
            ongoing = 0;
            memset(gsmreply, 0, sizeof(gsmreply));
            break;
        }
    }
}

void ConnectPacket()
{
    ongoing = 1;
    MsgConstruct();
    MQTTdata(0, 100);
    
}

void pingPacket(){
    ongoing = 1;
    pingmsgConstruct();
    downloadState = 9;
    Pingdata(0, 100);
}
```

I kindly request your help in this matter.