直接使用Azure云在C中实现paho-mqtt,"无法连接"?

时间:2017-11-28 14:28:07

标签: linux azure ssl mqtt paho

I' M试图发送和接收在一个覆盆子-π3.使用Azure云而是直接对PAHO-MQTT而不是天蓝色-MQTT SDK我按照提到的所有的指令的消息here。我已经生成了SSL证书,并且还放置在/ etc / certs文件路径中。我已经对ClientID,Address等代码进行了必要的更改。但是当我尝试运行代码时,我收到了一个错误。

  

错误:连接失败,返回代码-1

这是代码,

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

#define ADDRESS     "xxxxxxxx.azure-devices.net:8883"
#define CLIENTID    "Device-Id"
#define TOPIC       "devices/Device-Id/messages/events/"
#define PAYLOAD     "Hello World!"
#define QOS         0
#define TIMEOUT     10000L

int main(int argc, char* argv[])
{
    MQTTClient client;
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    MQTTClient_deliveryToken token;
    int rc;

    MQTTClient_create(&client, ADDRESS, CLIENTID,
        MQTTCLIENT_PERSISTENCE_NONE, NULL);
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;
    conn_opts.username = "xxxxxx.azure-devices.net/Device-ID/api-version=2017-11-25";
    conn_opts.password = "SharedAccessSignature sr=xxxxx.azure-devices.net&sig=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5&skn=Device-ID";
    conn_opts.ssl->truststore = "/etc/certs/ca.crt";

    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }
    pubmsg.payload = PAYLOAD;
    pubmsg.payloadlen = strlen(PAYLOAD);
    pubmsg.qos = QOS;
    pubmsg.retained = 0;
    MQTTClient_publishMessage(client, TOPIC, &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, TOPIC, CLIENTID);
    rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
    printf("Message with delivery token %d delivered\n", token);
    MQTTClient_disconnect(client, 10000);
    MQTTClient_destroy(&client);
    return rc;
}

我不确定这里有什么问题。帮帮我。提前谢谢。

0 个答案:

没有答案