AWS Shadow method of sending receiving


#1

Hi,

How exactly does the AWS Shadow service work regarding the “controlled publish period” example? Let me describe an example device with only one button:

I can switch on the button, I suppose the thing shadow button_state in the “reported” part should be changed to “on”, like:

{
“desired”: {
“button_state”: 0
},
“reported”: {
“button_state”: 1
}
}

But the problem is, I get a shadow_callback via this function: thing.on_shadow_request(shadow_callback) where the button state 0 is requested again… what is the correct behaviour here?


#2

Hi @Marcel,

in your case the shadow callback should look like this:

def shadow_callback(requested):
    print('requested button state:', requested['button_state'])
    button_state = requested['button_state']
    return {'publish_period': button_state}

From AWS IoT you should edit the “desired” section with the state you want to be sent to the device.
When the device returns from the callback it sends back a shadow update which edits the “reported” section.


#3

Hi LorenzoR,

But what if the desired state is: “OF” and I sent this to the shadow (reported):
thing.mqtt.publish(“dev/sample”, json.dumps({ ‘button_state’: “ON” }))

Then the shadow_callback is fired and the desired state “OFF” is pushed to the device again.

Or should I remove the “desired” part of the shadow? I hope you understand my question :slight_smile:


#4

Hi @Marcel,

the state pushed to the device is controlled by the “desired” section manually.
When you send back a state from the device it only updates the “reported” section without affecting what is going to be sent next to the device.

Hope I got it right :slight_smile:


#5

“When you send back a state from the device it only updates the “reported” section without affecting what is going to be sent next to the device.”

I see that the “reported” section is updated, but the shadow_callback is also fired after this? It looks like someone is trying to sent the “desired” section.

I appreciate your support!


#6

To clarify in more detail, when I publish a message via the AWS test console in:

$aws/things/button_test/shadow/update
Message:

{
“state”: {
“reported”: {
“button_state”: 1,
}
}
}

The shadow is updated and I don’t receive an answer. Fine for now.

But if I publish a message via Zerynth like this:
thing.mqtt.publish("$aws/things/button_test/shadow/update", json.dumps({"state": {"reported": {'button_state': ON, }}}))

The shadow_callback is fired like someone asked for a desired value and this will change the local button state again back to OFF. This way it’s impossible to set the button_state to ON.

So in general my question is, how can I control the button_state via the AWS cloud and how can I inform the Cloud about a locally changed button state


#7

Hi @Marcel,

to update your device shadow remotely go to “Manage->Things” click on the AWS thing associated to your device, then “Shadow”: Edit the Shadow’s JSON adding chosen “desired” section and click “Save”.

This action triggers the device’s callback, at the end of which, just by returning a dictionary you are allowed to update the “reported” section.

This is the correct flow for updating the state as a response to a remote request.

On the other hand, if you want to update the shadow without an external trigger you can use the udpate_shadow method which takes a dictionary like the one used as a return value in the shadow callback (without the need to specify state and reported keys).


#8

Ok, I understand the first part, but the issue I have is with this:

On the other hand, if you want to update the shadow without an external trigger you can use the udpate_shadow method which takes a dictionary like the one used as a return value in the shadow callback (without the need to specify state and reported keys).

You problaby refer to:

thing.update_shadow({'btn_state': ON,})

If I run this line of code, the reported shadow state of button_state succesfully changes to “ON”, but immediately after this the shadow_callback function is fired. And because the desired state in the shadow was set to OFF, the button state of my device imediately changes to OFF again.


#9

For now my workaround is to first delete the shadow so that the “desired” part doesn’t exist anymore, then update the shadow after this.For now it’s ok.


#10

Hi @Marcel,

unfortunately this behaviour sounds new to me, we will check as soon as possible.
Cheers