Problem while receiving packets from cloudmqtt.com to Arduino

Hello Ravi,

I have written a code to receive packets from cloudmqtt.com with the reference of your code but I can’t figure out why While loop not receiving topic and message packet and serial monitor showing continuously some garbage.

  unsigned int Counter = 0;
  unsigned long datalength,CheckSum,RLength;
  unsigned short topiclength;
  unsigned char topic[30];
  char str[250];
  unsigned char encodedByte;
  int X; 
  unsigned short MQTTProtocolNameLength;
  unsigned short MQTTClientIDLength;
  unsigned short MQTTUsernameLength;
  unsigned short MQTTPasswordLength;
  unsigned short MQTTMsgLength;
  unsigned short MQTTtopicLength;
       
 
  const char MQTTHost[30] = "m14.cloudmqtt.com";
  const char MQTTPort[10] = "19262";
  const char MQTTClientID[20] = "ABCDEF";
  const char MQTTTopic[30] = "MSG_SEND";
  const char MQTTProtocolName[10] = "MQIsdp";
  const char MQTTMSG[30]="E_FUNNEL";
  const char MQTTLVL = 0x03;
  const char MQTTFlags = 0xC2;
  const unsigned int MQTTKeepAlive = 60;
  const char MQTTUsername[30] = "yuekjdjr";
  const char MQTTPassword[35] = "572hxEXnhRxS";
  const char MQTTQOS = 0x00;
  const char MQTTPacketID = 0x0001;
 

void setup() 
{
  Serial.begin(9600);
  while(!Serial)
  {
    ;  
  }
  delay(1000);
  gsm_connect();
  Serial.print("GSM started \n");
  gprs_connect();
  Serial.print("GPRS started \n");
  TCP_IP_connect();
  Serial.print("TCP connection establish \n");
  SendConnectPacket();
  Serial.print("Connect packet send \n");
  delay(100);
  
}
/**************************************************************************/
void loop()
{
 // SendPublishPacket();
  SendSubscribePacket();
  Serial.print("Msg send successfully \n");
  delay(100);
 while(1)
 {
          if(Serial.available()>0)
          {
             str[0] = Serial.read();
             Serial.write(str[0]);
                 
          } 
        
 }  
}
/******************************************************************/

void gsm_connect(void)
{
  Serial.println("GSM initializing......");
  delay(100);
  Serial.print("AT+CREG?\r\n");
  delay(1000);
  Serial.print("AT+CSQ\r\n");
  delay(1000);
}

void gprs_connect(void)
{
  Serial.println("GPRS initialized.....\n");
  delay(100);
  Serial.print("AT+CGATT=1\r\n");
  delay(1000);
  
}

void TCP_IP_connect(void)
{
  Serial.println("TCP_IP connection initializing.....\n");
  delay(100);
  TCP_IP_disconnect();
  delay(500);
  Serial.print("AT+CSTT=\"internet\",\"\",\"\"\r\n");
  delay(1000);
  Serial.print("AT+CIPMODE=0\r\n");    
  delay(1000);
  Serial.print("AT+CIICR\r\n"); 
  delay(5000);
  Serial.print("AT+CIFSR\r\n");
  delay(5000);
  Serial.print("AT+CIPSTART=\"TCP\",\"m14.cloudmqtt.com\",\"19262\"\r\n");
  delay(4000); 
}

void TCP_IP_disconnect(void)
{
  Serial.println("TCP_IP disconnecting.....");
  delay(100);
  Serial.print("AT+CIPSHUT\r\n");
  delay(1000);
}

void gprs_disconneect(void)
{
  Serial.println("GPRS disconnecting........."); 
  delay(100);
  Serial.print("AT+CGATT=0\r\n");
  delay(1000);
}  
/******************************************************************************/
  void SendConnectPacket(void)
  {
    Serial.print("\r\nAT+CIPSEND\r\n");
    delay(3000);
    
    Serial.write(0x10);
    
    MQTTProtocolNameLength = strlen(MQTTProtocolName);
    MQTTClientIDLength = strlen(MQTTClientID);
    MQTTUsernameLength = strlen(MQTTUsername);
    MQTTPasswordLength = strlen(MQTTPassword);
    datalength = MQTTProtocolNameLength+2+4+MQTTClientIDLength+2+MQTTUsernameLength+2+MQTTPasswordLength+2;
    delay(2000);
    X=datalength;
    do
    {
        encodedByte = X%128;
        X = X/128;
        // if there are more data to encode, set the top bit of this byte
        if ( X > 0 ) { encodedByte |= 128; }
        
         Serial.write(encodedByte);
    }
    while ( X > 0 );
    
    
    Serial.write(MQTTProtocolNameLength >> 8);
    Serial.write(MQTTProtocolNameLength & 0xFF);
    Serial.print(MQTTProtocolName);
    
    Serial.write(MQTTLVL); // LVL
    Serial.write(MQTTFlags); // Flags
    Serial.write(MQTTKeepAlive >> 8);
    Serial.write(MQTTKeepAlive & 0xFF);
    
    
    Serial.write(MQTTClientIDLength >> 8);
    Serial.write(MQTTClientIDLength & 0xFF);
    Serial.print(MQTTClientID);
    
    
    Serial.write(MQTTUsernameLength >> 8);
    Serial.write(MQTTUsernameLength & 0xFF);
    Serial.print(MQTTUsername);
    
    
    Serial.write(MQTTPasswordLength >> 8);
    Serial.write(MQTTPasswordLength & 0xFF);
    Serial.print(MQTTPassword);
    
    Serial.write(0x1A);

    delay(5000);
    
}
/**************************************************************/
void SendPublishPacket(void)
{
      Serial.print("\r\nAT+CIPSEND\r\n");
      delay(2000);
      
      MQTTtopicLength = strlen(MQTTTopic);
      MQTTMsgLength = strlen(MQTTMSG);
      datalength = MQTTtopicLength+MQTTMsgLength;
      delay(2000);
      Serial.write(0x30);
      X=datalength+4;
      do
      {
          encodedByte = X%128;
          X = X/128;
          // if there are more data to encode, set the top bit of this byte
          if ( X > 0 ) { encodedByte |= 128; }          
           Serial.write(encodedByte);
      }
      while ( X > 0 );
      
      Serial.write(MQTTtopicLength >> 8);
      Serial.write(MQTTtopicLength & 0xFF);
      Serial.print(MQTTTopic);

       Serial.write(MQTTMsgLength >> 8);
      Serial.write(MQTTMsgLength & 0xFF);
      Serial.print(MQTTMSG);
      
      Serial.write(0x1A); 
      delay(5000);
     }

/***************************************************************************/
void SendSubscribePacket(void)
{
      Serial.print("\r\nAT+CIPSEND\r\n");
      delay(3000);

      memset(str,0,250);
      topiclength = strlen(MQTTTopic);            
      datalength = 2+2+topiclength+1;
      delay(1000);

      Serial.write(0x82);
      X=datalength;
      do
      {
          encodedByte = X%128;
          X = X/128;
          // if there are more data to encode, set the top bit of this byte
          if ( X > 0 ) { encodedByte |= 128; }         
          Serial.write(encodedByte);
      }
      while ( X > 0 );
      Serial.write(MQTTPacketID >> 8);
      Serial.write(MQTTPacketID & 0xFF);
      Serial.write(topiclength >> 8);
      Serial.write(topiclength & 0xFF);
      Serial.print(MQTTTopic);
      Serial.write(MQTTQOS);
      
      Serial.write(0x1A);     
}

You can try Peters working code, @mbari