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++)
{
HAL_Delay(250);
/*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)
continue;
/*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)
continue;
/*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)
continue;
/*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)
continue;
/*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)
continue;
/*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)
continue;
/*Status*/
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)
continue;
/*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)
continue;
/*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)
continue;
/*APN settings*/
if(SIM800_SendCommand((uint8_t*)"AT+CSTT=\"\",\"\",\"\"\n", 17, (uint8_t*)"AT+CSTT=\"\",\"\",\"\"\r\nOK\r\n", 22, 100) != SIM800_OK)
continue;
/*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)
continue;
/*Did IP address get?*/
if(SIM800_SendCommand((uint8_t*)"AT+CIFSR\n", 9, (uint8_t*)"AT+CIFSR\r\n", 10, 100) != SIM800_OK)
continue;
HAL_Delay(100);
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)
continue;
else
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.