Forked from dave-malone/aws-iot-update-thing-shadow-lambda-function.js
Created
July 28, 2019 15:56
-
-
Save ram1505/ee0030d1d234764873a470d5eb8dc53e to your computer and use it in GitHub Desktop.
AWS Lambda function example of how to update IoT Thing Shadows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk') | |
AWS.config.region = process.env.AWS_REGION | |
const iotdata = new AWS.IotData({ | |
endpoint: process.env.MQTT_BROKER_ENDPOINT, | |
accessKeyId: process.env.ACCESS_KEY_ID, | |
secretAccessKey: process.env.SECRET_ACCESS_KEY | |
}) | |
const openState = "open" | |
const closedState = "closed" | |
let currentState = closedState | |
function toggleGarageDoor() { | |
return new Promise((resolve, reject) => { | |
let desiredState = (currentState === closedState) ? openState : closedState | |
var params = { | |
payload: `{"state":{"desired":{"door":"${desiredState}"}}}`, | |
thingName: process.env.THING_NAME | |
} | |
iotdata.updateThingShadow(params, (err, data) => { | |
if (err){ | |
console.log(err, err.stack) | |
reject(`Failed to update thing shadow: ${err.errorMessage}`) | |
}else{ | |
console.log(`update thing shadow response: ${JSON.stringify(data)}`) | |
currentState = desiredState | |
resolve({"update thing shadow response": data}) | |
} | |
}) | |
}) | |
} | |
exports.handler = async (event, context, callback) => { | |
await toggleGarageDoor() | |
.then((result) => callback(null, result)) | |
.catch((err) => callback(err)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment