MQTT guidance needed

I am running FreeRTOS 202104.00 on a PIC32MX and currently have a simple tcp project using FreeRTOS_send and FreeRTOS_recv from the FreeRTOS+TCP library.

I would like to create the a simple MQTT client project that connects to a public broker without TLS.

(1) Since I have a working tcp library, do I need to create a transport interface? My quess is that I have to set up the MQTT client to use my existing TCP layer using the sample implementations.

(2) I have studied the multitask demo under the Windows simulator that uses the coreMQTT Agent library, but that seems like overkill for my needs. Once I get the above resolved, should I focus on the MQTT_Plain_Text demo to get a single threaded client connecting to a public broker.

Thank you.

Yes, you do need a transport interface (which is just 2 function pointers) to allow coreMQTT to send and receive data. Here is more documentation and here is an example which should work for you.


Which TCP stack are you using? There may be a transport interface available already.

Thank you very much Gaurav. The first link seems to present the Transport Interface a little more clearly that what I’ve read elsewhere, so between the two, I should be able to figure things out.

I’m not sure how to figure out which TCP stack, nor if there is already a transport interface available.

I downloaded all of FreeRTOv202104.00 and the FreeRTOS_TCP_IP.c header starts with:

  • FreeRTOS+TCP V2.3.2
  • Copyright (C) 2020, Inc. or its affiliates. All Rights Reserved.

As I mentioned in the post, I am able to send and receive information using TCP, so at least that is working.

The second link Gaurav posted is probably what you need.

Working from the example under .\coreMQTT_Windows_Simulator\MQTT_Plain_Text I have run into a build error using MPLABX with a PIC32MX.

In file included from ../using_plaintext.h:31:0,
                 from ../PlaintextMQTTExample.c:67:
../../../FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/include/FreeRTOS_Sockets.h:222:23: error: 
	expected '=', ',', ';', 'asm' or '__attribute__' before 'BaseType_t'

Looking at line 222 (static portINLINE …) in FreeRTOS_sockets.h, it looks like this:

/* The socket type itself. */
    struct xSOCKET;
    typedef struct xSOCKET         * Socket_t;
    typedef struct xSOCKET const   * ConstSocket_t;

    static portINLINE BaseType_t xSocketValid( Socket_t xSocket )
        BaseType_t xReturnValue = pdFALSE;

         * There are two values which can indicate an invalid socket:
         * FREERTOS_INVALID_SOCKET and NULL.  In order to compare against
         * both values, the code cannot be compliant with rule 11.4,
         * hence the Coverity suppression statement below.
        /* coverity[misra_c_2012_rule_11_4_violation] */
        if( ( xSocket != FREERTOS_INVALID_SOCKET ) && ( xSocket != NULL ) )
            xReturnValue = pdTRUE;

        return xReturnValue;

Any thoughts on what might be causing this compile problem? I’m using FreeRTOSv2021004.00 and it included FreeRTOS+TCP v2.3.2.

Which FreeRTOS port are you using? What is the definition of portINLINE for your port? Does the error go away if you remove portINLINE from this line?

I am using the MPLAB/PIC32MX port:

The definition of portINLINE in FreeRTOSIPConfigDefaults.h is:
#define portINLINE inline

Eliminating portINLINE from line 222 of FreeRTOS_sockets.h did indeed eliminate that build error - although now I have a host of new errors to work on.

I’ll have to check if removing that line broke a previous TCP project though.

Thank you!

This seems right.

It probably means that you do not need to remove this but the actual error is somewhere else. Can you share the complete build log? Can you share your project also?

The remaining errors turned out to be my mistakes and I now have a clean build, provided I leave out portINLINE from line 222 of FreeRTOS_sockets.h.

However, I am receiving a stack overflow when I attempt to connect to the server using

xNetworkStatus = prvConnectToServerWithBackoffRetries( &xNetworkContext );

and suspect that I don’t have my NetworkConext set up correctly. I’ll see if I can figure this out from all of the tips I received and will report back.

Happy to share the build log, but now that it is clean that may not be necessary. If I get stuck, I can upload a zip file of the project as well.

I’m still getting a stack overflow when trying to connect to the server. I am building the project with using_plaintext.c, which includes using_plaintext.h, which includes transport_interface.h. (I have uploaded all three files.)

Using the debugger, I can see that the overflow occurs when calling this function:

xNetworkStatus = Plaintext_FreeRTOS_Connect( pxNetworkContext,
           mqttexampleTRANSPORT_SEND_RECV_TIMEOUT_MS );

I’ve uploaded a jpg showing the network context and I believe the broker parameters are correct:

#define democonfigMQTT_BROKER_ENDPOINT “
#define democonfigMQTT_BROKER_PORT (1883)

Any suggestions will be most welcomed!

transport_interface.h (9.9 KB)
using_plaintext.c (7.1 KB)
using_plaintext.h (4.5 KB)

Which task is generating stack overflow? Can you try increasing the stack size of that task?

It was prvMQTTDemoTask() from PlaintextMQTTExample.c. Increasing it from 200 to 400 words seems to have enabled me to connect with the broker. Onto subscribing.

Thanks for all of your help Gaurav!

Success at last! I did have to remove portINLINE from line 222 of FreeRTOS_Sockets.h and increase the stack size of the demo task above the minimum. I used 400 words. Thank you!

Glad that it worked for you.