MQTT Client Connection Object Setup

Connection's Parameters

µC/MQTT-client provides the function MQTTc_ConnSetParam to configure parameters related to the MQTT Client Connection object. The function takes as argument the type of parameter and the pointer to the parameter. The following parameter types are available :

Generic Parameters


Table - MQTTc Connection Parameters
Parameter TypeDescription
MQTTc_PARAM_TYPE_BROKER_IP_ADDR

IP Address of the MQTT broker.

This field is overwritten if broker name is set.

MQTTc_PARAM_TYPE_BROKER_NAME

Name of the MQTT broker.

This method will only work if DNS is available

This field is overwritten if broker IP address is set.

MQTTc_PARAM_TYPE_BROKER_PORT_NBR

Sets a specific port for the MQTT broker.

By default, the port is set to 1883.

MQTTc_PARAM_TYPE_INACTIVITY_TIMEOUT_S

Sets the Inactivity Timeout of the connection

This value represents the time of inactivity allowed on a connection before it is automatically closed.

MQTTc_PARAM_TYPE_CLIENT_ID_STR

Connection's Client ID. This ID should be unique amongst devices connected to broker.

MQTTc_PARAM_TYPE_USERNAME_STR

Connection's username string.

This field is optional.

MQTTc_PARAM_TYPE_PASSWORD_STR

Connection's password string.

This field is optional.

MQTTc_PARAM_TYPE_KEEP_ALIVE_TMR_SEC

Connection's keep alive, in seconds.

This value represent the maximal time between messages sent by the device to the broker.

If no message is sent for more than this time, the broker will close the connection.

MQTTc_PARAM_TYPE_WILL_CFG_PTR

Pointer to will configuration.

This field is optional.

MQTTc_PARAM_TYPE_SECURE_CFG_PTR

Pointer to secure configuration.

This field is optional.

MQTTc_PARAM_TYPE_TIMEOUT_MSConnection's timeout for socket 'Open', in milliseconds.
MQTTc_PARAM_TYPE_PUBLISH_RX_MSG_PTR

Pointer to MQTT-client message object that will be used to receive PUBLISH messages from broker.

This parameter is mandatory.


Callback options

Table - MQTTc Connection Callbacks
Parameter TypeDescription
MQTTc_PARAM_TYPE_CALLBACK_ON_COMPLSets the generic callback function to notify the application whenever an event occurs. This callback is called before every other callback and may be used in place of all the other ones.
MQTTc_PARAM_TYPE_CALLBACK_ON_CONNECT_CMPL

Sets the callback function to notify the application when a CONNECT message with the MQTT broker is completed.

This parameter is optional.

MQTTc_PARAM_TYPE_CALLBACK_ON_PUBLISH_CMPL

Sets the callback function to notify the application when a PUBLISH message with the MQTT broker is completed.

This parameter is optional.

MQTTc_PARAM_TYPE_CALLBACK_ON_SUBSCRIBE_CMPL

Sets the callback function to notify the application when a SUBSCRIBE message with the MQTT broker is completed.

This parameter is optional.

MQTTc_PARAM_TYPE_CALLBACK_ON_UNSUBSCRIBE_CMPL

Sets the callback function to notify the application when an UNSUBSCRIBE message with the MQTT broker is completed.

This parameter is optional.

MQTTc_PARAM_TYPE_CALLBACK_ON_PINGREQ_CMPL

Sets the callback function to notify the application when a PINGREQ message with the MQTT broker is completed.

This parameter is optional.

MQTTc_PARAM_TYPE_CALLBACK_ON_DISCONNECT_CMPL

Sets the callback function to notify the application when a DISCONNECT message with the MQTT broker is completed.

This parameter is optional.

MQTTc_PARAM_TYPE_CALLBACK_ON_ERR_CALLBACK

Sets the callback function to notify the application when an error with the socket connection occurs.

A connection can be closed by the MQTT broker, by the client when an unexpected error occurred. To be able to re-open the socket connection and re-connect the client, this callback should be provided.
This parameter is optional.

MQTTc_PARAM_TYPE_CALLBACK_ON_PUBLISH_RX

Sets the callback function to notify the application when a PUBLISH message is received from the broker.

This parameter is mandatory.

MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR

Sets the argument that will be passed to every callback function.

This parameter is optional.


Example

Listing - MQTTc Connection Configuration Example
CPU_BOOLEAN  App_MQTTc_ConnPrepare (MQTTc_CONN  *p_conn)
{
    MQTTc_ERR  err;


                                                        /* ---------------- INIT NEW CONNECTION --------------- */
    MQTTc_ConnClr(p_conn, &err);
    if (err != MQTTc_ERR_NONE) {
        return (DEF_FAIL);
    }
                                                        /* Set conn parameters.                                 */
    MQTTc_ConnSetParam(       &AppMQTTc_Conn,
                               MQTTc_PARAM_TYPE_BROKER_NAME,
                       (void *)APP_MQTTc_BROKER_NAME,
                              &err);
    if (err != MQTTc_ERR_NONE) {
        return (DEF_FAIL);
    }

    MQTTc_ConnSetParam(       &AppMQTTc_Conn,
                               MQTTc_PARAM_TYPE_CLIENT_ID_STR,
                       (void *)APP_MQTTc_CLIENT_ID_NAME,
                              &err);
    if (err != MQTTc_ERR_NONE) {
        return (DEF_FAIL);
    }

    MQTTc_ConnSetParam(       &AppMQTTc_Conn,
                               MQTTc_PARAM_TYPE_USERNAME_STR,
                       (void *)APP_MQTTc_USERNAME,
                              &err);
    if (err != MQTTc_ERR_NONE) {
        return (DEF_FAIL);
    }

    MQTTc_ConnSetParam(       &AppMQTTc_Conn, 
                               MQTTc_PARAM_TYPE_PASSWORD_STR,                 
                       (void *)APP_MQTTc_PASSWORD,
                              &err);
    if (err != MQTTc_ERR_NONE) {
        return (DEF_FAIL);
    }

    MQTTc_ConnSetParam(       &AppMQTTc_Conn, 
                               MQTTc_PARAM_TYPE_KEEP_ALIVE_TMR_SEC,           
                       (void *)1000u,
                              &err);
    if (err != MQTTc_ERR_NONE) {
        return (DEF_FAIL);
    }

    MQTTc_ConnSetParam(       &AppMQTTc_Conn, 
                               MQTTc_PARAM_TYPE_CALLBACK_ON_COMPL,            
                       (void *)AppMQTTc_OnCmplCallbackFnct,
                              &err);
    if (err != MQTTc_ERR_NONE) {
        return (DEF_FAIL);
    }

    MQTTc_ConnSetParam(       &AppMQTTc_Conn, 
                               MQTTc_PARAM_TYPE_CALLBACK_ON_CONNECT_CMPL,     
                       (void *)AppMQTTc_OnConnectCmplCallbackFnct,
                              &err);
    if (err != MQTTc_ERR_NONE) {
        return (DEF_FAIL);
    }

    MQTTc_ConnSetParam(       &AppMQTTc_Conn, 
                               MQTTc_PARAM_TYPE_CALLBACK_ON_PUBLISH_CMPL,     
                       (void *)AppMQTTc_OnPublishCmplCallbackFnct,
                              &err);
    if (err != MQTTc_ERR_NONE) {
        return (DEF_FAIL);
    }

    MQTTc_ConnSetParam(       &AppMQTTc_Conn, 
                               MQTTc_PARAM_TYPE_CALLBACK_ON_SUBSCRIBE_CMPL,   
                       (void *)AppMQTTc_OnSubscribeCmplCallbackFnct,
                              &err);
    if (err != MQTTc_ERR_NONE) {
        return (DEF_FAIL);
    }

    MQTTc_ConnSetParam(       &AppMQTTc_Conn, 
                               MQTTc_PARAM_TYPE_CALLBACK_ON_ERR_CALLBACK,     
                       (void *)AppMQTTc_OnErrCallbackFnct,             
                              &err);
    if (err != MQTTc_ERR_NONE) {
        return (DEF_FAIL);
    }

    MQTTc_ConnSetParam(        &AppMQTTc_Conn, 
                                MQTTc_PARAM_TYPE_PUBLISH_RX_MSG_PTR,           
                       (void *)&AppMQTTc_MsgPublishRx,
                               &err);
    if (err != MQTTc_ERR_NONE) {
        return (DEF_FAIL);
    }

    MQTTc_ConnSetParam(       &AppMQTTc_Conn, 
                               MQTTc_PARAM_TYPE_CALLBACK_ON_PUBLISH_RX,       
                       (void *)AppMQTTc_OnPublishRxCallbackFnct,
                              &err);
    if (err != MQTTc_ERR_NONE) {
        return (DEF_FAIL);
    }

    MQTTc_ConnSetParam(       &AppMQTTc_Conn, 
                               MQTTc_PARAM_TYPE_TIMEOUT_MS,                   
                       (void *)30000u,
                              &err);
    if (err != MQTTc_ERR_NONE) {
        return (DEF_FAIL);
    }

    return (DEF_OK);
}