0

I am using the Azure IoT Device Client SDK for .NET Core (1.17.0-preview-001 but also tried 1.7.0-stable). When calling deviceClient.GetTwinAsync()all fields are NULLexcept for the Properties(Desired as well as Reported are there): enter image description here

At least things like DeviceId I would expect to be there. Also when I add any Tags in the DeviceTwin, those does not get down to the device.

Is this a bug or am I missing something?

silent
  • 14,494
  • 4
  • 46
  • 86

2 Answers2

1

The device twin composed with tags, properties(desired, reported) and metadata. And we can only query the properties from device app like figure below as the document stated: enter image description here

You can also refer the Azure IoT hub endpoints for the specific operation support by Azure IoT hub like figure below:

enter image description here

Device twin management. Each IoT hub exposes a set of service-facing HTTPS REST endpoint to query and update device twins (update tags and properties).

This behavior is also easy to verified by tracking the raw data using Fiddler. For example, here is a query to get device twin from Azure IoT Hub using the MQTT protocol via MQTTBOX: enter image description here

And based on my understanding, the device app rarely need that info. You may consider manage the device twin from service-endpoint instead of client app. If you do have that kind of special scenario, you can submit the feedback from Azure IoT - UserVoice .

Fei Xue
  • 14,369
  • 1
  • 19
  • 27
  • 1
    Ok, the info that in fact only Twin Properties are visible to devices was a bit too hidden for me. Or maybe the name of the Device Client SDK method GetTwinAsync() is misleading. Since in fact the device cannot read it's twin, only the properties there in. Should rather be GetTwinPropertiesAsync() ;-) But I guess that's what it is for now. Thanks for the help. – silent Mar 23 '18 at 08:30
  • Can you guys help me here with similar question: https://stackoverflow.com/questions/53643821/is-web-socket-supported-in-net-core-throwing-system-net-websockets-websocket – kudlatiger Dec 07 '18 at 05:13
-1

You can use Microsoft.Azure.Devices to get device twin.It is a service client SDK for Azure IoT Hub.

dynamic registryManager = RegistryManager.CreateFromConnectionString("{connectionString}");
var deviceTwin = await registryManager.GetTwinAsync("{device name}");

In this article,maybe you will get more understanding about Azure IoT SDKs.

  • Device SDKs enable you to build apps that run on your IoT devices. These apps send telemetry to your IoT hub, and optionally receive messages from your IoT hub.
  • Service SDKs enable you to manage your IoT hub, and optionally send messages to your IoT devices.
Michael Xu
  • 4,382
  • 1
  • 8
  • 16
  • I think you missed the point of my question: I am working on the device, not a service backend. Using the service client SDK would be a very bad idea in this case as I do not want to use service keys on the devices. – silent Mar 22 '18 at 09:09
  • @silent, you can refer to [Understand and use device twins in IoT Hub](https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-device-twins) and [IoT Hub permissions](https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-security#iot-hub-permissions),it is due to the requirement of the ServiceConnect permission. – Michael Xu Mar 22 '18 at 09:59
  • Ok, I can see there that it says: "Tags are not visible to device apps." (not sure why but I assume there is some design logic behind that). But it doesn't say anything about other data like the DeviceId. Does the same apply to all fields apart from Properties? If so, this should be stated. – silent Mar 22 '18 at 11:35
  • @silent, there is another optional way to get Device Twins,using Azure IoT Hub Device Twins REST APIs.Please refer to [Device Twin Api](https://learn.microsoft.com/en-us/rest/api/iothub/devicetwinapi/getdevicetwin). – Michael Xu Mar 23 '18 at 06:41