简体   繁体   English

MQTT 无法连接到后端 AWS IoT 核心

[英]MQTT can`t connect to backend AWS IoT core

UPDATED: I am trying to connect to a backend (AWS IoT Core) as I am using a small wp77 with a busybox image running.更新:我正在尝试连接到后端(AWS IoT Core),因为我正在使用一个运行着busybox图像的小型wp77。 When using the 8883 port the connection times out, so I have changed to port 443 and I can connect to the backend, still, the socket gets closed after three attempts.使用 8883 端口时,连接超时,所以我已更改为端口 443,我可以连接到后端,但在尝试了 3 次后,套接字仍然关闭。 This is the log readout from my app:这是我的应用程序的日志读数:

Aug 14 03:29:13 fx30s user.info Legato: INFO | 8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttMain[2717] | mqttMain[2717] | DSC connected... starting MQTT connection DSC 已连接...开始 MQTT 连接

Aug 14 03:29:13 fx30s user.info Legato: INFO | 8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttMain[2717] | mqttMain[2717] |

Aug 14 03:29:13 fx30s user.info Legato: INFO | 8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttMain[2717] | mqttMain[2717] |

Aug 14 03:29:13 fx30s user.info Legato: INFO | 8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttMain[2717]/mqttCliSvrComponent T=main | mqttMain[2717]/mqttCliSvrComponent T=main | mqttCliSvr.c DcsStateHandler() 317 | mqttCliSvr.c DcsStateHandler() 317 | rmnet_data0 connected! rmnet_data0 已连接! Starting MQTT session启动 MQTT session

Aug 14 03:29:13 fx30s user.info Legato: INFO | 8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttClient[1120]/mqttClientApiComponent T=main | mqttClient[1120]/mqttClientApiComponent T=main | mqttClientApi.c mqttClient_StartSession() 452 | mqttClientApi.c mqttClient_StartSession() 452 | StartSession called调用 StartSession

Aug 14 03:29:13 fx30s user.info Legato: INFO | 8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttClient[1120]/mqttClientApiComponent T=main | mqttClient[1120]/mqttClientApiComponent T=main | mqttClientApi.c mqttClient_StartSession() 455 | mqttClientApi.c mqttClient_StartSession() 455 | mqttClientPts NOT NULL mqttClientPts 不是 NULL

Aug 14 03:29:13 fx30s user.info Legato: INFO | 8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttClient[1120] | mqtt客户端[1120] | mqtt_StartSession... connecting... mqtt_StartSession... 正在连接...

Aug 14 03:29:13 fx30s user.info Legato: INFO | 8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttClient[1120] |. mqtt客户端[1120] |。 Seeding the random number generator... ok播种随机数生成器...好的

Aug 14 03:29:13 fx30s user.info Legato: INFO | 8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttClient[1120] |. mqtt客户端[1120] |。 Loading the CA root certificate... load certs from certs ok (0 skipped)加载 CA 根证书...从证书加载证书 ok (0 跳过)

Aug 14 03:29:13 fx30s user.info Legato: INFO | 8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttClient[1120] |. mqtt客户端[1120] |。 Loading the client certificate... /home/root/aws-iot/xxxxxx-certificate.pem.crt ok正在加载客户端证书... /home/root/aws-iot/xxxxxx-certificate.pem.crt ok

Aug 14 03:29:13 fx30s user.info Legato: INFO | 8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttClient[1120] |. mqtt客户端[1120] |。 Loading the client private key... /home/root/aws-iot/xxxxxx-private.pem.key ok正在加载客户端私钥... /home/root/aws-iot/xxxxxx-private.pem.key ok

Aug 14 03:29:13 fx30s user.info Legato: INFO | 8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttClient[1120] |. mqtt客户端[1120] |。 Connecting to tcp/xxxxxx-ats.iot.ap-southeast-1.amazonaws.com/443...正在连接到 tcp/xxxxxx-ats.iot.ap-southeast-1.amazonaws.com/443...

Aug 14 03:29:13 fx30s user.info Legato: INFO | 8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttClient[1120] | mqtt客户端[1120] | ok好的

Aug 14 03:29:13 fx30s user.info Legato: INFO | 8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttClient[1120] |. mqtt客户端[1120] |。 Setting up the TLS structure... ok设置 TLS 结构...好的

Aug 14 03:29:15 fx30s user.info Legato: INFO | 8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqttClient[1120] |. mqtt客户端[1120] |。 Performing the TLS handshake... ok执行 TLS 握手...好的

Aug 14 03:29:15 fx30s user.info Legato: INFO | 8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqttClient[1120] | mqtt客户端[1120] | [ Protocol is TLSv1.2 ] [ 协议为 TLSv1.2 ]

Aug 14 03:29:15 fx30s user.info Legato: INFO | 8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqttClient[1120] | mqtt客户端[1120] | [ Ciphersuite is TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256 ] [密码套件是 TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256]

Aug 14 03:29:15 fx30s user.info Legato: INFO | 8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqttClient[1120] |. mqtt客户端[1120] |。 Verifying peer X.509 certificate... ok正在验证对等 X.509 证书...好的

Aug 14 03:29:15 fx30s user.info Legato: INFO | 8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqttClient[1120] | mqtt客户端[1120] | clientId: FX30S客户编号:FX30S

Aug 14 03:29:15 fx30s user.info Legato: INFO | 8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqttClient[1120] | mqtt客户端[1120] | username:用户名:

Aug 14 03:29:15 fx30s user.info Legato: INFO | 8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqttClient[1120] | mqtt客户端[1120] | Attempting (1/3) to connect to tcp://xxxxxx-ats.iot.ap-southeast-1.amazonaws.com:443...尝试 (1/3) 连接到 tcp://xxxxxx-ats.iot.ap-southeast-1.amazonaws.com:443...

Aug 14 03:29:15 fx30s user.info Legato: INFO | 8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqttClient[1120] | mqtt客户端[1120] | Check connection options.检查连接选项。

Aug 14 03:29:15 fx30s user.info Legato: INFO | 8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqttClient[1120] | mqtt客户端[1120] | MQTT serialize connection. MQTT 序列化连接。

Aug 14 03:29:15 fx30s user.info Legato: INFO | 8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqttClient[1120] | mqtt客户端[1120] | MQTT send packet. MQTT 发送数据包。

Aug 14 03:29:15 fx30s user.info Legato: INFO | 8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqttClient[1120] | mqtt客户端[1120] | bytes sent: 31发送的字节数:31

Aug 14 03:29:15 fx30s user.info Legato: INFO | 8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqttClient[1120] | mqtt客户端[1120] | total bytes sent: 31 of 31发送的总字节数:31 个,共 31 个

Aug 14 03:29:15 fx30s user.info Legato: INFO | 8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqttClient[1120] | mqtt客户端[1120] | MQTT waiting for CONNACK MQTT 等待 CONNACK

Aug 14 03:29:20 fx30s user.info Legato: INFO | 8 月 14 日 03:29:20 fx30s user.info 连奏:信息 | mqttClient[1120] | mqtt客户端[1120] | MQTT is now exiting with -1 MQTT 现在以 -1 退出

Aug 14 03:29:20 fx30s user.info Legato: INFO | 8 月 14 日 03:29:20 fx30s user.info 连奏:信息 | mqttClient[1120] | mqtt客户端[1120] | Failed失败的

Aug 14 03:29:20 fx30s user.info Legato: INFO | 8 月 14 日 03:29:20 fx30s user.info 连奏:信息 | mqttClient[1120] | mqtt客户端[1120] | tlsSocket_close 1 tlsSocket_close 1

And this is the code, something I have taken from github and just testing on my device...这就是代码,我从 github 拿来的东西,只是在我的设备上测试......

int MQTTConnect(Client* c, MQTTPacket_connectData* options)
{
    Timer connect_timer;
    int rc = FAILURE;
    MQTTPacket_connectData default_options = MQTTPacket_connectData_initializer;
    int len = 0;
    
    InitTimer(&connect_timer);
    countdown_ms(&connect_timer, c->command_timeout_ms);

    if (c->isconnected) // don't send connect packet again if we are already connected
        goto exit;

    fprintf(stdout, "Check connection options.");
    fflush(stdout);

    if (options == 0)
    {
        fprintf(stdout, "MQTTConnect Set default options...");
        fflush(stdout);
        options = &default_options; // set default options if none were supplied
    }
    
    c->keepAliveInterval = options->keepAliveInterval;
    countdown(&c->ping_timer, c->keepAliveInterval);
    fprintf(stdout, "MQTT serialize connection.");
    fflush(stdout);
    if ((len = MQTTSerialize_connect(c->buf, c->buf_size, options)) <= )
        goto exit;

    #if 0
    {
        int i;
        for (i=0; i<len; i++)
        {
            fprintf(stdout, "%.2x", c->buf[i]);
        }
        fprintf(stdout, "\n");
        fflush(stdout);

        fprintf(stdout, "\n%.*s\n", len, c->buf);
        fflush(stdout);
    }
    #endif

    fprintf(stdout, "MQTT send packet.");
    fflush(stdout);
    if ((rc = sendPacket(c, len, &connect_timer)) != SUCCESS)  // send the connect packet
        goto exit; // there was a problem
    
    // this will be a blocking call, wait for the connack
    fprintf(stdout, "MQTT waiting for CONNACK");
    fflush(stdout);
    if (waitfor(c, CONNACK, &connect_timer) == CONNACK)
    {
        unsigned char connack_rc = 255;
        char sessionPresent = 0;
        if (MQTTDeserialize_connack((unsigned char*)&sessionPresent, &connack_rc, c->readbuf, c->readbuf_size) == 1)
            rc = connack_rc;
        else
            rc = FAILURE;
    }
    else
        rc = FAILURE;
exit:
    fprintf(stdout, "MQTT is now exiting with %d", rc );
    fflush(stdout);  
    if (rc == SUCCESS)
    //if (SUCCESS == SUCCESS)
        c->isconnected = 1;
    return rc;
    //return SUCCESS;
}

For some reason the backend doesn`t confirm the connection with a CONNACK and this is the reason why this fails.由于某种原因,后端没有确认与 CONNACK 的连接,这就是失败的原因。 AWS specify to use APLN when connecting to the backend through port 443 so I was wondering if this is the problem and how I can specify this protocol for tls on paho.mqtt.c (I know there is a solution for python.!). AWS 指定在通过端口 443 连接到后端时使用 APLN,所以我想知道这是否是问题所在,以及如何在 paho.mqtt.c 上为 tls 指定此协议(我知道 python 有一个解决方案!)。

Thank you for your help.谢谢您的帮助。

Yes, since port 443 is typically used for HTTPS you must use ALPN to indicate that you are communicating with MQTT w/ TLS on port 443 using the 'x-amzn-mqtt-ca' protocol name.是的,由于端口 443 通常用于 HTTPS,因此您必须使用 ALPN 来指示您正在使用“x-amzn-mqtt-ca”协议名称在端口 443 上与带有 TLS 的 MQTT 进行通信。

This blog post gives an example of setting ALPN for Paho-MQTT client.这篇文给出了一个为 Paho-MQTT 客户端设置 ALPN 的例子。

Another thing that you might want to double check is that您可能要仔细检查的另一件事是

  1. The client certificate that you are using on your client is registered with AWS IoT您在客户端上使用的客户端证书已向 AWS IoT 注册
  2. That the certificate you have registered has a policy attached to it that permits things like MQTT connection/publish/subscribe/receive.您注册的证书附加了一个策略,允许诸如 MQTT 连接/发布/订阅/接收之类的事情。

It is possible that if the certificate or policy is incorrect, then TLS handshake will complete, but AWS IoT will end the connection shortly after.如果证书或策略不正确,则可能会完成 TLS 握手,但 AWS IoT 很快就会结束连接。

I haven`t found or solved the problem with this code, but instead used the AWS SDK for C and everything works fine now.我还没有发现或解决此代码的问题,而是将 AWS SDK 用于 C,现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM