0

I am using the Azure IOT SDK, trying to send a Cloud to Device Message with a string body and receive it in the SDK. No matter what I send, the SDK says its in a byte array format. How do I send a string instead of a byte array from the cloud to my device SDK?

This is the code sample I followed: https://github.com/Azure/azure-iot-sdk-c/blob/master/iothub_client/samples/iothub_ll_c2d_sample/iothub_ll_c2d_sample.c

This is my callback:

IOTHUBMESSAGE_DISPOSITION_RESULT CAzureProcessor::ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
{
    CMyObject* pObject = (CMyObject*)userContextCallback;
    if (pObject != NULL)
    {
       IOTHUBMESSAGE_CONTENT_TYPE content_type = IoTHubMessage_GetContentType(message);
        if (content_type == IOTHUBMESSAGE_STRING)
       {
            //do stuff
       }
    }
    return IOTHUBMESSAGE_ACCEPTED;
}

My problem is I never get to 'dostuff' because IoTHubMessage_GetContentType always returns IOTHUBMESSAGE_BYTEARRAY when I send a C2D message either from the "Message to Device" page on the Azure portal or from Device Explorer. How do I go about sending a string from either of those two points?

If it helps, this is the string I am trying to send:

[{"devicename":"My ROC","alias":"elevation","value":100}]

Timothy John Laird
  • 1,101
  • 2
  • 13
  • 24
  • I ended up calling IoTHubMessage_GetByteArray and null terminating the byte array to make a string. It would still be interesting to know if there is a better way. – Timothy John Laird Oct 29 '19 at 22:25
  • Did you set content type to "text/plain" when sending the c2d message? Alternatively, you might be better off just setting your content type to "application/json" if your are sending JSON anyway :-) – kartben Nov 03 '19 at 09:01

1 Answers1

0

Don't know if you had check this post because I use a variant of the same solution given and it works well for me for C2D communication.

Sending Cloud to Device Messages using IoT DevKit and Azure IoT Hub - Device Code

Hope this helps.

Zainu
  • 120
  • 1
  • 7