Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
What happens when something fails? It would be great if our connected devices were able to let us know
something was wrong but they cannot. They aren't there anymore.
To solve this problem we're going to use a feature of mqtt called LWT (Last Will and Testament).
When we create a connection to the AWS IoT Gateway we'll instruct the gateway to publish a message on our
behalf if we suddenly terminate our connection. Anything listening on this topic can then notify us. For this
example we'll post a message to a Lambda function that could send a mobile device push notification using
SNS. For the lab it won't send a push message but we'll log it anyway.
Setup a Lambda function to handle our disconnect events:
Go to the Lambda console and create a new Lambda function.
You can use the hello-world template.
Enter "iotLWT" for the lambda function name.
Keep all other defaults and roles.
Lab 6 - Failure Scenario
Step 1
You'll want your lambda code to look as follows, just a simple log right now to Cloud Watch.
console.log('Loading function');
exports.handler = function(event, context) {
console.log('Received event:', JSON.stringify(event, null, 2));
context.succeed(true);
};
Setup a new rule to handle LWT events:
Go to the AWS IoT console, click Create Resource, Create Rule.
We can call this rule thinglwt.
We'll select all actions on the lwt topic.
There will be one action to send a message to a Lambda function as shown below
Step 2
JavaScript
Let's modify our client to have a preparred message ready to send in the event of an exception:
We are adding a "will" object to our device object when the connection is created. This is passed to the
underlying mqtt client and maps to standard LWT properties.
Make the follow changes to your code:
Step 3
var awsIot = require('aws-iot-device-sdk');
var lwtMessage = {
"message": "We disconnected! Help!"
};
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",
will: {
"topic":"lwt",
"payload": JSON.stringify(lwtMessage),
"qos":0,
"retain":false
}
});
var message = {
val1: "Value 1",
val2: "Value 2",
val3: "Value 3",
message: "Test Message"
};
device.on('connect', function() {
console.log('Connected!');
setTimeout(function() {
// uncomment the next line to test your AWS IoT Rule actions.
//device.publish('lwt', JSON.stringify(message));
}, 2500);
console.log('Waiting for a crash...press CTRL-Z');
});
You'll notice here there a some lines commented out. The device.publish is there just for testing to make sure
your Lambda function and AWS IoT rule are working. The first time you do this, uncomment that line and test
run this. You should see a disconnect message. You know then Lambda etc is all connected ok. Comment the
JavaScript
line out, run this again and hit CTRL-C to terminate and watch LWT get triggered.
Now you can run
node thingtest.js
Hit CTRL+C and then review your Lambda logs. You should see your disconnect message.
Bash

More Related Content

AWS IoT 핸즈온 워크샵 - 실습 4. Device Failure 상황 처리하기 (김무현 솔루션즈 아키텍트)

  • 1. What happens when something fails? It would be great if our connected devices were able to let us know something was wrong but they cannot. They aren't there anymore. To solve this problem we're going to use a feature of mqtt called LWT (Last Will and Testament). When we create a connection to the AWS IoT Gateway we'll instruct the gateway to publish a message on our behalf if we suddenly terminate our connection. Anything listening on this topic can then notify us. For this example we'll post a message to a Lambda function that could send a mobile device push notification using SNS. For the lab it won't send a push message but we'll log it anyway. Setup a Lambda function to handle our disconnect events: Go to the Lambda console and create a new Lambda function. You can use the hello-world template. Enter "iotLWT" for the lambda function name. Keep all other defaults and roles. Lab 6 - Failure Scenario Step 1
  • 2. You'll want your lambda code to look as follows, just a simple log right now to Cloud Watch. console.log('Loading function'); exports.handler = function(event, context) { console.log('Received event:', JSON.stringify(event, null, 2)); context.succeed(true); }; Setup a new rule to handle LWT events: Go to the AWS IoT console, click Create Resource, Create Rule. We can call this rule thinglwt. We'll select all actions on the lwt topic. There will be one action to send a message to a Lambda function as shown below Step 2 JavaScript
  • 3. Let's modify our client to have a preparred message ready to send in the event of an exception: We are adding a "will" object to our device object when the connection is created. This is passed to the underlying mqtt client and maps to standard LWT properties. Make the follow changes to your code: Step 3
  • 4. var awsIot = require('aws-iot-device-sdk'); var lwtMessage = { "message": "We disconnected! Help!" }; 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", will: { "topic":"lwt", "payload": JSON.stringify(lwtMessage), "qos":0, "retain":false } }); var message = { val1: "Value 1", val2: "Value 2", val3: "Value 3", message: "Test Message" }; device.on('connect', function() { console.log('Connected!'); setTimeout(function() { // uncomment the next line to test your AWS IoT Rule actions. //device.publish('lwt', JSON.stringify(message)); }, 2500); console.log('Waiting for a crash...press CTRL-Z'); }); You'll notice here there a some lines commented out. The device.publish is there just for testing to make sure your Lambda function and AWS IoT rule are working. The first time you do this, uncomment that line and test run this. You should see a disconnect message. You know then Lambda etc is all connected ok. Comment the JavaScript
  • 5. line out, run this again and hit CTRL-C to terminate and watch LWT get triggered. Now you can run node thingtest.js Hit CTRL+C and then review your Lambda logs. You should see your disconnect message. Bash