6 Replies Latest reply on Aug 30, 2017 3:06 AM by gbrentegani RSS
    gbrentegani Newbie

    Cloud To Device comunication

    Hi everyone,

     

    I want to talking about Cloud to Device Communication.

     

    One our project have to use the Message to Device communication.

     

    A Rasperry is connected to the machine by serial port and with software into it is able to send machine's data to iot hub.

     

    The communication need to be bidirectional, because we have to send commands to the machine.

     

    The software into the raspberry receives messages through a device-specific-endpoint (device/{DeviceId}/messages/devicebound) and we can't modify this software.

     

    As seen below, the message is shipped correctly, raspberry knows the command {"Id":"0x02"} and responds correctly. We have a lot of command with different "Id".

     

     

    So, the only way to send message from Cloud to Device is using the AMQP protocol throught microsoft ServiceBus. This is possible only by writting a custom application in C#,nodeJS and Java.

     

    (for more details see: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-messages-c2d)

     

    In Thingworx we create a HTTP REST service :

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    var params = {
    thingName: deviceID /* STRING */
    };
    
    
    // result: INFOTABLE dataShape: AzureIotHubDeviceDataShape
    var pop = Things["ConnectionServicesHub"].DescribeAzureIotDevice(params);
    
    
    
    
    
    
    var isConnected = pop.rows[0].connectionState;
    
    
    logger.warn(isConnected);
    
    
    if( isConnected == "Connected" ){
       
        //INIZIO - login
        var params_login = {
            url: "http://iothubproduction.azurewebsites.net/api/login",
            content: "username=iothubowner&password=iothubpassword@!&grant_type=password"
        };
    
    
    var response = JSON.parse(Resources["ContentLoaderFunctions"].PostText(params_login));
       
    var access_token = response.access_token;
    //FINE- login
    
        //INIZIO - chiamata REST
      var params_rest = {
            headers:{
                "Authorization":"Bearer " + access_token,
                "Content-Type":"application/json"
            },
            url: "http://iothubproduction.azurewebsites.net/api/cloudtodevice/sendmessage",
            content: {"DeviceID":deviceID , "Value":value},
            password: "MyPASSWORD",
            username: "MyAdministrator"
        };
       
        response = Resources["ContentLoaderFunctions"].PostJSON(params_rest);
    
    
        if ( response.state == null )
          {
        response = false;
          }
       
        else{
            if( response.stateMessage == "OK" )
                        {
                response = true;
                        }
           
                        else
                        {
                            response = false;
                        }
            }
    
    
        var result = response;
        logger.warn("INVIATO");
    //FINE - chiamata REST
    }
    
    
    else if (isConnected == "Disconnected") {
    
    var result=false;
    }
    

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

     

    This service Send DeviceID (i.e  the name of  remote azure Thing) and Value (i.e JSON with the command Id) as input parameters of our C# application.  (Documentation:Cloud-to-device messages with Azure IoT Hub (.NET) | Microsoft Docs)


    The C# application send message(Value) through microsoft serviceBus in AMQP protocol and it is the only way to send message cloud to device.


    Sending message C2D by HTTP protocol is not supported yet.



    As Follow the service works correctly.


    Sending Message to device comunication will be integrated in the azure connector?


    Service WriteAzureProperty  in the connectionServiceHub how it works?


    Thanks.



    Gabriele



      • Re: Cloud To Device comunication
        ekobyakov-2 Explorer

        Connector supports delivering of property changes from platform to the edge. It is doing that under the hood by invoking WriteAzureProperty method on ConnectionServicesHub. Executing that service will send a cloud-to-device message to the device via the IoT Hub.

         

        Here is the sample code to receive cloud-to-device massages at the edge: Cloud-to-device messages with Azure IoT Hub (Node) | Microsoft Docs

          • Re: Cloud To Device comunication
            gbrentegani Newbie

            Hi,

             

            I tested this service and I tried to send the my formatted message:

             

            I need to send a json like this:  {"Id":"0x02"} ,

             

            I tried to set parameters in WriteAzureProperty like this:


             

             

            var params = {
            propertyName: Id/* STRING */,
            thingName: MYTHING /* STRING */,
            propertyValue: 0x02 /* VARIANT */
            };
            
            
            // no return
            Things["ConnectionServicesHub"].WriteAzureProperty(params);
            
            

             

             

             

            Message that I receive on my Device:

             

            {"Id":{"value":"0x02","baseType":"STRING"}}


            Is there a possibility to modify the message?   I would need this: {"Id":"0x02"}



              • Re: Cloud To Device comunication
                aanjan Heavyweight Champ

                Gabriele, do you have a remote property on ThingWorx associated with the 'Id' property on your device/ Hub? If so, you can directly set the value on the property itself.

                  • Re: Cloud To Device comunication
                    gbrentegani Newbie

                    Hi Aanjan, I tried to set the property and yes it works.

                     

                    But it is works for one property at a time.

                     

                    The message that I need to send can be composed with others properties:

                     

                    They are commands, so for example:

                     

                    • {"Id":"0x02"}                                                                           (the Device response me with his status)

                                                                                    

                    • {"Id":"0x10","SubId":"0x01","NumRigheLog":"0x00"}            (the Device sends me the contability)

                     

                    In this case( {"Id":"0x10","SubId":"0x01","NumRigheLog":"0x00"}  ) I created a service like this to set the properties:

                     

                     

                    {me.Id="0x10",me.SubId="0x01",me.NumRigheLog="0x00"}

                     

                     

                    but javascript is single-threaded and the result is sending one by one, the Device receives three JSON :

                     

                    1.  {"Id":"0x10"}

                     

                    2.  {"SubId":"0x01"}

                     

                    3.  {"NumRigheLog":"0x00"}

                     

                    The device is not able to interpret this command.

                     

                    Device knows this single json {"Id":"0x10","SubId":"0x01","NumRigheLog":"0x00"};




                    I need this:



                    On the device:

                     

                    So how can I do this with Thingworx?

                     

                    It is possible with WriteAzureProperty or I have to modify all my code on the Devices?

                     

                     

                    Thanks