如何使用Paho异步发布示例(MQTT C客户端库)

应用程序通过发送带有TOPIC-A的MQTT PUBLISH来请求遥测数据。按照编码https://www.eclipse.org/paho/files/mqttdoc/MQTTClient/html/subasync.html进行的过程 收到PUBLISH / TOPIC-A并发送带有TOPIC-B的出版物(此消息的有效载荷包括遥测数据)

目标主机: Linux orangepipc2 4.19.63-sunxi64#5.92 SMP Fri Aug 2 00:18:27 CEST 2019 aarch64 GNU / Linux mosquitto版本1.5.7 eclipse Paho同步MQTT C客户端库版本:1.3.1 内部版本:2019-11-06T19:07:02Z gcc版本7.4.1 20181213 [linaro-7.4-2019.02修订版56ec6f6b99cc167ff0c2f8e1a2eed33b1edc85d4](Linaro GCC 7.4-2019.02)

int msgarrvd(void *context,char *topicName,int topicLen,MQTTClient_message *message)
{
    int i;
    char* payloadptr;
    printf("Message arrived\n");
    printf("     topic: %s\n",topicName);
    printf("   message: ");
    payloadptr = message->payload;
    for(i=0; i<message->payloadlen; i++)
    {
        putchar(*payloadptr++);
    }
    putchar('\n');
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);


// added code as per the main section of the example https://www.eclipse.org/paho/files/mqttdoc/MQTTClient/html/pubasync.html

    MQTTClient client1;
    MQTTClient_connectOptions conn_opts1 = MQTTClient_connectOptions_initializer;
    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    MQTTClient_deliveryToken token;
    int rc1;
    MQTTClient_create(&client1,ADDRESS,CLIENTID1,MQTTCLIENT_PERSISTENCE_NONE,NULL);
    conn_opts1.keepAliveInterval = 20;
    conn_opts1.cleansession = 1;
    if ((rc1 = MQTTClient_connect(client1,&conn_opts1)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect,return code %d\n",rc1);
        exit(EXIT_FAILURE);
    }
    pubmsg.payload = PAYLOAD;
    pubmsg.payloadlen = strlen(PAYLOAD);
    pubmsg.qos = QOS;
    pubmsg.retained = 0;
    MQTTClient_publishMessage(client1,TOPIC1,&pubmsg,&token);
    printf("Waiting for up to %d seconds for publication of %s\n"
            "on topic %s for client with ClientID: %s\n",(int)(TIMEOUT/1000),PAYLOAD,CLIENTID1);
    rc1 = MQTTClient_waitForCompletion(client1,token,TIMEOUT);
    printf("Message with delivery token %d delivered\n",token);
    MQTTClient_disconnect(client1,10000);
    MQTTClient_destroy(&client1);
//    return rc1;

// end of the added code

    return 1;
}

我无法使用TOPIC1发布新的MQTT消息。我应该使用其他进程来发布TOPIC1吗?

20191107 180109.954 3 CodeclientSub <- PINGRESP
20191107 180130.977 3 CodeclientSub -> PINGREQ (0)
20191107 180130.977 3 CodeclientSub <- PINGRESP
20191107 180136.045 3 CodeclientSub <- PUBLISH msgid: 1 qos: 1 retained: 0 payload: Hello World! blah-blah
20191107 180136.046 3 CodeclientSub -> PUBACK msgid: 1 (0)
Message arrived
     topic: MQTT_Examples
   message: Hello World! blah-blah
Failed to connect,return code -1
wc031546 回答:如何使用Paho异步发布示例(MQTT C客户端库)

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3142163.html

大家都在问