【问题标题】:Paho MQTT C Client connection with azure IoT-Hub on Beaglebone BlackPaho MQTT C 客户端与 Beaglebone Black 上的 azure IoT-Hub 的连接
【发布时间】:2016-06-22 07:57:22
【问题描述】:

我正在尝试使用 MQTT 连接 Azure IoT-Hub,并使用来自 Beaglebone black(操作系统:Debian Wheezy)的 Paho C 客户端发送和接收消息。我在 Ubuntu 机器上使用 eclipse CDT 来开发我的应用程序并远程部署/调试。

当我在我的本地 ubuntu 机器上运行应用程序(使用 gcc 编译)时,Azure 连接成功并且我能够发送数据包。

我按照here 的规定交叉编译了 OPENSSL,并在“/usr/arm-linux-gnueabihf”文件夹中复制了相应的目录。但是,当我使用 arm-linux-gnueabihf-gcc-4.7 编译并在我的 beaglebone black(使用 gdb-multiarch)上进行远程调试时,我的输出控制台中出现以下错误:

3068126320:error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed:s3_clnt.c:1185:
3068126320:error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed:s3_clnt.c:1185:
Failed to connect, return code -1

请帮我解决这个问题。谢谢。

Edit: Suspected link - Reg: 疑似重复link 中的错误是一样的,但是那里的 OPENSSL 错误是由于证书过期。但在我的情况下,它是在与 azure 和 BBB 的 MQTT 连接期间。此外,该链接中我的问题没有答案。根据该链接,当我们选择 SSL/TLS 安全连接时,禁用证书验证毫无意义。

我的代码:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "MQTTClient.h"
#define PAYLOAD     "Hello World!"
#define QOS         1
#define TIMEOUT     10000L
#define TOPIC1       "devices/Manoj_Test/messages/events/"
#define ADDRESS     "ssl://xxxxxxxx.azure-devices.net:8883"
#define CLIENTID1    "Manoj_Test"

int main(void)
{
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
int rc1;

MQTTClient_create(&client, ADDRESS, CLIENTID1, 1, NULL);
conn_opts.cleansession = 1;
conn_opts.username = "xxxxxxxx.azure-devices.net/Manoj_Test";
conn_opts.password = "SharedAccessSignature sr=xxxxxxxx.azure-devices.net%2fdevices%2fManoj_Test&sig=GyizT%2b7uyIpOkMJjTfN%2fpOZh9CnuQedNB%2bre2NrL1Kg%3d&se=1496395529";


 MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);


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

MQTTClient_subscribe(client, TOPIC, QOS);
while(1)
{
    pubmsg.payload = PAYLOAD;
    pubmsg.payloadlen = strlen(PAYLOAD);
    pubmsg.qos = 1;
    pubmsg.retained = 0;
    MQTTClient_publishMessage(client, TOPIC1, &pubmsg, &token);
    printf("Waiting for up to %d seconds for publication of %s\non topic %s for client with ClientID: %s\n", (int)(TIMEOUT/1000), PAYLOAD, TOPIC1, CLIENTID1);
    rc1 = MQTTClient_waitForCompletion(client, token, TIMEOUT);
    printf("Message with delivery token %d delivered\n", token);
    usleep(100000);
}
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
return rc1;
}

【问题讨论】:

  • 这些错误是运行时的,对吧?我也遇到过这些错误,对我来说这意味着它所说的:它无法验证 SSL 证书。我不确定您是否可以通过 Chrome 之类的浏览器访问这些服务,但如果可以,您可以检查 Chrome 是否会为您提供有关证书的相同警告。
  • @bzeaman 是的。错误发生在运行时。我无法从浏览器访问这些服务,因为这是基于 TCP 的 MQTT 代理。但我使用 gcc 验证了在 ubuntu 上构建的相同代码,并成功连接到 Azure。对于我怀疑为重复的问题,请参阅我的帖子编辑,该链接无法回答我的问题。

标签: c azure cross-compiling beagleboneblack azure-iot-hub


【解决方案1】:

您是否考虑过使用 Azure IoT SDK 连接到 IoT 中心? Debian 支持开箱即用,并且抽象了建立连接的复杂性。您可以阅读此blog,了解使用 SDK 的好处。

【讨论】:

    【解决方案2】:

    我解决了这个问题。
    我从命令行以 .crt 格式获取服务器证书并保存到 /usr/local/share/ca-certificates/ 文件夹。

    openssl s_client -showcerts -connect server.edu:443 </dev/null 2>/dev/null|openssl x509 -outform DER >mycertfile.crt 
    

    然后我更新了信任库证书以添加上面下载的证书,

    update-ca-certificates
    

    更新后,我在代码中引用了 ca-certificates 文件,

    conn_opts.ssl->trustStore = "/etc/ssl/certs/ca-certificates.crt";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多