Home > Software engineering >  How to reject a "desired" change in AWS IoT Device Shadow
How to reject a "desired" change in AWS IoT Device Shadow

Time:01-16

We are using AWS IoT "Device Shadow" service to sync settings between cloud back-end services and our embedded device.

The device publishes its "reported" state to $aws/things/xxx/shadow/update:

{ "state": { "reported" : ... } }

And when the back-end wants to change something, it publishes a "desired" state:

{ "state": { "desired" : ... } }

This generates a delta message on $aws/things/xxx/shadow/delta, which we subscribe to on the device. This all works well.

However, sometimes the device needs to reject a change (typically because the device was offline and so the change is too old and not reliable data). In this case, we don't want to keep getting "delta" messages for this change we want to reject.

According to this post on AWS forums, we can set the "desired" state to null to clear it: [https://repost.aws/questions/QUx_YtxvdmTFODhuuNd4D_Yw/how-to-deal-with-shadow-updates-initated-by-device-not-and-server]

As per the example, I have tried sending this from our device to $aws/things/xxx/shadow/update:

{"state" : {"desired" : null } }

However, this is rejected by the server and causes a message on $aws/things/xxx/shadow/update/rejected:

{
  "code": 400,
  "message": "Payload contains invalid json"
}

The JSON looks valid. Also, this DOES work if I post it to $aws/things/xxx/shadow/update from the online MQTT client in the AWS console (i.e. it is accepted, and has the desired effect - the desired section of the device shadow is deleted).

Why do I get this error when the message is sent from the device? How do I clear / reject a "desired" change?

--- EDIT ---

It turns out this was a threading issue on the client, leading to a garbled message. A useful debug method is to subscribe to $aws/things/xxx/shadow/update in the AWS console and look at what is ACTUALLY being sent.

Setting the "desired" to null (as per the JSON shown below) does work to clear the desired changes to the device shadow.

CodePudding user response:

I quickly tested with mosquitto_pub and it seems working no problem.

ENDPOINT="<your-id>-ats.iot.<region>.amazonaws.com"
THING_NAME=ta

mosquitto_pub --cafile AmazonRootCA1.pem \
  --cert device.pem.crt \
  --key private.pem.key \
  -h $ENDPOINT \
  -p 8883 \
  -t '$aws/things/ta/shadow/name/sb/update' \
  -i pub_$THING_NAME \
  -f ./desired_is_null.json \
  -d

desired_is_null.json is as follows:

{
  "state": {
    "desired": null
  }
}

Can you try this to narrow down if the problem is client side?

  • Related