none
Invoke a direct method on a device (preview)

    Question

  • Hi,

    I am having a problem to demonstrate invoking a direct method on device (preview) using a SDK .Net package.

    I can simulate an invoker by either Http request or console program built with a Microsoft.Azure.Devices Service SDK version 1.1.0-preview-004 using the following method:

    client.InvokeDeviceMethodAsync("myDevice", method);

    The problem is, I can not find a preview of the Device SDK .Net package for building a device method listener such as

    client.OnDeviceMethod(...)

    The Tutorial: Use direct methods demonstrates this new great feature using packages for Node.js. I hope, there is a Device SDK preview package for .Net to build a simulated device with c#.  

    Thanks in advance.

    Roman

      




    • Edited by Roman Kiss Wednesday, October 12, 2016 10:34 PM
    Wednesday, October 12, 2016 7:43 PM

Answers

  • Thanks for Microsoft team for effort to release the Microsoft.Azure.Devices.Client SDK (version 1.1.4) in C#.

    I think, this thread can be closed, see more details here

    Thanks

    Roman



    • Marked as answer by Roman Kiss Friday, December 16, 2016 7:11 AM
    • Edited by Roman Kiss Friday, December 16, 2016 7:12 AM
    Friday, December 16, 2016 7:11 AM

All replies

  • Tuesday, October 25, 2016 9:07 PM
  • Hi IoTGirl,

    - my question is related to the non-telemetry messaging such as Invoke a direct method on a device (preview). Based on the Tutorial: Use direct method, I have built a simulated device using a Node.js where the following install for Node.js is used:

    npm install azure-iothub@dtpreview --save

    I would like to build a simulated device for direct method using the Azure IoT device SDK for .Net. The problem is, I cannot find a nuget package for this preview. All demonstrations for this preview (direct method, twins, etc.) on the device face is done and supported using a Node.js.

    Thanks

    Roman



    • Edited by Roman Kiss Wednesday, October 26, 2016 3:56 AM
    Wednesday, October 26, 2016 3:56 AM
  • It seems that this functionality is not yet implemented in the C# SDK :-(.

    You can find the source of the Preview SDK here: https://github.com/Azure/azure-iot-sdks-preview

    Right now I am debugging the Node Implementation to figure out what they do :-)

    Thnx

     Andreas

    Thursday, November 03, 2016 12:20 PM
  • Thanks Andreas,

    - yes, it looks like the C# SDK is missing, now. I hope we will have it soon, all preview features are great and very useful in the IoT architecture.

    I already found the link of the source of preview. There is an updated tool "Device Explorer Twin" which is now for preview features such as the twin and CallMethod on Device.

    btw. I have little bit modified its source code (file DeviceTwinAndMethod.cs) in two places:

    - method GetDeviceTwinData for Json formatting text 

    if (deviceTwin != null)
    {
      result.deviceJson = deviceTwin.ToJson(Formatting.Indented);
      result.tagsJson = deviceTwin.Tags.ToJson(Formatting.Indented);
      result.reportedPropertiesJson = deviceTwin.Properties.Reported.ToJson(Formatting.Indented);
      result.desiredPropertiesJson = deviceTwin.Properties.Desired.ToJson(Formatting.Indented);
    }
     

    the other place was necessary to parse the Method payload text:

    - I have added in the method CallDeviceMethod the following line:

    deviceMethodPayload = 
        JRaw.Parse(deviceMethodPayload).ToString();

    Thanks

    Roman

     


    • Edited by Roman Kiss Thursday, November 03, 2016 1:40 PM
    Thursday, November 03, 2016 1:39 PM
  • Cool I tried that Roman and it worked out fine. Although this still requires the device to be in NodeJS. The sender sends with AMPQ but the receiver still needs to be an MQTT client right now.

    Therefore I tried to add an additional subscription to "$iothub/methods/POST/#" in the MqttIotHubAdapter in the Microsoft.Azure.Devices.Client library but the client still reporting a "method not found". I looks like if the message never is sent to the client (I do not even receive packages at TSL level).

    Thursday, November 03, 2016 5:00 PM
  • - Yes, the device-faced must be MQTT client, and for now we have only Node.js implementation. Try also a twin properties feature with an eventing pattern between the device and service faces.

    The service-facing endpoint can be used also HTTPS, the following is an example of my test (using an Advanced Rest Client Application from the Chrome Web Store) to invoke a direct method on the device:


     Thanks

     Roman

    • Edited by Roman Kiss Thursday, November 03, 2016 7:40 PM
    Thursday, November 03, 2016 7:39 PM
  • I know. I am trying to implement that missing functionality to the C# client (for fun purpose). And although I am pretty sure I set up the same subscriptions I am missing something. It is a bit frustrating, but I want to figure that out :-)
    Thursday, November 03, 2016 8:26 PM
  • The reason IOT Hub was not sending anything to my C# Client lies in the MQTT Connect package not in the subscriptions:

    You need to modify the username from:

    • <yourhubName>.azure-devices.net/<yourDeviceID>

    To:

    • <yourHubName>.azure-devices.net/<yourDeviceID>/DeviceClientType=azure-iot-device%2F1.1.0-dtpreview&api-version=2016-09-30-preview

    Additionally I added a topic subscription to $iothub/methods/POST/# and then you will receive the messages. This additionally needs some fixing in code, but at least they are coming :-)

    Thnx

     Andreas

    Thursday, November 03, 2016 9:11 PM
  • Roman, I have made it work :-). Read how I did it here: https://blog.spectologic.com/2016/11/04/direct-methods-with-iothub-in-c/

    Cheers

     Andreas

    Friday, November 04, 2016 12:06 AM
  • Thanks Andreas.

    I have looked at your implementation, it's a great workaround, but I will wait for MS release with a full implementation in C# for preview features (twin, method, job, etc.)

    Roman



    • Edited by Roman Kiss Friday, November 04, 2016 1:38 PM
    Friday, November 04, 2016 1:37 PM
  • Of course Roman :-) - This is a hack and was an interesting journey into the depths of both library implementations.
    Friday, November 04, 2016 2:06 PM
  • Hi Andreas,

    - I am trying to simulate a device client using the 3rd party tools like the MQTTBox, MQTTlens or HivoMQ based on the setup described in the azure document IoT Hub MQTT support - Using the MQTT protocol directly.

    I am facing a problem with the MQTT Connect. The tools didn't show up any details about the error reason. I also made a change in the username like you suggested, but still the tools can not made a successful connect to the Azure IoT Hub.

    Please, could you verify with your device if the above tools can be connected to the Azure IoT Hub?

    Thank you so much.

    Roman



    • Edited by Roman Kiss Saturday, November 26, 2016 3:45 PM
    Saturday, November 26, 2016 3:44 PM
  • Hi Roman,
    After some struggle I was able to connect the MQTTBOX:

    Here some definitions:

    $deviceId = "myDeviceID"
    $iotHubName = "myiotHubName"
    $host = $iotHubName + ".azure-devices.net:8883"
    $username = $iotHubName + ".azure-devices.net/" + $deviceId 

    Optional you can also add the following string to the username to enable methods:

    "/DeviceClientType=azure-iot-device%2F1.1.0-dtpreview&api-version=2016-09-30-preview".

    I calculated the password using the device explorer. The result looks like this

    HostName=myiotHubName.azure-devices.net;DeviceId=myDeviceId;SharedAccessSignature=SharedAccessSignature sr=myiotHubName.azure-devices.net%2Fdevices%2FmyDeviceId&sig=<signature>

    Then I took only the SharedAccessSignature value as password:

    $password = SharedAccessSignature sr=myiotHubName.azure-devices.net%2Fdevices%2FmyDeviceId&sig=<signature>

    Finally I set the connection parameters in MQTT Box like this

    MQTT Client Name = $deviceId
    MQTT Client Id = $deviceId
    Append timestamp to MQTT Client = No
    MQTT v 3.1.1 compliant = Yes
    Protocol = mqtts / tsl
    Host = $host
    Clean Session = yes
    AutoConnect on Launch = no
    SSL/TSL Version = Auto
    User Name = $username
    Password = $password

    I have also tried to subscribe to "devices/myDeviceId/messages/devicebound/#" but when I sent the message via the Node-Sample-App I never received anything in MQTT-Box. I figured out that this subscription topic finally lets me receive the direct messages in MQTT Box:

    Topic to subscribe: $iothub/methods/POST/#

    The result looks like this:

    qos : 0, retain : false, cmd : publish, dup : false, topic : $iothub/methods/POST/writeLine/?$rid=3, messageId: , length : 62, Raw payload : 3497321081051101013211611132981013211911410511611610111034

    Thnx

     Andreas



    • Edited by Andreas Pollak Thursday, December 08, 2016 8:26 PM added subscription topic
    Thursday, December 08, 2016 7:54 PM
  • Thanks Andreas,

    finally, after few days I have made workable this MQTTBox Client with Azure IoT Hub (MQTT broker), have a look at this

    All MQTT device features are working well in the combo MQTTBox - Azure IoT Hub - Device Explorer Twin (or Https)

    Thanks again for your help.

    Roman



    • Edited by Roman Kiss Thursday, December 08, 2016 9:59 PM
    Thursday, December 08, 2016 8:51 PM
  • Thanks for Microsoft team for effort to release the Microsoft.Azure.Devices.Client SDK (version 1.1.4) in C#.

    I think, this thread can be closed, see more details here

    Thanks

    Roman



    • Marked as answer by Roman Kiss Friday, December 16, 2016 7:11 AM
    • Edited by Roman Kiss Friday, December 16, 2016 7:12 AM
    Friday, December 16, 2016 7:11 AM