Configuring an IOT button on AWS

IOT on AWS

In this post I'll show you how to configure an IOT button for use with your AWS account. There are limitless possibilities of what you do here but I'll show you how to use the button to trigger a Lambda function in AWS. Specifically, I'll have that Lambda function send a blast text message out to a group of subscribers to an SNS topic. This could be used for example to cancel a group meeting with a single button press. The beauty with this that you don't even need to sit down at a computer or take more than one second to simply press the button.

IOT

If you are not familiar with IOT you can read about it here. You can even buy your own button. I got mine for free from the Alexa developer program and decided I would blog about how to set it up and make it do something useful. This blog post will assume that you have basic competency with Lambda. Adding the button to you account is tantamount to adding it as a trigger to the Lambda function.

Using Lambda to Publish to SNS

Below I'll share simple Lambda code to publish to a SNS topic. I've shown you how to publish to SNS previously in Python with this post. I found a simple gist that publishes to SNS via NodeJs. You can check it out here. The code below is pretty straight forward but there are a few points to highlight. One is that your region defined in the code needs to be the same region as your topic ARN. You'll also need to check your role when creating the Lambda function. There is already a pre-defined SNS role that we will be using here. Here is how mine is set.

The actual Lambda function is:

console.log('Loading function');

var AWS = require('aws-sdk');  
AWS.config.region = 'us-west-2';

exports.handler = function(event, context) {  
    console.log("\n\nLoading handler\n\n");
    var sns = new AWS.SNS();

    sns.publish({
        Message: 'Test publish to SNS from Lambda',
        TopicArn: 'TOPIC_ARN'
    }, function(err, data) {
        if (err) {
            console.log(err.stack);
            return;
        }
        console.log('push sent');
        console.log(data);
        context.done(null, 'Function Finished!');  
    });
};

Adding a Trigger

Once your Lambda function is made you can add a trigger. Your trigger will be an IOT device button. It will be one of the options when click add a trigger in the left hand side menu. You can then configure your trigger as an IOT button. Here is what it looks like.

You'll need the device serial number from the back of your button to generate the certificate. Once you enter it AWS will generate a certificate and private key for you. You should download them because you will need them for the final wifi setup of the button. The remaining instructions are pretty simple to follow and here they are.

The only catch is once you hold down the IOT button for 5 seconds to start it's wifi broadcast you'll need to change your wifi to the buttons SSID. Once you do, navigating to http://192.168.0.1/index.html will allow to complete the setup. Here is the setup page once you change over the wifi network.

You'll need your wifi password and this is where you will upload your certificate and private key. The Endpoint information will be displayed in your AWS console and I have redacted mine here for obvious reasons. Once you agree to their terms an conditions your button will be setup and usable with your account.

Pressing the button will now publish to your SNS topic and any subscribers will get a message. A good test is to use your own email. Be a bit patient with the button because it takes about thirty seconds for the message to trigger the Lambda function and publish to the topic. You don't want to spam yourself. Not that I did that. =/

All Set

Now that the button is attached to your account and you can set a trigger to any Lambda function. You can use it as a light switch or to order a pizza.