本帖最后由 xmlhttp 于 2019-5-9 21:07 编辑
直接拿的LWIP扩展例程的网络实验8 NETCONN_TCP客户端这个修改的,连接服务器代码如下:
[mw_shl_code=applescript,true]
u8 mqtt_connect(void)
{
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
unsigned char buf[200];
int buflen = sizeof(buf);
int len = 0;
memset(buf,' ',buflen);
data.clientID.cstring = "STM32F103_TEST";
data.keepAliveInterval = 20;
data.cleansession =0;
data.username.cstring = "m1";
data.password.cstring = "admin";
data.MQTTVersion = 4;
len = MQTTSerialize_connect(buf, buflen, &data);
transport_sendPacketBuffer((char*)buf,len);
delay_ms(100);
if (MQTTPacket_read(buf, buflen, transport_getdata) == CONNACK)
{
unsigned char sessionPresent, connack_rc;
if ( MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen)!= 1 || connack_rc != 0)
{
printf("Unable to connect, return code %d
", connack_rc);
return 0;
}
else
{
printf("MQTT CONNECT!
");
}
}
else
printf("No connack
");
return 0;
}
[/mw_shl_code]
在服务端有这样的日志输出:
1557404053: New client connected from x.x.x.x as STM32F103_TEST (p2, c0, k20, u'm1').
1557404053: No will message specified.
1557404053: Sending CONNACK to STM32F103_TEST (1, 0)
运行到MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen)等于1,connack_rc=2
串口中打印 Unable to connect, return code 2,请问要怎么修改?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
10 25 00 04 4D 51 54 54 04 C0 00 14 00 0E 53 54 4D 33 32 46 31 30 33 5F 54 45 53 54 00 02 6D 31 00 05 61 64 6D 69 6E
运行if (MQTTPacket_read(buf, len, transport_getdata) == CONNACK)后的buf数据是:
20 20 20 02 01 00 54 54 04 C0 00 14 00 0E 53 54 4D 33 32 46 31 30 33 5F 54 45 53 54 00 02 6D 31 00 05 61 64 6D 69 6E
一周热门 更多>