AWS IoT for ESP32 v1.0.0
An ESP-IDF based solution
|
The device shadow service is offered by AWS IoT (AWS) that provides services to manage device shadow using MQTT APIs. The device shadow, as the name implies, represents a shadow of the actual device on AWS cloud. This shadow holds the information about the current state of the device.
A shadow is used to store and retrieve information about the current state of the device. AWS uses JSON format to represent a device shadow.
A device shadow with led state and led color information:
To change the state of a device a shadow update request must be sent to AWS.
AWS manages shadow updates and has defined three states that describe the aspects of shadow updates.
State | Purpose |
---|---|
Desired | App requests for a Desired state. |
Reported | Device reports its Current state. |
Delta | It exists when there is a difference between Desired & Reported states. It is Reported by AWS IoT as a way to indicate which shadow element has been updated. |
The JSON document in the shadow section was a simplified version. The actual JSON used by AWS for handling shadow updates is shown below.
Let's consider a simple scenario to understand how AWS handles shadow update process.
Let's assume the light bulb is off and let's omit the color from the device shadow to keep it simple. The current state of the device is now defined.
Device shadow with bulb off:
The app sends a shadow update request indicating the user desired state.
As a result the desired state is updated in device shadow.
On seeing this difference between the desired and reported state AWS reports delta state, which is to turn on the bulb.
The device actually subscribes to shadow delta topic. When a delta state exists AWS notifies the device by publishing a message on the delta topic. On receiving delta state device should perform desired action, here it turns on the bulb.
To perform an action the library callbacks a delta handler which is registered using the AWS_shadowDeltaRegister
API.
After turning on the light bulb, the device reports that it is now on by updating its shadow.
The device updates reported state using AWS_shadowUpdateReported
API.
The ligh bulb is now on and the device shadow now represents the current state of the bulb. Take a look at the device shadow and compare it with the shadow before the bulb was turned on.
Device shadow with bulb on:
The library handles shadow update at element level. This means for shadow update with delta state you have to register a callback handler for each element using AWS_shadowDeltaRegister
API. If your shadow contains two elements led
and color
, this means that you will need to register callbacks for both.
The steps below explains the necessary configuration and initialization required to manage device shadow by using the library. Ensure your device is provisioned with certificates otherwise you have to manually configure the device certificates.
Define the shadow elements and callback handlers. You will also need variables to handle the desired and reported state values.
Sample variables to handle led state:
Sample shadow element:
Sample shadow update callback handler:
Please refer to step #1 of Publish-Subscribe user guide to use thing certificates.
If you want to use other two methods then please read the Provisioning methods of Lib AWS under Overview section and configure the system configuration variable of type systemInitConfig_st
.
Using the above configuration initialize the system and connect to AWS.
Refer step #2 of the update process, the device should perform some action.
Sample application task code: