调试MQTT遇到的问题

发布于 2019-11-22 17:18:46    浏览:1058
首先我加载了pahoMQTT的组件,并且按照例程的说明,验证了sample程序,能够连上我自己的MQTT服务器,服务器端也看到了我发的消息
第二步,我根据 的引导,写了我自己的MQTT程序
测试程序在AT_SOCKET连接成功后才执行MQTT连接
然后就报错了,跟踪代码,貌似是url判断不正确,但是在sample里也是一样的tcp url,不知道哪里出了问题,请教下

报错信息如下:
\ | /
- RT - Thread Operating System
/ | \ 3.1.3 build Nov 19 2019
2006 - 2019 Copyright by rt-thread team
[I/sal.skt] Socket Abstraction Layer initialize success.
[I/at.clnt] AT client(V1.2.0) on device uart2 initialize success.
msh />[E/at.clnt] wait connect timeout (5000 millisecond)!
[E/at.sim800c] AT network initialize failed (-2)!
[I/at.sim800c] CHINA MOBILE
[I/at.sim800c] AT network initialize success!
[E/mqtt] resolve uri err
[E/mqtt] Net connect error(-1).
[D/mqtt] restart!
[E/mqtt] resolve uri err
[E/mqtt] Net connect error(-1).


跟踪到的出错位置:
报错MQTT跟踪.jpg




附上我的MQTT通信测试函数:
static MQTTClient client;

MQTTPacket_connectData condata = MQTTPacket_connectData_initializer;
static char cid[20] = {0};
/* 随机ID */
rt_snprintf(cid, sizeof(cid), "rtthread%d", rt_tick_get());

/* 连接参数 */
memcpy(&client.condata, &condata, sizeof(condata));
client.condata.clientID.cstring = cid;
client.condata.keepAliveInterval = 60;
client.condata.cleansession = 1;
client.condata.username.cstring = MQTT_USERNAME; //????
client.condata.password.cstring = MQTT_PASSWORD; //????

/* 遗嘱信息 */
client.condata.willFlag = 1;
client.condata.will.qos = 1;
client.condata.will.retained = 0;
client.condata.will.topicName.cstring = rt_strdup(MQTT_PUBTOPIC);
client.condata.will.message.cstring = MQTT_WILLMSG;

/* 事件回调函数 */
client.connect_callback = mqtt_connect_callback; //????????
client.online_callback = mqtt_online_callback; //????????
client.offline_callback = mqtt_offline_callback; //????????

/* ÉèÖö©Ôıí */
client.messageHandlers[0].topicFilter = MQTT_SUBTOPIC;
client.messageHandlers[0].callback = mqtt_sub_callback;
client.messageHandlers[0].qos = QOS1;
/* set default subscribe event callback */
client.defaultMessageHandler = mqtt_sub_default_callback;

/* 运行MQTT*/
paho_mqtt_start(&client);
rt_thread_mdelay(5000);

/* 循环上报数据*/
while(1)
{ char test_mqtt[]="{\"1\":\"2\"}";
rt_thread_mdelay(2000);
paho_mqtt_publish(&client, QOS1, MQTT_PUBTOPIC, test_mqtt);
}
}



查看更多

9 个回答
aozima
aozima 2019-11-22
拒绝白嫖,拒绝键盘侠!
看不出楼主是连接的哪个服务器,可能是服务器有问题了。
sjj430
sjj430 2019-11-22
This guy hasn't written anything yet
aozima 发表于 2019-11-22 17:24
看不出楼主是连接的哪个服务器,可能是服务器有问题了。


我的服务器一直是在跑的,其他应用都正常
sjj430
sjj430 2019-11-22
This guy hasn't written anything yet
aozima 发表于 2019-11-22 17:24
看不出楼主是连接的哪个服务器,可能是服务器有问题了。


并且,之前sample里用的也是同一个服务器,端口,账号和密码
sjj430
sjj430 2019-11-22
This guy hasn't written anything yet
好像是找到问题了,对url没有复制,少了这句话
client.uri = MQTT_URI;

但新的问题又出来了,就当记录贴吧

\ | /
- RT - Thread Operating System
/ | \ 3.1.3 build Nov 19 2019
2006 - 2019 Copyright by rt-thread team
[I/sal.skt] Socket Abstraction Layer initialize success.
[I/at.clnt] AT client(V1.2.0) on device uart2 initialize success.
msh />[E/at.clnt] wait connect timeout (5000 millisecond)!
[E/at.sim800c] AT network initialize failed (-2)!
[I/at.sim800c] CHINA MOBILE
[I/at.sim800c] AT network initialize success!
[D/mqtt] ipv4 address port: 1993
[D/mqtt] HOST = 'natural.kylinzw.com'
[E/mqtt] MQTT connect error(-1): UNKNOWN_ERROR.
[D/mqtt] restart!
[D/mqtt] ipv4 address port: 1993
[D/mqtt] HOST = 'natural.kylinzw.com'
[E/mqtt] MQTT connect error(-1): UNKNOWN_ERROR.
[D/mqtt] restart!
[D/mqtt] ipv4 address port: 1993
[D/mqtt] HOST = 'natural.kylinzw.com'
sjj430
sjj430 2019-11-22
This guy hasn't written anything yet
再之后,对比sample。加上了以下
/* 分配缓冲区 */
client.buf_size = client.readbuf_size = 1024;
client.buf = rt_calloc(1, client.buf_size);
client.readbuf = rt_calloc(1, client.readbuf_size);
if (!(client.buf && client.readbuf))
{
LOG_E("no memory for MQTT client buffer!");
return -1;
}

然后又有进一步的变化了,貌似连上过,但是还有错误

\ | /
- RT - Thread Operating System
/ | \ 3.1.3 build Nov 19 2019
2006 - 2019 Copyright by rt-thread team
[I/sal.skt] Socket Abstraction Layer initialize success.
[I/at.clnt] AT client(V1.2.0) on device uart2 initialize success.
msh />[E/at.clnt] wait connect timeout (5000 millisecond)!
[E/at.sim800c] AT network initialize failed (-2)!
[I/at.sim800c] CHINA MOBILE
[I/at.sim800c] AT network initialize success!
[D/mqtt] ipv4 address port: 1993
[D/mqtt] HOST = 'natural.kylinzw.com'
[I/mqtt] MQTT server connect success.
[I/mqtt] Subscribe #0 $biz/toClient/ OK!
[D/mqtt] MQTTSerialize_publish sendPacket rc: -1
[D/mqtt] restart!
[D/mqtt] ipv4 address port: 1993
[D/mqtt] HOST = 'natural.kylinzw.com'
[E/mqtt] getaddrinfo err: 202 'natural.kylinzw.com'
[E/mqtt] resolve uri err
[E/mqtt] Net connect error(-1).
[E/at.clnt] Read response buffer failed. The Response buffer size is out of buffer size(64)!
[E/at.clnt] Read response buffer failed. The Response buffer size is out of buffer size(64)!
[E/at.clnt] Read response buffer failed. The Response buffer size is out of buffer size(64)!
[E/at.clnt] Read response buffer failed. The Response buffer size is out of buffer size(64)!
[D/mqtt] restart!

这里想稍微吐槽下,文档里写的以后能不能完善一些,完全按照文档做,估计是很多问题的
sjj430
sjj430 2019-11-22
This guy hasn't written anything yet
报错MQTT跟踪.jpg
成功收到了

hello
hello 2019-11-22
This guy hasn't written anything yet
sjj430 发表于 2019-11-22 20:36
成功收到了


怎么解决的?
sjj430
sjj430 2019-11-22
This guy hasn't written anything yet
上面都一步步贴出了,改的东西,现在是上报可以了,接收的还没调
keyq
keyq 2021-03-16
This guy hasn't written anything yet

越看 越像现在的我 。。 哎 真的是一步一个坑。 文档还不写。。。

撰写答案

请登录后再发布答案,点击登录
关注者
0
被浏览
1.1k

发布
问题

分享
好友

手机
浏览

扫码手机浏览