Problem with tx_event_flags_get funtion in NetXDuo MQTT application on STM32F4

Kirsten Jens 1 Reputation point
2023-01-23T09:00:42.9333333+00:00

An MQTT client based on NetXDuo on a STM32F4 controller subscribes successfully to a topic and also receives successfully data on the subsribed topic from the broker.

The low level driver for MQTT receives all messages, however the function tx_event_flags_get is not reporting the last up to 8 messages.

This tx_event_flags_get function reveals these last messages only when further messages are sent by the broker.

It looks like a pointer malfunctioning in a ring buffer, but we could not figure out, where this is happening.

In our case, we send 1000 messages and receive 992, messages 993 to 1000 we receive, when we send message 1001 to 1008.

Messages 1001 to 1008 are seen only when more messages are sent.

Azure IoT
Azure IoT
A category of Azure services for internet of things devices.
397 questions
Azure RTOS
Azure RTOS
An Azure embedded development suite including a small but powerful operating system for resource-constrained devices.
331 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Kirsten Jens 1 Reputation point
    2023-01-26T16:26:25.9+00:00

    Hi Cindy,

    please see the code below.

    The code should work like this:

    • We have a MQTT broker running on an Ubuntu Linux machine in the local network.
    • We start our application, which should collect 1000 messages and then quit the application.
    • We then start a MQTT client running on an Ubuntu Linux machine that publishes on the same topic each time a 25 Byte long message, with a counter included in the message, for 1000 times. The sending is controlled by a Python script and amounts to about max. 5 messages per second.
    • Our client, the one on the STM32F4 receives these messages, the "tx_event_flags_get" reports only on about 992, sometimes 993.
    • How do we know, that all messages where received?
      • first, we have a second subscribing client on the Ubuntu machine, this client reports all messages, hence the broker receives all published messages and sends them all to the subcribers.
      • second, we restart the above mentioned python script, now the missing messages come first and this time, the counter only goes to 984, (if we set our counter for remaining_msg let us say to 2000 or 3000.
      • alternatively to second, we can trigger 8 more single messages from the publishing client and we will see the "lost" messages first, before the single messages are shown.

    Hence, the application works nicely, but the messages are not shown as received till the end.

    #define DEMO_MESSAGE_EVENT 1
    #define DEMO_ALL_EVENTS 3
    
    
    
      /* Subscribe to the topic with QoS level 1. */
      ret = nxd_mqtt_client_subscribe(&mqtt_client, 
                                      TOPIC_NAME, 
                                      STRLEN(TOPIC_NAME), 
                                      QOS1);
      if (ret != NX_SUCCESS) { Error_Handler(); }  
    
      message_count = 0;
      remaining_msg = NB_MESSAGE;
      
      while(remaining_msg)  
      { 
        waitForMessage = 1;
        /* Now wait for the broker to publish the message. */
    
        while(waitForMessage)
        {
          waitForMessage = tx_event_flags_get(&mqtt_app_flag, 
                                              DEMO_ALL_EVENTS,
                                              TX_OR_CLEAR, 
                                              &events, 
                                              1); //TX_WAIT_FOREVER);
        }
        
    
        if(events & DEMO_MESSAGE_EVENT)
        {
          /* Try to get message from the broker */
          ret = nxd_mqtt_client_message_get(&mqtt_client, 
                                            topic_buffer, 
                                            sizeof(topic_buffer), 
                                            &topic_length,
                                            message_buffer, 
                                            sizeof(message_buffer), 
                                            &message_length);
            
            topic_buffer[topic_length] = 0;
            message_buffer[message_length] = 0;
    
            if(ret == NXD_MQTT_SUCCESS)
            {
              remaining_msg -- ;
              message_count ++ ;
            }
            else if(ret == NXD_MQTT_NO_MESSAGE)
            {
              // do nothing
            }
            else
            {
              Error_Handler();
            }
        }
      }
      
      /* Now unsubscribe the topic. */
      ret = nxd_mqtt_client_unsubscribe(&mqtt_client, TOPIC_NAME, STRLEN(TOPIC_NAME));
      if (ret != NX_SUCCESS) { Error_Handler(); }
    
    
    0 comments No comments

  2. Kirsten Jens 1 Reputation point
    2023-01-26T16:27:07.1966667+00:00

    Hi Cindy,

    please see the code below.

    The code should work like this:

    • We have a MQTT broker running on an Ubuntu Linux machine in the local network.
    • We start our application, which should collect 1000 messages and then quit the application.
    • We then start a MQTT client running on an Ubuntu Linux machine that publishes on the same topic each time a 25 Byte long message, with a counter included in the message, for 1000 times. The sending is controlled by a Python script and amounts to about max. 5 messages per second.
    • Our client, the one on the STM32F4 receives these messages, the "tx_event_flags_get" reports only on about 992, sometimes 993.
    • How do we know, that all messages where received?
    • first, we have a second subscribing client on the Ubuntu machine, this client reports all messages, hence the broker receives all published messages and sends them all to the subcribers.
    • second, we restart the above mentioned python script, now the missing messages come first and this time, the counter only goes to 984, (if we set our counter for remaining_msg let us say to 2000 or 3000.
    • alternatively to second, we can trigger 8 more single messages from the publishing client and we will see the "lost" messages first, before the single messages are shown. Hence, the application works nicely, but the messages are not shown as received till the end.
    #define DEMO_MESSAGE_EVENT 1
    #define DEMO_ALL_EVENTS 3
    
    
    
      /* Subscribe to the topic with QoS level 1. */
      ret = nxd_mqtt_client_subscribe(&mqtt_client, 
                                      TOPIC_NAME, 
                                      STRLEN(TOPIC_NAME), 
                                      QOS1);
      if (ret != NX_SUCCESS) { Error_Handler(); }  
    
      message_count = 0;
      remaining_msg = NB_MESSAGE;
      
      while(remaining_msg)  
      { 
        waitForMessage = 1;
        /* Now wait for the broker to publish the message. */
    
        while(waitForMessage)
        {
          waitForMessage = tx_event_flags_get(&mqtt_app_flag, 
                                              DEMO_ALL_EVENTS,
                                              TX_OR_CLEAR, 
                                              &events, 
                                              1); //TX_WAIT_FOREVER);
        }
        
    
        if(events & DEMO_MESSAGE_EVENT)
        {
          /* Try to get message from the broker */
          ret = nxd_mqtt_client_message_get(&mqtt_client, 
                                            topic_buffer, 
                                            sizeof(topic_buffer), 
                                            &topic_length,
                                            message_buffer, 
                                            sizeof(message_buffer), 
                                            &message_length);
            
            topic_buffer[topic_length] = 0;
            message_buffer[message_length] = 0;
    
            if(ret == NXD_MQTT_SUCCESS)
            {
              remaining_msg -- ;
              message_count ++ ;
            }
            else if(ret == NXD_MQTT_NO_MESSAGE)
            {
              // do nothing
            }
            else
            {
              Error_Handler();
            }
        }
      }
      
      /* Now unsubscribe the topic. */
      ret = nxd_mqtt_client_unsubscribe(&mqtt_client, TOPIC_NAME, STRLEN(TOPIC_NAME));
      if (ret != NX_SUCCESS) { Error_Handler(); }
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.