question

11989479 avatar image
0 Votes"
11989479 asked 11989479 answered

[Azure IoT SDK]How can I set/get/monitor the value of ModuleTwin?

I am implementing a IoT Edge App, and I want to set/get the value of ModuleTwin and monitor the value change of ModuleTwin.

I refer to the sample:
https://github.com/Azure/azure-iot-sdk-java/blob/master/device/iot-device-samples/module-twin-sample/src/main/java/samples/com/microsoft/azure/sdk/iot/ModuleTwinSample.java

My understanding is following:
1. set ModuleTwin: ModuleClient.sendReportedProperties()
2. get ModuleTwin: ModuleClient.getTwin()
3. monitor ModuleTwin: ModuleClient.subscribeToTwinDesiredProperties()
Am I right?

I try to test with iotedgehubdev, but after excute client.getTwin(), there is no callback excuted.

Output logs:

 MyFirstModule    | Client created
 MyFirstModule    | Open connection to IoT Hub.
 MyFirstModule    | Connection Status: Connected
 MyFirstModule    | Start device Twin and get remaining properties...
 MyFirstModule    | IoT Hub responded to device twin operation with status OK
 MyFirstModule    | Subscribe to Desired properties on device Twin...
 MyFirstModule    | Get device Twin...

What's the problem?

azure-iot-sdk
· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I'm sorry, I made a mistake.

I do not add any properties in ModuleTwin at IoT Hub side.
After I add any property, I can get both reported properties and desired properties now.

But I still have a problem.
SDK said "this module client will receive a callback each time a desired property is updated" after startTwin.

I try to modify any value of desired properties in ModuleTwin at IoT Hub side, but do not recevie any callback.

What should I do? thanks.

0 Votes 0 ·

I try to use the following API:
startTwin(IotHubEventCallback deviceTwinStatusCallback, Object deviceTwinStatusCallbackContext, DeviceTwin.TwinPropertiesCallback genericPropertiesCallBack, Object genericPropertyCallBackContext)

But i met below build error:

 [ERROR] COMPILATION ERROR : 
 [INFO] -------------------------------------------------------------
 [ERROR] /home/Work/MyEdgeSolution2/modules/MyFirstModule/src/main/java/com/edgemodule/App.java:[8,53] 找不到符号
   符号:   类 TwinPropertiesCallback
   位置: 程序包 com.microsoft.azure.sdk.iot.device.DeviceTwin
 [INFO] 1 error

How can I fix it?

0 Votes 0 ·

Hi @11989479 ,
It is my understanding that you are customizing the sample from here correct?

If you run the sample as is, do you still have compilation errors and you don't receive a notification for desired properties changes on ModuleTwin?

Please note that this Module Twin is related with an Azure IoT Hub Device and not related with a Module on IoT Edge.

See: Understand and use module twins in IoT Hub

Module identity and module twins provide a management separation of concerns when working with IoT devices that have modular software components.

The sample you are using relates with the above.
Thanks!


0 Votes 0 ·
Show more comments

@11989479 I believe you have found the workaround for the initial issue reported in this thread. To make it more clear, can you share and verify the answer with others?

Thanks.

0 Votes 0 ·

1 Answer

11989479 avatar image
1 Vote"
11989479 answered

We can use the following APIs:

  1. Get Module Twin:
    ModuleClient.startTwin(IotHubEventCallback deviceTwinStatusCallback, Object deviceTwinStatusCallbackContext, DeviceTwin.TwinPropertyCallBack genericPropertyCallBack, Object genericPropertyCallBackContext)

  2. Set Module Twin:
    InternalClient.sendReportedProperties(Set<DeviceTwin.Property> reportedProperties)

  3. Monitor Module Twin:
    InternalClient.subscribeToTwinDesiredProperties(Map<DeviceTwin.Property,DeviceTwin.Pair<DeviceTwin.TwinPropertyCallBack,Object>> onDesiredPropertyChange)

For details we can refer to sample:
https://github.com/Azure/azure-iot-sdk-java/blob/master/device/iot-device-samples/module-twin-sample/src/main/java/samples/com/microsoft/azure/sdk/iot/ModuleTwinSample.java

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.