MQTT Issue with Longer Messages

MQTT Issue with Longer Messages

Home Forums Networking Software MQTT Issue with Longer Messages

Tagged: 

This topic contains 0 replies, has 1 voice, and was last updated by  Alek Andreev 1 year, 6 months ago.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #22997

    Alek Andreev
    Participant

    Hello!

    I have a working prototype with uC/MQTTc. Things work well until the message body reaches about 100 bytes length. Then, the message receive callback starts being called with a pointer to a null byte string and a 0 message length.

    Do you have any suggestions why this must be happening and how to work around it?

    I’ve attached demonstration code below:

    
    #define check_err() if (err != 0) return to_mf_status(err);
    
    static MQTTc_MSG mfx_mqtt_msg_conn;
    static char mfx_mqtt_msg_conn_buf[128];
    
    static MQTTc_MSG mfx_mqtt_msg_sub;
    static char mfx_mqtt_msg_sub_buf[128];
    
    static MQTTc_MSG mfx_mqtt_receive_msg;
    static char mfx_mqtt_receive_msg_buf[1024];
    
    static MQTTc_MSG mfx_mqtt_publish_msg;
    static char mfx_mqtt_publish_msg_buf[1024];
    
    mf_status_t mqtt_connect(char *hostname, int port, bool secure, char *username, char *password) {
        MQTTc_ERR err;
    
        MQTTc_ConnClose(&mqttc, DEF_NULL, &err);
    
        MQTTc_ConnClr(&mqttc, &err);
        check_err();
    
        mqtt_send_packet_flag = false;
    
        /* Configure the message buffer */
    
        MQTTc_MsgClr(&mqtt_msg_sub, &err);
        check_err();
    
        MQTTc_MsgSetParam(&mqtt_msg_conn, MQTTc_PARAM_TYPE_MSG_BUF_PTR, mqtt_msg_conn_buf, &err);
        check_err();
    
        MQTTc_MsgSetParam(&mqtt_msg_conn, MQTTc_PARAM_TYPE_MSG_BUF_LEN, (void *) sizeof(mqtt_msg_conn_buf), &err);
        check_err();
    
        MQTTc_MsgClr(&mqtt_msg_sub, &err);
        check_err();
    
        MQTTc_MsgSetParam(&mqtt_msg_sub, MQTTc_PARAM_TYPE_MSG_BUF_PTR, mqtt_msg_sub_buf, &err);
        check_err();
    
        MQTTc_MsgSetParam(&mqtt_msg_sub, MQTTc_PARAM_TYPE_MSG_BUF_LEN, (void *) sizeof(mqtt_msg_sub_buf), &err);
        check_err();
    
        MQTTc_MsgClr(&mqtt_publish_msg, &err);
        check_err();
    
        MQTTc_MsgSetParam(&mqtt_publish_msg, MQTTc_PARAM_TYPE_MSG_BUF_PTR, &mqtt_publish_msg_buf[0], &err);
        check_err();
    
        MQTTc_MsgSetParam(&mqtt_publish_msg, MQTTc_PARAM_TYPE_MSG_BUF_LEN, (void *) MF_CONFIG_SIZE,
                          &err);
        check_err();
    
        MQTTc_MsgClr(&mqtt_receive_msg, &err);
        check_err();
    
        memset(mqtt_receive_msg_buf, 0, MF_CONFIG_SIZE);
    
        MQTTc_MsgSetParam(&mqtt_receive_msg, MQTTc_PARAM_TYPE_MSG_BUF_PTR, &mqtt_receive_msg_buf[0], &err);
        check_err();
    
        MQTTc_MsgSetParam(&mqtt_receive_msg, MQTTc_PARAM_TYPE_MSG_BUF_LEN, (void *) MF_CONFIG_SIZE,
                          &err);
        check_err();
        /* Configure the connection object with hostname, user, pass, etc. */
    
        MQTTc_ConnSetParam(&mqttc, MQTTc_PARAM_TYPE_BROKER_NAME, (void *) hostname, &err);
        check_err();
    
        MQTTc_ConnSetParam(&mqttc, MQTTc_PARAM_TYPE_BROKER_PORT_NBR, (void *) port, &err);
        check_err();
    
        char device_id[MF_DEVICE_ID_SIZE] = MF_DEVICE_ID;
    
        MQTTc_ConnSetParam(&mqttc, MQTTc_PARAM_TYPE_CLIENT_ID_STR, (void*) device_id, &err);
        check_err();
    
        MQTTc_ConnSetParam(&mqttc, MQTTc_PARAM_TYPE_USERNAME_STR, (void *) &username, &err);
        check_err();
    
        MQTTc_ConnSetParam(&mqttc, MQTTc_PARAM_TYPE_PASSWORD_STR, (void *) password, &err);
        check_err();
    
        MQTTc_ConnSetParam(&mqttc, MQTTc_PARAM_TYPE_KEEP_ALIVE_TMR_SEC, (void *) 1000u, &err);
        check_err();
    
        MQTTc_ConnSetParam(&mqttc, MQTTc_PARAM_TYPE_TIMEOUT_MS, (void *) 5000u, &err);
        check_err();
    
        MQTTc_ConnSetParam(&mqttc, MQTTc_PARAM_TYPE_PUBLISH_RX_MSG_PTR, &mqtt_receive_msg, &err);
        check_err();
    
        MQTTc_ConnSetParam(&mqttc, MQTTc_PARAM_TYPE_CALLBACK_ON_ERR_CALLBACK, (void *) mqtt_callback_error, &err);
        check_err();
    
        MQTTc_ConnSetParam(&mqttc, MQTTc_PARAM_TYPE_CALLBACK_ON_PUBLISH_RX, (void *) mqtt_receive_callback, &err);
        check_err();
    
        MQTTc_ConnSetParam(&mqttc, MQTTc_PARAM_TYPE_CALLBACK_ON_PUBLISH_CMPL, (void *) mqtt_callback_publish_ok, &err);
        check_err();
    
        MQTTc_ConnSetParam(&mqttc, MQTTc_PARAM_TYPE_CALLBACK_ON_CONNECT_CMPL, (void *) mqtt_callback_connect_ok, &err);
        check_err();
    
        APP_TRACE_INFO(("Connecting to MQTT broker\n"));
    
        MQTTc_ConnOpen(&mqttc, MQTTc_FLAGS_NONE, &err);
        check_err();
    
        APP_TRACE_INFO(("Connected to MQTT broker\n"));
    
        /* Send the CONNECT message once the socket has been connected */
        MQTTc_Connect(&mqttc, &mqtt_msg_conn, &err);
        check_err();
    
        return MF_SUCCESS;
    }
    
    void mqtt_status(bool *connected, bool *ready) {
        *connected = mqtt_connected;
        *ready = mqtt_ready;
    }
    
    static void mqtt_receive_callback(MQTTc_CONN *conn, const CPU_CHAR *topic, CPU_INT32U topic_len,
                                          const CPU_CHAR *msg_buf, CPU_INT32U payload_len, void *p_arg, MQTTc_ERR err) {
        // Do something with the message
    }
    
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

View the complete site map

x
Loading...