Sending commands from nRF Cloud to LTE Device

Hi!

In our project, we need to send cloud-to-device commands. By "command" I mean a request to do some short action, not a request to change device's configuration/state. We see two ways of sending such requests:

1. C2D messages

2. Device shadow

What are the pros and cons of each way?

Here I see a recommendation to use Device shadow for commands sending. But if we talk about commands in the sense I described above, would this recommendation be still actual?

If yes, is there a recommended way (or some sample) of Device shadow usage for sending such commands? I mean, which fields Device shadow should contain and some algorithm of updating desired and reported section during sending command and its execution.

In case of C2D messages, the main problem seems to be command delivery when the device is offline. Right? Or is there some mechanism for this case in nRF Cloud?

Thank you!

  • Hi  ,

    Ah, thanks -- using "state" works as expected ... now the cmd is processed and appears in "reported: config: cmd" in the full shadow. I think that addresses my first problem. For my information, can you explain why "state" is necessary when sending a device update via MQTT but is not allowed when using the API (UpdateDevice)?

    However, the problem I still have is: When the device is online, it receives the delta update in the "desired: config: cmd" section (and can now process it, as above). But when the device is offline and then comes online, how should it retrieve the pending delta update? 

    Thanks,
    -- Terrence

  • why "state" is necessary when sending a device update via MQTT but is not allowed when using the API (UpdateDevice)?

    "state" is necessary when interacting with the shadow via MQTT because that is what AWS requires.
    it is not needed in the REST API since that is a custom nrf cloud API.

    the device should subscribe to the /update/delta topic and it should receive delta events until they are processed. 

  • Hi  ,

    "state" is necessary when interacting with the shadow via MQTT because that is what AWS requires.
    it is not needed in the REST API since that is a custom nrf cloud API.

    Got it, thanks.

    the device should subscribe to the /update/delta topic and it should receive delta events until they are processed. 

    Unfortunately, the device simulator doesn't get any delta events after connecting. 

    Here's the delta event when the device simulator is connected:

    ************** MESSAGE RECEIVED ***********

    TOPIC: <deviceid>/shadow/update/delta

    MESSAGE: {

      "state": {

        "nrfcloud_mqtt_topic_prefix": "prod/.../",

        "pairing": {

          "state": "paired",

          "topics": {

            "d2c": "prod/...",

            "c2d": "prod/..."

          }

        },

        "config": {

          "cmd": "command"

        }

      }

    }

    *******************************************

    Then I stop the device simulator and send a new "cmd: command1" update via the cloud API. Then I start the device simulator. It subscribes (again) to /update/delta and fetches the full shadow:

    ************** MESSAGE RECEIVED ***********

    TOPIC: <deviceid>/shadow/get/accepted

    MESSAGE: {

      "desired": {

        "pairing": {

          "state": "paired",

          "topics": {

            "d2c": "prod/...",

            "c2d": "prod/..."

          }

        },

        "nrfcloud_mqtt_topic_prefix": "prod/..."

      },

      "reported": {

        "control": {

          "alertsEn": true,

          "logLvl": 3

        }

      },

      "config": {

        "activeMode": true,

        "locationTimeout": 300,

        "activeWaitTime": 300,

        "movementResolution": 120,

        "movementTimeout": 3600,

        "accThreshAct": 4,

        "accThreshInact": 4,

        "accTimeoutInact": 60,

        "nod": []

      }

    }

    *******************************************

    The "cmd: command1" is not present. And even if the device simulator continues to be connected it never receives a delta update of the "cmd: command1". 

    Thanks,
    -- Terrence

  • Hello, Justin is not able to contribute to cloud support anymore. I'll take over. Apologies for the delay. 

    It is clear that our documentation needs more info on this topic. Issue reported

    The notify of shadow update while device was offline does not seem to work as Justin describe. I'll try to reproduce the issue with simulator v2 

  • Hello,

    So, there are two separate topics to subscribe. Please see https://docs.nordicsemi.com/bundle/nrf-cloud/page/APIs/MQTT/Topics.html#shadow-topics 

    `${deviceId}/shadow/get/accepted` - nRF Cloud will send delta only when you send empty message on  $aws/things/${deviceId}/shadow/get.
    `${deviceId}/shadow/update/delta` - nRf Cloud will send event automatically when change happens
    SO, you need to subscribe both. And when device is connecting again after offline period you send a empty message to the $aws/things/${deviceId}/shadow/get. This will trigger nRF Cloud to send the delta to the "get/accepted".
    Justin's coment that cloud would be sending even on "update/delta" is incorrect in the use case you described. Yes could will be sending the message if there is connection (device is online and keeping the connection alive). But if device is sleeping, the connection will close after 30 seconds. 
Related