Skip to content

Commit ff8ca8f

Browse files
Add Shadow sample (#217)
* Added Shadow sample using the code in the shadow-sample branch as a base * Adjusted README in samples to include Shadow sample * Adjusted Shadow sample README policy for correct highlighting * Updated Shadow sample to await result returning rather than the puback * Removed additional console log reporting shadow update
1 parent 1daaa28 commit ff8ca8f

File tree

6 files changed

+4279
-0
lines changed

6 files changed

+4279
-0
lines changed

samples/README.md

+89
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* [pub_sub](#nodepub_sub)
44
* [pub_sub_js](#nodepub_sub_js)
55
* [pub_sub_pkcs11](#nodepub_sub_pkcs11)
6+
* [shadow](#nodeshadow)
67
* [fleet provisioning](#fleet-provisioning)
78
* [basic discovery](#nodebasic_discovery)
89

@@ -165,6 +166,94 @@ To run this sample using [SoftHSM2](https://www.opendnssec.org/softhsm/) as the
165166
node dist/index.js --endpoint <xxxx-ats.iot.xxxx.amazonaws.com> --root-ca <AmazonRootCA1.pem> --cert <certificate.pem.crt> --pkcs11_lib <libsofthsm2.so> --pin <user-pin> --token_label <token-label> --key_label <key-label>
166167
```
167168

169+
## Node/shadow
170+
171+
This sample uses the AWS IoT
172+
[Device Shadow](https://docs.aws.amazon.com/iot/latest/developerguide/iot-device-shadows.html)
173+
Service to keep a property in
174+
sync between device and server. Imagine a light whose color may be changed
175+
through an app, or set by a local user.
176+
177+
Once connected, type a value in the terminal and press Enter to update
178+
the property's "reported" value. The sample also responds when the "desired"
179+
value changes on the server. To observe this, edit the Shadow document in
180+
the AWS Console and set a new "desired" value.
181+
182+
On startup, the sample requests the shadow document to learn the property's
183+
initial state. The sample also subscribes to "delta" events from the server,
184+
which are sent when a property's "desired" value differs from its "reported"
185+
value. When the sample learns of a new desired value, that value is changed
186+
on the device and an update is sent to the server with the new "reported"
187+
value.
188+
189+
Your Thing's
190+
[Policy](https://docs.aws.amazon.com/iot/latest/developerguide/iot-policies.html)
191+
must provide privileges for this sample to connect, subscribe, publish,
192+
and receive.
193+
194+
<details>
195+
<summary>(see sample policy)</summary>
196+
<pre>
197+
{
198+
"Version": "2012-10-17",
199+
"Statement": [
200+
{
201+
"Effect": "Allow",
202+
"Action": [
203+
"iot:Publish"
204+
],
205+
"Resource": [
206+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/get",
207+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/update"
208+
]
209+
},
210+
{
211+
"Effect": "Allow",
212+
"Action": [
213+
"iot:Receive"
214+
],
215+
"Resource": [
216+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/get/accepted",
217+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/get/rejected",
218+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/update/accepted",
219+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/update/rejected",
220+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/update/delta"
221+
]
222+
},
223+
{
224+
"Effect": "Allow",
225+
"Action": [
226+
"iot:Subscribe"
227+
],
228+
"Resource": [
229+
"arn:aws:iot:<b>region</b>:<b>account</b>:topicfilter/$aws/things/<b>thingname</b>/shadow/get/accepted",
230+
"arn:aws:iot:<b>region</b>:<b>account</b>:topicfilter/$aws/things/<b>thingname</b>/shadow/get/rejected",
231+
"arn:aws:iot:<b>region</b>:<b>account</b>:topicfilter/$aws/things/<b>thingname</b>/shadow/update/accepted",
232+
"arn:aws:iot:<b>region</b>:<b>account</b>:topicfilter/$aws/things/<b>thingname</b>/shadow/update/rejected",
233+
"arn:aws:iot:<b>region</b>:<b>account</b>:topicfilter/$aws/things/<b>thingname</b>/shadow/update/delta"
234+
]
235+
},
236+
{
237+
"Effect": "Allow",
238+
"Action": "iot:Connect",
239+
"Resource": "arn:aws:iot:<b>region</b>:<b>account</b>:client/test-*"
240+
}
241+
]
242+
}
243+
</pre>
244+
</details>
245+
246+
To run the Shadow sample use the following command:
247+
248+
``` sh
249+
npm install
250+
node dist/index.js --endpoint <endpoint> --ca_file <path to root CA1> --cert <path to the certificate> --key <path to the private key> -- thing_name <thing name> --shadow_property <shadow property name>
251+
```
252+
253+
This will allow you to run the program and set the shadow property by typing in the console.
254+
Enter any value to set the shadow property to it. Enter `null` to clear the property and `clear_shadow`
255+
to clear all values. To disconnect and exit the program, enter `quit`.
256+
168257
## Fleet Provisioning
169258

170259
This sample uses the AWS IoT

0 commit comments

Comments
 (0)