forked from TanKucukhas/Cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiotUpdateShadow.js
58 lines (39 loc) · 1.68 KB
/
iotUpdateShadow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
var aws = require('aws-sdk');
// aws-sdk is a default part of Lambda. Install locally with NPM: https://www.npmjs.com/package/aws-sdk
// Full doc on aws-sdk http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/welcome.html
const config = {};
config.IOT_BROKER_ENDPOINT = "a2eshrcp6u0y0c.iot.us-east-1.amazonaws.com".toLowerCase();
config.IOT_BROKER_REGION = "us-east-1";
config.IOT_THING_NAME = "waterPump";
// MQTT clients can subscribe to topic $aws/things/waterPump/shadow/update/accepted
module.exports = {
setShadow: (payload, callback) => {
var payloadObj = { "state":
{
"desired": payload
}
};
//Prepare the parameters of the update call
var paramsUpdate = {
"thingName" : config.IOT_THING_NAME,
"payload" : JSON.stringify(payloadObj)
};
aws.config.region = config.IOT_BROKER_REGION;
//Initializing client for IoT
var iotData = new aws.IotData({endpoint: config.IOT_BROKER_ENDPOINT});
console.log("new aws.IotData made");
console.log("config = " + JSON.stringify(config));
//Update Device Shadow
iotData.updateThingShadow(paramsUpdate, function(err, data) {
if (err){
console.log("error calling updateThingShadow ", err);
callback("not ok");
}
else {
console.log("updated the shadow");
// console.log(data);
callback("ok");
}
});
}
}