更新:我正在尝试连接到后端(AWS IoT Core),因为我正在使用一个运行着busybox图像的小型wp77。使用 8883 端口时,连接超时,所以我已更改为端口 443,我可以连接到后端,但在尝试 3 次后,套接字仍然关闭。这是我的应用程序的日志读数:
8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttMain[2717] | DSC 已连接...开始 MQTT 连接
8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttMain[2717] |
8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttMain[2717] |
8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttMain[2717]/mqttCliSvrComponent T=main | mqttCliSvr.c DcsStateHandler() 317 | rmnet_data0 已连接!开始 MQTT 会话
8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttClient[1120]/mqttClientApiComponent T=main | mqttClientApi.c mqttClient_StartSession() 452 | 调用 StartSession
8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqttClient[1120]/mqttClientApiComponent T=main | mqttClientApi.c mqttClient_StartSession() 455 | mqttClientPts 不为空
8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqtt客户端[1120] | mqtt_StartSession... 正在连接...
8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqtt客户端[1120] | . 播种随机数生成器...好的
8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqtt客户端[1120] | . 加载 CA 根证书 ...从证书加载证书 ok(跳过 0 个)
8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqtt客户端[1120] | . 正在加载客户端证书... /home/root/aws-iot/xxxxxx-certificate.pem.crt ok
8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqtt客户端[1120] | . 正在加载客户端私钥... /home/root/aws-iot/xxxxxx-private.pem.key ok
8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqtt客户端[1120] | . 正在连接到 tcp/xxxxxx-ats.iot.ap-southeast-1.amazonaws.com/443...
8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqtt客户端[1120] | 好的
8 月 14 日 03:29:13 fx30s user.info 连奏:信息 | mqtt客户端[1120] | . 设置 TLS 结构...好的
8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqtt客户端[1120] | . 执行 TLS 握手...好的
8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqtt客户端[1120] | [ 协议为 TLSv1.2 ]
8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqtt客户端[1120] | [密码套件是 TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256]
8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqtt客户端[1120] | . 正在验证对等 X.509 证书...好的
8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqtt客户端[1120] | 客户编号:FX30S
8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqtt客户端[1120] | 用户名 :
8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqtt客户端[1120] | 正在尝试 (1/3) 连接到 tcp://xxxxxx-ats.iot.ap-southeast-1.amazonaws.com:443...
8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqtt客户端[1120] | 检查连接选项。
8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqtt客户端[1120] | MQTT 序列化连接。
8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqtt客户端[1120] | MQTT 发送数据包。
8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqtt客户端[1120] | 发送的字节数:31
8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqtt客户端[1120] | 发送的总字节数:31 个,共 31 个
8 月 14 日 03:29:15 fx30s user.info 连奏:信息 | mqtt客户端[1120] | MQTT 等待 CONNACK
8 月 14 日 03:29:20 fx30s user.info 连奏:信息 | mqtt客户端[1120] | MQTT 现在以 -1 退出
8 月 14 日 03:29:20 fx30s user.info 连奏:信息 | mqtt客户端[1120] | 失败的
8 月 14 日 03:29:20 fx30s user.info 连奏:信息 | mqtt客户端[1120] | tlsSocket_close 1
这是代码,我从 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;
}
由于某种原因,后端没有确认与 CONNACK 的连接,这就是失败的原因。AWS 指定在通过端口 443 连接到后端时使用 APLN,所以我想知道这是否是问题所在以及如何在 paho.mqtt.c 上为 tls 指定此协议(我知道 python 有解决方案!!)。
谢谢您的帮助。