Unable to transmit MQTT packet via SIM800L

mqtt

#1

Hello,
I am trying to connect to MQTT broker : broker.mqttdashboard.com via SIM800L
Everything going ok and i do get “SEND OK” but the server is not receiving the packet.
Can you please help?

Below is the code:
#define RESET_PIN 6
int modemStatus = 0;

  void initModem() {
  int startTime = 0;
  switch (modemStatus) {
    case 0:
      Serial1.print("ATE0\r\n");
      modemStatus = 1;
      delay(2000);  
      Serial1.print("AT\r\n");
      delay(2000);
      
      break;
    case 1:
      Serial1.print("AT+CFUN?\r\n");
      delay(2000);
      modemStatus = 2;
      break;
    case 2:
      Serial1.print("AT+CSTT=\"portalnmms\",\"\",\"\"\r\n");
      delay(2000);
      modemStatus = 3;
      break;
    case 3:
      Serial1.print("AT+CIICR\r\n");
      delay(2000);
      modemStatus = 4;
      break;
    case 4:
      Serial1.print("AT+CIFSR\r\n");
      delay(2000);
      modemStatus = 5;
      break;
    case 5:
      Serial1.print("AT+CIPSTART=\"TCP\",\"broker.mqttdashboard.com\",\"1883\"\r\n");
      //Serial1.print("AT+CIPSTART=\"TCP\",\"broker.hivemq.com\",\"1883\"\r\n");
      //Serial1.print("AT+CIPSTART=\"TCP\",\"io.adafruit.com\",\"1883\"\r\n");
      
      delay(5000);
      modemStatus = 6;
      break;
    
    case 6:     
      
      Serial.print("Sending.....  AT+CIPSEND\r\n");
      Serial1.print("\r\nAT+CIPSEND\r\n");  
      delay(3000);
      modemStatus = 7;
      break;

    case 7:

      Serial.println("");
      Serial.println("Sending Connect Packet");

     /* static const char connectPacket[26] = {
        0x10,   //Control Byte - 4Bits(MSB) for Control Type & 4Bits(LSB) Control Flag
        0x0C + 0x0C,     //Length of Packet
        0x00,       //Protocol Name Length (MSB)
        0x04,       //Protocol Name Length (LSB)
        0x4D,       //  'M',        // Protocol Name 1st Byte
        0x51,       //  'Q',        // Protocol Name 2nd Byte
        0x54,       //  'T',        // Protocol Name 3rd Byte
        0x54,       //  'T',        // Protocol Name 4th Byte
        0x04,       // Protocol Level/Version (3.1 in this case)
        0x02,       // Connect Flags
        0x00,       // Keep Alive in Seconds (MSB)
        0x3D,       // Keep Alive in Seconds (LSB)
        0x00,       // Client Identifier Length (MSB)
        0x0C,       // Client Identifier Length (LSB)
        'M', 'Q', 'T', 'T', '-', '-', 'c', 'l', 'i', 'e', 'n', 't'
      }; */

      static const char connectPacket[31] = {
        0x10,   //Control Byte - 4Bits(MSB) for Control Type & 4Bits(LSB) Control Flag
        0x00,
        0x00,        
        0x00,
        0x0E + 0x0C,     //Length of Packet        
        0x00,       //Protocol Name Length (MSB)
        0x06,       //Protocol Name Length (LSB)
        0x4D,       //  'M',        // Protocol Name 1st Byte
        0x51,       //  'Q',        // Protocol Name 2nd Byte
        0x49,       //  'I',        // Protocol Name 3rd Byte
        0x73,       //  's',        // Protocol Name 4th Byte
        0x64,       //  'd'         // Protocol Name 5th Byte
        0x70,       //  'p'         // Protocol Name 6th Byte
        0x03,       // Protocol Level/Version (3.1 in this case)
        0x02,       // Connect Flags
        0x00,       // Keep Alive in Seconds (MSB)
        0x3D,       // Keep Alive in Seconds (LSB)
        0x00,       // Client Identifier Length (MSB)
        0x0C,       // Client Identifier Length (LSB)
        0x4D,       //  'M', 
        0x51,       //  'Q', 
        0x54,       //  'T', 
        0x54,       //  'T', 
        0x2D,       //  '-', 
        0x2D,       //  '-', 
        0x63,       //  'c', 
        0x6C,       //  'l', 
        0x69,       //  'i', 
        0x65,       //  'e', 
        0x6E,       //  'n', 
        0x74        //  't'
      };

      
      for (int i = 0; i < strlen(connectPacket); i++)
      {
        Serial1.write(connectPacket[i]);
        Serial1.flush();
      }

      Serial1.write(0x1A);        
      Serial.println("CONNECT Packet Sent...");
      delay(5000);  
      modemStatus = 8;
      break;
    case 8:
      //PUBLISH A MESSAGE
      
      Serial.println("");
      Serial.println("Publishing data...");
      Serial1.print("AT+CIPSEND\r\n");
      delay(1000);
      static const char publishPacket[14] = {0x30,
                                             0X0C,
                                             0x00,
                                             0x05,
                                             0x61,  //  'a',
                                             0x2F,  //  '/',
                                             0x62,  //  'b',
                                             0x2F,  //  '/',
                                             0x63,  //  'c',
                                             0x68,  //  'h'
                                             0x65,  //  'e'
                                             0x6C,  //  'l'
                                             0x6C,  //  'l'
                                             0x6F,  //  'o'
                                          };

      for (int i = 0; i < strlen(publishPacket); i++){
        Serial1.write(publishPacket[i]);
        Serial1.flush();
      }

      Serial1.write(0x1A);        
      Serial1.flush();
      modemStatus = 9;
      break;
    case 9:
      break;
    default:
      Serial.println("Invalid Modem Status");
      break;
  }
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial1.begin(9600);

  //RESET MODEM  
  Serial.println("Init Modem");
  pinMode(RESET_PIN, OUTPUT);
  digitalWrite(RESET_PIN, LOW);
  delay(1000);
  digitalWrite(RESET_PIN, HIGH);
  delay(10000);

}


void loop() {
  initModem();

  // put your main code here, to run repeatedly:
  if (Serial.available()) {
    Serial1.println(Serial.readString());
  }

}

void serialEvent1() {
    Serial.print("\nSerialEvent begin **********************, Modem Status =");
    Serial.println(modemStatus);
  if (Serial1.available()) {
    while (Serial1.available())
    {
        Serial.print((char)Serial1.read());  
    }
  }
    Serial.print("\nSerialEvent ends **********************, Modem Status =");
    Serial.println(modemStatus);

}

Below is the output on my serial monitor:
Init Modem

SerialEvent begin **********************, Modem Status =1
ATE0
ATE0

OK
AT

OK

SerialEvent ends **********************, Modem Status =1

SerialEvent begin **********************, Modem Status =2
AT+CFUN?

+CFUN: 1

OK

SerialEvent ends **********************, Modem Status =2

SerialEvent begin **********************, Modem Status =3
AT+CSTT="portalnmms","",""

OK

SerialEvent ends **********************, Modem Status =3

SerialEvent begin **********************, Modem Status =4
AT+CIICR

OK

SerialEvent ends **********************, Modem Status =4

SerialEvent begin **********************, Modem Status =5
AT+CIFSR

10.119.13.176

SerialEvent ends **********************, Modem Status =5

SerialEvent begin **********************, Modem Status =6
AT+CIPSTART="TCP","broker.mqttdashboard.com","1883"

OK

CO
SerialEvent ends **********************, Modem Status =6
Sending.....  AT+CIPSEND

SerialEvent begin **********************, Modem Status =7

AT+CIPSEND

> 
SerialEvent ends **********************, Modem Status =7

Sending Connect Packet
CONNECT Packet Sent...

SerialEvent begin **********************, Modem Status =8

SEND OK

SerialEvent ends **********************, Modem Status =8

Publishing data...

SerialEvent begin **********************, Modem Status =9
AT+CIPSEND

> 0
SerialEvent ends **********************, Modem Status =9

SerialEvent begin **********************, Modem Status =9

SerialEvent ends **********************, Modem Status =9

SerialEvent begin **********************, Modem Status =9

SEND OK

SerialEvent ends **********************, Modem Status =9

SerialEvent begin **********************, Modem Status =9

SerialEvent ends **********************, Modem Status =9

SerialEvent begin **********************, Modem Status =9

CLOSED

SerialEvent ends **********************, Modem Status =9

Please help.

Regards
Sandeep