0

如果我使用 QoS 0,我的代码(如下)正在工作。但对于 QoS 1 或 QoS 2。MQTTClient_publishMessage(...) 失败。我是否缺少任何配置?或者,是因为我使用的是免费的 XIvely 帐户吗?

我使用 Paho API。

------------------------------------------- 切割开始 ---------------- ---------------------

/**
 * @file
 *
 *  Paho MQ Client API to Xively mqtt broker
 *
 */

    enter code here

#include "MQTTClient.h"
#include <stdlib.h>

void usage()
{
    printf("Usage: speicify QoS\n");
    printf("     turn  on bulb -> ka_pub Qos 1 1\n");
    printf("     turn off bulb -> ka_pub Qos 1 0\n");
    printf("     send 7 to led -> ka_pub Qos 2 7\n");
    printf("     send 2 to led -> ka_pub Qos 2 2\n");
}

int main(int argc, char** argv)
{
    int rc = 0;

    if (argc < 3) {
        usage();
        exit (0);
    }

    char TOPIC[250] ; // given enough space first to avoid malloc
    MQTTClient client;
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    MQTTClient_deliveryToken token;

    int QoS = atoi(argv[1]);

    if (argv[2][0] == '1') {
        strcpy(TOPIC, BULB_TOPIC);
        conn_opts.username = BULB_API_KEY ;
        conn_opts.password = "" ; // will be ignored

    } else
        if (argv[2][0] == '2') {
            strcpy(TOPIC, LED_TOPIC) ;
            conn_opts.username = LED_API_KEY ;
            conn_opts.password = "" ; // will be ignored
        } else {
            printf("Bad arg\n");
            usage();
            exit (0);
        }

    setenv("MQTT_C_CLIENT_TRACE", "ON", 1); // same as 'stdout'
    setenv("MQTT_C_CLIENT_TRACE_LEVEL", "ERROR", 1);  //ERROR, PROTOCOL, MINIMUM, MEDIUM and MAXIMUM

    MQTTClient_create(&client, XIVELY_END_URL, "test", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);

    //conn_opts.keepAliveInterval = 20; // init to 60
    //conn_opts.cleansession = 1; // default 1, will clean previous msg in server
    conn_opts.reliable = 0 ; //default 1, only 1 can in-flight,  0 - allow 10 msg

    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect, return code %d\n", rc);
        exit(-1);
    }

    // prepare publish msg

    char tmsg[250] ; // check max 250 boundary later
    if (argv[2][0] == '1')
        sprintf(tmsg, "{\"id\":\"switch\",\"current_value\":\"%c\"}", argv[3][0]);
    else
        sprintf(tmsg, "{\"id\":\"num\",\"current_value\":\"%c\"}", argv[3][0]) ;

    int tmsg_len = strlen(tmsg);

        pubmsg.payload = &tmsg[0] ;
        pubmsg.payloadlen = tmsg_len; //mlen
        pubmsg.qos = QoS;
        pubmsg.retained = 0;

        MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
        //MQTTClient_publish(client, TOPIC, pubmsg.payloadlen, pubmsg.payload,
        //               pubmsg.qos, pubmsg.retained, &token);

        rc = MQTTClient_waitForCompletion(client, token, 100000);

        printf("Finish publish for TOPIC: %s, QoS: %d, msg of '%s'\n", TOPIC, pubmsg.qos, (char *) pubmsg.payload);


        if (rc == 0)
            MyLog(LOGA_INFO, "verdict pass");
        else
            MyLog(LOGA_INFO, "verdict fail");

    MQTTClient_disconnect(client, 10000);
    MQTTClient_destroy(&client);

    return rc;
}

------------------------------------------- 切割结束 ---------------- ---------------------

4

1 回答 1

1

我不相信 Xively 支持 QoS>0。

于 2014-12-10T15:43:10.867 回答