Hi Support team,
The communication of my net-core of nRF5340 is working well, and I want to try mqtt subscription, but it always fails, could you help give me some hints?
I try to do the subscription in the event handler of MQTT_EVT_CONNACK:
#define SPARKPLUGB_NCMD "spBv1.0/BDS/NCMD/Node1/#" #define SPARKPLUGB_DCMD "spBv1.0/BDS/DCMD/Node1/#" int sparkb_sub_cmd(struct mqtt_client *const client) { struct mqtt_topic cmdTopic[2]; cmdTopic[0].topic.utf8 = (uint8_t *)SPARKPLUGB_NCMD; cmdTopic[0].topic.size = strlen(SPARKPLUGB_NCMD); cmdTopic[0].qos = MQTT_QOS_0_AT_MOST_ONCE; cmdTopic[1].topic.utf8 = (uint8_t *)SPARKPLUGB_DCMD; cmdTopic[1].topic.size = strlen(SPARKPLUGB_DCMD); cmdTopic[1].qos = MQTT_QOS_0_AT_MOST_ONCE; struct mqtt_subscription_list sub_param = { .list = cmdTopic, .list_count = 2, .message_id = 0U }; return mqtt_subscribe(client, &sub_param); } In void mqtt_evt_handler(struct mqtt_client *const client, const struct mqtt_evt *evt) { int err; switch (evt->type) { case MQTT_EVT_CONNACK: if (evt->result != 0) { LOG_ERR("-----[MQTT_EVT_CONNACK] MQTT connect failed %d", evt->result); break; } g_connected = true; LOG_INF("-----[MQTT_EVT_CONNACK] MQTT client connected!"); //Upon connect successful, subscribe to sparkplug NCMD and DCMD msgs. A production should handle MQTT connect failures. err = sparkb_sub_cmd(client); if (err != 0) { LOG_ERR("-----[MQTT_EVT_CONNACK] Failed to subscribe sparkplug NCMD and DCMD: %d", err); } break;
The MQTT connection succeded, but the subscription is failed, the error code is -22 (#define EINVAL 22 /* Invalid argument */), the trace as below:
[00:00:25.672,332] <inf> main: modem_test() successful! [00:00:25.905,395] <inf> mqtttest: mqtt_connect OK, rc is 0 [00:00:26.146,850] <inf> mqtttest: do zsock_poll() 2s, ret = 1 [00:00:26.146,972] <inf> mqtttest: -----[MQTT_EVT_CONNACK] MQTT client connected! [00:00:26.147,003] <err> mqtttest: -----[MQTT_EVT_CONNACK] Failed to subscribe sparkplug NCMD and DCMD: -22
The published two topics "spBv1.0/BDS/NCMD/Node1/#" and "spBv1.0/BDS/DCMD/Node1/#" by other mqtt clients, so these two topics have existed on the MQTT server.
Could you help have a look and give some hints as to why subscriptions always fail? Thank you very much.
Best regards,
Yanpeng Wu