After subscribing to the topic you mentioned, do you send an empty message to $iothub/twin/GET/?$rid={request id}
, with a populated value for request ID?
Device Twin Topic Configuration
Hello,
I am fairly new with azure and I'm working on a PIC32MZ microcontroller to configure a device twin to IotHub. I am subscribing to topic $iothub/twin/res/#
but I'm not receiving any response back from IotHub. I am able to receive telemetry data on the device from an events topic though. Is there special configuration for setting up the device twin?
Thank you!
AZURE_DEVICE_ID is the Request ID. I am able to update the twin's desired properties by publishing to $iothub/twin/PATCH/properties/reported/?$rid=AZURE_DEVICE_ID. I'll get a 204 response to the device meaning the message was successful. When I want to retrieve a device twin properties by sending an empty message to topic $iothub/twin/GET/?$rid=AZURE_DEVICE_ID, the IotHub never sends a response. Below is my code and all of the publishing is done on 'toggle' as I'm alternating sending different messages to different topics. The setting of publish topic name and publish message begins on line 172.
case APP_MQTT_STATE_CLIENT_CONNECT:
// Send Connect and wait for Connect Ack
resCode = MqttClient_Connect(&mqttCtx->mqttClient, &mqttCtx->mqttConnect);
if (resCode == MQTT_CODE_CONTINUE)
{
break;
}
APP_MQTT_ClientResult(mqttCtx, "MqttClient_Connect", resCode) ;
if (resCode != MQTT_CODE_SUCCESS)
{
mqttCtx->currState = APP_MQTT_STATE_NET_DISCONNECT;
break;
}
// Validate Connect
SYS_CONSOLE_PRINT("MQTT Task - Connect Ack: Return Code %u, Session Present %s\r\n", mqttCtx->mqttConnect.ack.return_code,
(mqttCtx->mqttConnect.ack.flags & MQTT_CONNECT_ACK_FLAG_SESSION_PRESENT) ? "Yes" : "No");
///////// Abhinav Modified /////////////
if (mqttCtx->mqttConnect.ack.return_code == 0 && DPS_DONE)
IOTHUB_CONN_DONE = true;
///////// Abhinav Modified /////////////
#if defined(WOLFMQTT_V5) && defined(WOLFMQTT_PROPERTY_CB)
// print the acquired client ID
SYS_CONSOLE_PRINT("MQTT Task - Connect Ack: Assigned Client ID: %s", mqttCtx->pClientId);
#endif // defined(WOLFMQTT_V5) && defined(WOLFMQTT_PROPERTY_CB)
// set the topic list
/*SUS:*/ memset(&mqttCtx->mqttSubscribe, 0, sizeof(mqttCtx->mqttSubscribe));
///////// Abhinav Modified /////////////
if (DPS_DONE)
{
#ifdef DPS_SERVER
strncpy(mqttCtx->topicName,AZURE_IOTHUB_SUB_TOPIC,sizeof(AZURE_IOTHUB_SUB_TOPIC)-1);// sizeof(mqttCtx->topicName) - 1);//; APP_MQTT_DEFAULT_TOPIC_NAME
#endif
}
else
{
strncpy(mqttCtx->topicName,AZURE_MSGS_TOPIC_NAME,sizeof(AZURE_MSGS_TOPIC_NAME)-1);
}
///////// Abhinav Modified /////////////
if (DPS_DONE)
{
// Subscription 1 IOTHUB_SUB
//"devices/"AZURE_DEVICE_ID"/messages/devicebound/#"
mqttCtx->mqttTopic[0].topic_filter = AZURE_IOTHUB_SUB_TOPIC; // Sub to IoTHub events
mqttCtx->qos = APP_MQTT_DEFAULT_QOS;
mqttCtx->mqttTopic[0].qos = mqttCtx->qos;
// Subscription 2 TWIN_SUB subscribe to operation responses
//"$iothub/twin/res/#"
mqttCtx->mqttTopic[1].topic_filter = AZURE_DEVICE_TWIN_SUB_TOPIC; // Sub to IoTHub events
mqttCtx->mqttTopic[1].qos = mqttCtx->qos;
// Subscription 3 TWIN_SUB subscribe to desired property updates
//"$iothub/twin/PATCH/properties/desired/#"
mqttCtx->mqttTopic[2].topic_filter = AZURE_DEVICE_TWIN_SUB_DESIRED_TOPIC; // Sub to IoTHub events
mqttCtx->mqttTopic[2].qos = mqttCtx->qos;
// Subscribe Topic
mqttCtx->mqttSubscribe.packet_id = APP_MQTT_NewPacketId(mqttCtx);//mqtt_get_packetid();//APP_MQTT_NewPacketId(mqttCtx);
mqttCtx->mqttSubscribe.topic_count = 3;//sizeof(mqttCtx->mqttTopic)/sizeof(MqttTopic);//1;
mqttCtx->mqttSubscribe.topics = mqttCtx->mqttTopic;
}
else
{
mqttCtx->mqttTopic[0].topic_filter = mqttCtx->topicName;
mqttCtx->qos = APP_MQTT_DEFAULT_QOS;
mqttCtx->mqttTopic[0].qos = mqttCtx->qos;
// Subscribe Topic
// Subscribe to DPS Topic
mqttCtx->mqttSubscribe.packet_id = APP_MQTT_NewPacketId(mqttCtx);//mqtt_get_packetid();//APP_MQTT_NewPacketId(mqttCtx);
mqttCtx->mqttSubscribe.topic_count = 1;//sizeof(mqttCtx->mqttTopic)/sizeof(MqttTopic);//1;
mqttCtx->mqttSubscribe.topics = mqttCtx->mqttTopic;
}
#ifdef WOLFMQTT_V5
if (mqttCtx->subIdAvailable)
{ // set the subscription identifier
mqttCtx->mqttTopic.sub_id = 1;
mqttProp = MqttClient_PropsAdd(&mqttCtx->mqttSubscribe.props);
if(mqttProp)
{
mqttProp->type = MQTT_PROP_SUBSCRIPTION_ID;
mqttProp->data_int = mqttCtx->mqttTopic.sub_id;
}
mqttCtx->propList = mqttCtx->mqttSubscribe.props;
}
#endif
for(j=0;j<mqttCtx->mqttSubscribe.topic_count;j++)
SYS_CONSOLE_PRINT("Verify - Subscribe Topic- [%s], topic filter- [%s]\r\n", mqttCtx->topicName, mqttCtx->mqttTopic[j].topic_filter);
mqttCtx->currState = APP_MQTT_STATE_SUBSCRIBE;
break;
case APP_MQTT_STATE_SUBSCRIBE:
resCode = MqttClient_Subscribe(&mqttCtx->mqttClient, &mqttCtx->mqttSubscribe);
//SYS_CONSOLE_PRINT("APP_MQTT_STATE_SUBSCRIBE resCode = %d\r\n", resCode);
if (resCode == MQTT_CODE_CONTINUE)
{ // wait some more
///////// Abhinav Modified /////////////
if (MSG_RECVD == true)
{
if (strncmp(device_status,"assigned",strlen("assigned")) == 0 && IOTHUB_CONN_DONE == false)
{
mqttCtx->currState = APP_MQTT_STATE_START_UNSUBSCRIBE;
DPS_DONE = true;
break;
}
if (IOTHUB_CONN_DONE == false)
goto PUB;
}
///////// Abhinav Modified /////////////
break;
}
APP_MQTT_ClientResult(mqttCtx, "MqttClient_Subscribe", resCode) ;
if (resCode != MQTT_CODE_SUCCESS)
{
mqttCtx->currState = APP_MQTT_STATE_NET_DISCONNECT;
break;
}
// show the successful subscribed topic
MqttTopic* pTopic = &mqttCtx->mqttSubscribe.topics[0];
SYS_CONSOLE_PRINT("MQTT Task - Subscribed Topic %s, Qos %u\r\n", pTopic->topic_filter, pTopic->qos);
///////// Abhinav Modified /////////////
// if (IOTHUB_CONN_DONE == true)
// {
// mqttCtx->currState = APP_MQTT_STATE_IDLE;
// break;
// }
///////// Abhinav Modified /////////////
PUB: // publish the topic
memset(&mqttCtx->mqttPublish, 0, sizeof(MqttPublish));
mqttCtx->mqttPublish.retain = 0;
mqttCtx->mqttPublish.qos = MQTT_QOS_1;
mqttCtx->mqttPublish.duplicate = 0;
mqttCtx->mqttPublish.packet_id = APP_MQTT_NewPacketId(mqttCtx);//mqtt_get_packetid();//APP_MQTT_NewPacketId(mqttCtx);
strncpy(mqttCtx->publishMessage, APP_MQTT_DEFAULT_MESSAGE, sizeof(mqttCtx->publishMessage) - 1);
//strncpy(mqttCtx->publishMessage,(const char *)post_buff,sizeof(post_buff)-1); // Messages from the radio Derrek Added OCB only
mqttCtx->mqttPublish.buffer = (uint8_t*)mqttCtx->publishMessage;
mqttCtx->mqttPublish.total_len = (uint16_t)strlen(mqttCtx->publishMessage);
///////// Abhinav Modified /////////////
#ifdef DPS_SERVER
if (MSG_RECVD == true && IOTHUB_CONN_DONE == false)
{
MSG_RECVD = false;
memset(new_publish_topic, 0, sizeof(new_publish_topic));
strncpy(new_publish_topic,AZURE_OPERATION_STATUS_TOPIC, sizeof(AZURE_OPERATION_STATUS_TOPIC));
mqttCtx->mqttPublish.topic_name = strcat(new_publish_topic,operation_id);
memset(operation_id, 0, sizeof(operation_id));
SYS_CONSOLE_PRINT("Verify - Operational Status Publish Topic = %s \r\n Message %s\r\n",mqttCtx->mqttPublish.topic_name,mqttCtx->mqttPublish.buffer);
}
else
{
if(DPS_DONE == true)
{
//mqttCtx->mqttPublish.topic_name = AZURE_IOTHUB_PUB_TOPIC;
/////////////// Derrek ADDED
if(toggle)
{
toggle = 0;
//"$iothub/twin/GET/?$rid="AZURE_DEVICE_ID
mqttCtx->mqttPublish.topic_name = AZURE_DEVICE_TWIN_PUB_UPDATE_TOPIC;
// APP_MQTT_TWIN_INIT_MSG = " "
snprintf(mqttCtx->publishMessage, sizeof(APP_MQTT_TWIN_INIT_MSG),"%s", APP_MQTT_TWIN_INIT_MSG);
}
else
{
toggle = 1;
//devices/"AZURE_DEVICE_ID"/messages/events/
mqttCtx->mqttPublish.topic_name = AZURE_IOTHUB_PUB_TOPIC;
// APP_MQTT_SMRTSCAPE_MSG = "{\"message\": \"Hello World!\"}"
strncpy(mqttCtx->publishMessage, APP_MQTT_SMRTSCAPE_MSG, sizeof(mqttCtx->publishMessage) - 1); // Hard code Smrtscape Msg
}
//strncpy(mqttCtx->publishMessage,(const char *)post_buff,sizeof(post_buff)-1); // Messages from the radio
mqttCtx->mqttPublish.buffer = (uint8_t*)mqttCtx->publishMessage;
mqttCtx->mqttPublish.total_len = (uint16_t)strlen(mqttCtx->publishMessage);
///////////////////////////
SYS_CONSOLE_PRINT("Verify - IOT_HUB Publish Topic %s \r\n Message %s\r\n", mqttCtx->mqttPublish.topic_name,mqttCtx->mqttPublish.buffer);
}
else
{
mqttCtx->mqttPublish.topic_name = AZURE_EVENT_TOPIC;
SYS_CONSOLE_PRINT("Verify - Device Registration Publish Topic %s \r\n Message %s\r\n", mqttCtx->mqttPublish.topic_name,mqttCtx->mqttPublish.buffer);
}
}
///////// Abhinav Modified /////////////
#else
mqttCtx->mqttPublish.topic_name = AZURE_EVENT_TOPIC;
SYS_CONSOLE_PRINT("Verify - Device Publish Topic %s \r\n Message %s\r\n", mqttCtx->mqttPublish.topic_name,mqttCtx->mqttPublish.buffer);
#endif
#ifdef WOLFMQTT_V5
// payload format indicator
mqttProp = MqttClient_PropsAdd(&mqttCtx->mqttPublish.props);
if(mqttProp)
{
mqttProp->type = MQTT_PROP_PLAYLOAD_FORMAT_IND;
mqttProp->data_int = 1;
}
// set the content type
mqttProp = MqttClient_PropsAdd(&mqttCtx->mqttPublish.props);
if(mqttProp)
{
mqttProp->type = MQTT_PROP_CONTENT_TYPE;
mqttProp->data_str.str = APP_MQTT_DEFAULT_CONTENT_TYPE;
mqttProp->data_str.len = (uint16_t)strlen(mqttProp->data_str.str);
}
if(mqttCtx->topicAliasMax > mqttCtx->topicAlias && mqttCtx->topicAlias > 0)
{ // set the topic alias
mqttProp = MqttClient_PropsAdd(&mqttCtx->mqttPublish.props);
if(mqttProp)
{
mqttProp->type = MQTT_PROP_TOPIC_ALIAS;
mqttProp->data_short = mqttCtx->topicAlias;
}
}
mqttCtx->propList = mqttCtx->mqttPublish.props;
#endif
// Derrek Changed
mqttCtx->requestStop = true; // OCB has this as true
mqttCtx->msgReceived = false; // OCB has this as false
mqttCtx->waitMsgRetries = APP_MQTT_WAIT_MESSAGE_RETRIES;
mqttCtx->currState = APP_MQTT_STATE_PUBLISH;
publish_timeout=0;
break;
case APP_MQTT_STATE_PUBLISH:
resCode = MqttClient_Publish(&mqttCtx->mqttClient, &mqttCtx->mqttPublish);
if (resCode == MQTT_CODE_CONTINUE)
{
break;
}
APP_MQTT_ClientResult(mqttCtx, "MqttClient_Publish", resCode) ;
if (resCode != MQTT_CODE_SUCCESS)
{
mqttCtx->currState = APP_MQTT_STATE_NET_DISCONNECT;
break;
}
else
{
SYS_CONSOLE_PRINT("MQTT Task - Published Topic: %s\r\n", mqttCtx->mqttPublish.topic_name);
}
mqttCtx->currState = APP_MQTT_STATE_START_WAIT;
// fall through
1 answer
Sort by: Most helpful
-
Matthijs van der Veer 4,376 Reputation points MVP
2022-01-04T18:44:19.057+00:00