1. In this lab we're going to cover the basics of "thing" shadows. We'll modify our existing "thing" to hold some
values and then we'll two clients to manage our shadow. One to check it's current state and one to send a
change via the shadow.
We will also look at the EV3 robotic demo which uses shadow state to control it's movements.
Let's run a script to create our shadow and then we'll use another one to read it back.
Create a new script file called "setshadow.js".
Add your boilerplate code to connect AWS IoT.
Add in the shadow message which we will publish in the device connection action.
Lab 5 - Thing Shadows
Step 1
2. var awsIot = require('aws-iot-device-sdk');
var device = awsIot.device({
"host": "data.iot.us-west-2.amazonaws.com",
"port": 8883,
"clientId": "1234",
"thingName": "thingtest",
"caPath": "./root-CA.cer",
"certPath": "./certificate.pem.crt",
"keyPath": "./private.pem.key",
"region": "us-west-2"
});
var msg = {
"state": {
"reported": {
"temperature": 75
},
"desired": {
"temperature": 75
}
}
}
device.on('connect', function() {
console.log('Connected!');
setTimeout(function() {
device.publish('$aws/things/thingtest/shadow/update', JSON.stringify(msg));
console.log('Set shadow value.');
}, 2500);
});
You'll notice we're setting the shadow on "thingtest" - we are connecting as this "thing" and our policy let's us
make this change.
You can now run this:
node setshadow.js
If you want, you can see this in the console, click on "thingtest" and you should see the shadow. To make this
more robust you can subscribe to the update/accepted and update/rejected incase there was an issue with
your request.
JavaScript
Bash
3. Next we're going to create a new script to read our shadow state. Use the follow to get started, enter your end
point values for your registered "thing":
Step 2
4. var awsIot = require('aws-iot-device-sdk');
var device = awsIot.device({
"host": "data.iot.us-west-2.amazonaws.com",
"port": 8883,
"clientId": "1234",
"thingName": "thingtest",
"caPath": "./root-CA.cer",
"certPath": "./certificate.pem.crt",
"keyPath": "./private.pem.key",
"region": "us-west-2"
});
device.on('connect', function() {
console.log('Connected!');
setTimeout(function() {
device.subscribe('$aws/things/thingtest/shadow/update/delta');
}, 2500);
console.log('Waiting ...');
});
Now on our connect event we'll use mqtt pub/sub model to access our shadow information. In this case we're
going to publish to the "thing" shadow get topic.
We next need to add a callback to our code to see the incoming sub message. Add the following:
device.on('message', function(topic, message) {
console.log('message', topic, message.toString());
});
Your full source code should look like the following:
JavaScript
JavaScript
5. var awsIot = require('aws-iot-device-sdk');
var device = awsIot.device({
"host": "data.iot.us-west-2.amazonaws.com",
"port": 8883,
"clientId": "1234",
"thingName": "thingtest",
"caPath": "./root-CA.cer",
"certPath": "./certificate.pem.crt",
"keyPath": "./private.pem.key",
"region": "us-west-2"
});
device.on('message', function(topic, message) {
console.log('message', topic, message.toString());
});
device.on('connect', function() {
console.log('Connected!');
setTimeout(function() {
device.subscribe('$aws/things/thingtest/shadow/update/delta');
}, 2500);
console.log('Waiting ...');
});
You can now run this code as follows:
node thingtest.js
While this is running, change the values of your shadow state in setshadow.js and run setshadow.js again while
thingtest.js is still running:
node setshadow.js
You should see the output of your shadow.
NOTE - you can also update the shadow via the AWS IoT dashboard by clicking on your thing and
updating it's shadow.
JavaScript
Bash
Bash
6. There is now a "thing" shadow wrapper available on Github, you can use this to interact with a specific shadow
or set of shadows. As an exercise, try implement the above using this wrapper:
[https://github.com/aws/aws-iot-device-sdk-js/blob/master/examples/thing-example.js]
Further reading