I am working on SIM800 module and MQTT broker (Mosquitto) to design a kind of IoT device. When I try the MQTT server, I encountered this problem. The SIM800 connects MQTT server on TCP connection. You can see the code below about SIM800 TCP connection:

SIM800_Status_t SIM800_InitTCP(void)
  uint8_t count;            //trying counter
    for(count = 0; count < TRYING_LIMIT; count++)
        /*Functionality = 1 - 10 sec*/
        if(SIM800_SendCommand((uint8_t*)"AT+CFUN=1\n", 10,        (uint8_t*)"AT+CFUN=1\r\nOK\r\n", 15, 10000) != SIM800_OK)
        /*Dont sleep - 100 ms*/
        if(SIM800_SendCommand((uint8_t*)"AT+CSCLK=0\n", 11,        (uint8_t*)"AT+CSCLK=0\r\nOK\r\n", 16, 100) != SIM800_OK)
        /*Is the SIM Card OK? - 5 sec*/
        if(SIM800_SendCommand((uint8_t*)"AT+CPIN?\n", 9,        (uint8_t*)"AT+CPIN?\r\n+CPIN: READY\r\n\r\nOK\r\n", 30, 5000) != SIM800_OK)
        /*Is the SIM Card Registered?*/
        if(SIM800_SendCommand((uint8_t*)"AT+CREG?\n", 9,        (uint8_t*)"AT+CREG?\r\n+CREG: 0,1\r\n\r\nOK\r\n", 28, 100) != SIM800_OK)

        /*GPRS Status- 75 sec*/
        if(SIM800_SendCommand((uint8_t*)"AT+CGATT?\n", 10,      (uint8_t*)"AT+CGATT?\r\n+CGATT: 1\r\n\r\nOK\r\n", 28, 75000) != SIM800_OK)

        /*If there is PDP context, close it- 65sec*/
        if(SIM800_SendCommand((uint8_t*)"AT+CIPSHUT\n", 11,     (uint8_t*)"AT+CIPSHUT\r\nSHUT OK\r\n", 21, 65000) != SIM800_OK)

        if(SIM800_SendCommand((uint8_t*)"AT+CIPSTATUS\n", 13,   (uint8_t*)"AT+CIPSTATUS\r\nOK\r\n\r\nSTATE: IP INITIAL\r\n", 39, 100) != SIM800_OK)

        /*Single server connection*/
        if(SIM800_SendCommand((uint8_t*)"AT+CIPMUX=0\n", 12,    (uint8_t*)"AT+CIPMUX=0\r\nOK\r\n", 17, 100) != SIM800_OK)

        /*Transparent mode*/
        if(SIM800_SendCommand((uint8_t*)"AT+CIPMODE=1\n", 13,   (uint8_t*)"AT+CIPMODE=1\r\nOK\r\n", 18, 100) != SIM800_OK)

        /*APN settings*/
        if(SIM800_SendCommand((uint8_t*)"AT+CSTT=\"\",\"\",\"\"\n", 17, (uint8_t*)"AT+CSTT=\"\",\"\",\"\"\r\nOK\r\n", 22, 100) != SIM800_OK)

        /*GPRS enable(85 sec)*/
        if(SIM800_SendCommand((uint8_t*)"AT+CIICR\n", 9,        (uint8_t*)"AT+CIICR\r\nOK\r\n", 14, 85000) != SIM800_OK)

        /*Did IP address get?*/
        if(SIM800_SendCommand((uint8_t*)"AT+CIFSR\n", 9,        (uint8_t*)"AT+CIFSR\r\n", 10, 100) != SIM800_OK)


        if(SIM800_SendCommand((uint8_t*)"AT+CIPSTART=\"TCP\",\"123.456.78.90\",\"1883\"\n", 28 + 14, (uint8_t*)"AT+CIPSTART=\"TCP\",\"123.456.78.90\",\"1883\"\r\nOK\r\n\r\nCONNECT\r\n", 44 + 14, 160000) != SIM800_OK)
                return SIM800_OK;
    return SIM800_ERROR;

The IP address is representational. The "SIM800_SendCommand" function parameters are:

SIM800_SendCommand(uint8_t* AT_Command, uint8_t AT_Command_Len, uint8_t* Response, uint8_t Response_Len, uint32_t Timeout),

The device successfully connects to MQTT Broker by using this code. After connected with TCP, device sends "Connect Request" to MQTT server immediately. The server returns the "CONN ACK" response successfully.

After the MQTT connection, device sends "Subscribe Request" to MQTT server to subscribe the "Topic A". The server returns the "SUB ACK" response.

The problem begins after this.

I am publishing some message to "Topic A" by using my phone (an android application) or "MQTTBox" application on PC.

When I publish the messages to "Topic A" each 15 seconds or each 1 minute, there is no problem. Everythings are working correctly. But when I publish the messages to "Topic A" after wait 2 minutes from last publish, after this, doesn't go any messages to SIM800 module (device).

You can see the problem's drawing below:


I suspected that the problem is originate from SIM800 moodule, but when i tried the module just using TCP connection by different simple server application with same scenario, there is no problem. Everything is working fine.

But when I tried same scenario with MQTT, the server forget the device and doesn't send any data on TCP. Even TCP connection close message (empty message).

I really appreciate your help in solving the problem.

  • 1
    What did you set the MQTT keep alive value to when you connected to the broker. I will guess you set it to 60 seconds.
    – hardillb
    Sep 22, 2019 at 13:51
  • 1
    I set the value as 0. The keep alive mechanism is disabled. But also I tried reduce keep alive timing to 30 seconds. When the time is up, the MQTT broker breaks TCP connection. I can see the broken TCP connection from SIM800 tx pin.
    – hakanuckus
    Sep 22, 2019 at 21:08

1 Answer 1


The default keepalive for mosquitto is 60 seconds, that means if you do not send a MQTT packet to broker at least every 90 (1.5 * keepalive) seconds then the broker will assume the client is dead and disconnect it.

In the comments you say you have disabled the built in keepalive handling so this is why the broker kills the connection if you try and leave 120 seconds between messages.

The best bet is not to set a zero value, but to set a value inline with the frequency that messages will be published so as not to waste bandwidth having to send pings to keep the connection alive.

  • Thank you for answer. I tried to connect and assign 0 to keep alive by using MQTTBox (all connection configurations are same as SIM800 side) . And in this time i followed the communication packets by using Wire Shark. Although the keep alive is 0 and there is no ping request during minutes, the broker hadn't closed the MQTTBox's connecrion.
    – hakanuckus
    Sep 22, 2019 at 21:27
  • Check the broker logs, it will tell you why it disconnected
    – hardillb
    Sep 22, 2019 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.