paho.mqtt.embedded-c icon indicating copy to clipboard operation
paho.mqtt.embedded-c copied to clipboard

embedded mqtt send / receive tasks hung-up when running concurrently on STM32

Open ddll184 opened this issue 8 years ago • 1 comments

So I am using paho.mqtt.embedded-c MQTT library on board STM32F107, with LWIP 1.4.1 as tcp/ip stack library, freeRTOS as embedded OS. With reference to MQTTEcho.c example code, I created mqtt send and recv tasks separately.

While doing test work with mqtt.fx tool, I found that if the data transmit frequency is not very high, both send and recv task work well, if the frequency is high enough, there will be a good chance that MQTTPublish method does not return, thus both send and recv task got suspended.

1)STACK SIZE and PRIORITY of the two tasks are as follows #define MQTT_RECVTASK_PRIORITY ( tskIDLE_PRIORITY + 3 ) #define MQTT_SENDTASK_PRIORITY ( tskIDLE_PRIORITY + 3 ) #define MQTT_RECVTASK_STACKSIZE configMINIMAL_STACK_SIZE * 6 //768bytes #define MQTT_SENDTASK_STACKSIZE configMINIMAL_STACK_SIZE * 4 //512bytes

2)The detailed implementation of the task is as follows

void mqttRecvMessageTask(void *pvParameters)
{
    Timer timer;
    TimerInit(&timer);
    int rc = 0;

    for (;;)
    {
        while (g_hostState.mqttState < MQTT_SUBSCRIBED)
        {
            vTaskDelay(200);
            continue;
        }

        TimerCountdownMS(&timer, 500); /* Don't wait too long if no traffic is incoming */
        if(MQTT_PINGERR == cycle(&client, &timer))
        {
            LED_GREEN_OFF();
            LED_BLUE_ON();
            PDEBUG("PING ERROR\r\n");
            MQTTDisconnect(&client);
            FreeRTOS_disconnect(&network);
            g_hostState.mqttState = MQTT_READY;
        }
    }
}
void mqttSendMessageTask(void *pvParameters)
 {
   int rc = 0;

   for (;;)
   {
       while (g_hostState.mqttState < MQTT_CONNECTED)
       {
           vTaskDelay(200);
           continue;
       }

       MessageBuffer *pSendData = NULL;
       BaseType_t xStatus = xQueueReceive(mqttSendQueue, &pSendData, 100);
       if (xStatus == pdPASS)
       {
          if ((rc = MQTTPublish(&client, pSendData->topic, &pSendData->message)) != MQTT_SUCCESS)
          {
            LED_GREEN_OFF();
            LED_BLUE_ON();
            PDEBUG("SEND ERROR\r\n");
            MQTTDisconnect(&client);
            FreeRTOS_disconnect(&network);
            g_hostState.mqttState = MQTT_READY;
          }
          vPortFree(pSendData->message.payload);
          vPortFree(pSendData);
          pSendData = NULL;
          PDEBUG("3\r\n");
       }
   }
}

I doubt the reason is related to that both mqtt send and recv tasks are running concurrently, because if I comment out MQTTPublish in send task, there will be no hangs any more. I noticed that the MQTTEcho.c example used MutexLock and MutexUnLock and I tried them too, but gave up because the result was out of control. Is that the key?

ddll184 avatar Nov 24 '16 10:11 ddll184