Skip to content

Instantly share code, notes, and snippets.

@ma2shita
Created May 8, 2016 11:08
Show Gist options
  • Save ma2shita/905971e4af2a0b9f73add70b75ee9fc3 to your computer and use it in GitHub Desktop.
Save ma2shita/905971e4af2a0b9f73add70b75ee9fc3 to your computer and use it in GitHub Desktop.
AWS IoT's Thing Shadow access w/ Websocket over MQTT and Cognito
<!--
AWS IoT's Thing Shadow access w/ Websocket over MQTT and Cognito
-->
<script type="text/javascript" src="https://sdk.amazonaws.com/js/aws-sdk-2.3.9.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/sha256.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/hmac-sha256.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/ma2shita/2c5a4c29242dd5382b05ce941731fd41/raw/eb4bc16c02e7a72bbd0086e937050c258da09a20/sigv4utils.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/ma2shita/63fb1dfd07753dc1ff67fe98132c0bc0/raw/ef50f3ec3276dd1c5dd90a3003e5105045b837e2/mqttws31.js"></script>
<script>
var thing_name = "patlite0";
var client; // define MQTT Client for global
var current_status; // Current status for global
// Get AWS Credentials from Cognito sample code
AWS.config.region = 'ap-northeast-1'; // Region
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'ap-northeast-1:ee820603-6830-4786-be8f-85c286c37b58',
});
// Main (Require ES2015 (using Template literal))
AWS.config.credentials.get(function(err) {
if (err) { console.warn("Get credential failed", err); return; }
var onOpen = function() {
client.subscribe(`$aws/things/${thing_name}/shadow/update/documents`);
// <Optional> Get actively current status (on startup and interval)
client.subscribe(`$aws/things/${thing_name}/shadow/get/accepted`, {onSuccess: function() {
var ping_for_get = new Paho.MQTT.Message("");
ping_for_get.destinationName = `$aws/things/${thing_name}/shadow/get`;
var active_sync = function() {
client.send(ping_for_get);
setTimeout(active_sync, 60000);
};
active_sync();
}});
};
var onMessage = function(message) {
var obj = JSON.parse(message.payloadString);
var state = ('current' in obj) ? obj.current : obj; // for normalize. NOTE: When receiving /update/documents there is a 'current' in JSON / ref: http://docs.aws.amazon.com/ja_jp/iot/latest/developerguide/thing-shadow-mqtt.html#update-documents-pub-sub-topic
console.log(message.destinationName, state);
};
var onClose = function(e) { console.log(e); };
var endpoint = SigV4Utils.getSignedUrl("wss", `data.iot.${AWS.config.region}.amazonaws.com`, "/mqtt", "iotdevicegateway", AWS.config.region, AWS.config.credentials.accessKeyId, AWS.config.credentials.secretAccessKey, AWS.config.credentials.sessionToken);
var clientId = Math.random().toString(36).substring(7);
client = new Paho.MQTT.Client(endpoint, clientId);
client.connect({useSSL: true, timeout: 3, mqttVersion: 4, onSuccess: onOpen, onFailure: onClose});
client.onMessageArrived = onMessage;
client.onConnectionLost = onClose;
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment