IoT, AWS, and the Amazon Dash Button

The internet of things or IoT in is simple way is the ability to connect any to device to internet. There are many uses for this but the most common one is to report state of the device.

With this post I want to show you how easy is to get started with IoT and start thinking on how IoT improve your business.

Amazon sells a IoT button https://aws.amazon.com/iotbutton/ . This button allows you after configure to sell an email to yourself. AWS offers a quick start wizard that takes cares of the creation registration and setup of the button. This is great if you want to get going in 15 minutes without understanding what is going on under the hood http://docs.aws.amazon.com/iot/latest/developerguide/iot-button-lambda.html.

This is what the button looks like

images

In this post I will show the code in Python 3, the current quick start is written in Node.js and I wanted to write the code in different language. I also will show you how to keep track of how many clicks are left, how to send an email using SNS topics, what IAM permissions are needed and at the how to kill all instances on the region.

One of the drawbacks of the button is that it only has battery for  2000 clicks. The device cost around $20 bucks.  DISCLAIMER YOU SHOULD THIS ON A LAB AWS ACCOUNT AS THE CODE TERMINATE INSTANCES IN YOUR ACCOUNT

REQUIREMENTS TO DO THIS LAB
AWS account
AWS IoT button
Some basic knowledge about IAM polices, Dynamo and Lambda

  1. We need to create a lambda function and choose author from scratch. Select a name, and for role choose create role from template. Select the AWS IoT Button permissions.
    2017-10-29_22-30-41.png
  2. Click on create function. Make the following changes, runtime Python3.6 an handler iot_blog.lambda_handler. Before saving copy this piece of code into the function.
    def lambda_handler(event, context):
           print(‘1’)

    2017-10-29_22-41-06.png

  3. Under Environment type “email_address” for your key and then your email address in value
    2017-10-30_00-01-22
  4. Now lets click on triggers to create the IoT trigger. Click on Add Trigger, and choose AWS IoT.
    2017-10-29_22-44-40.png
  5. This will allow us to add our button and configure your button. This will register your button on the IoT hub in AWS and create the certificates to upload to your button.
    Your button has a DSN (device serial number)  on the back that needs to be enter. After that click on generate certificate keys.
    2017-10-29_23-01-39
  6. After you download the certificates hold the button for 5 seconds until is blinking blue. After that should you see a SSID called “Button ConfigureMe – 2DA”
    2017-10-29_23-06-46.png
  7. After you connect in your browser open new tab and go to the following link
    http://192.168.0.1/index.html
    Get the following information from the page the subdomain, region and the password.
    2017-10-29_23-10-52.png
    Click save and if you button blink green without red is ready to be configured
  8. Go back to the AWS console, check on Enable trigger and submit. Now that you have created the trigger lets add additional policies to the role on IAM.
    Add the following policies
    AmazonEC2FullAccess
    AWSXrayWriteOnlyAcccess
    AmazonDynamoDBFullAccess
    2017-10-29_23-20-13.png
  9. Now that we have all setup, lets go back to the lambda and copy the the following code into your lambda.

Should look like this
2017-10-29_23-55-10.png

This is what the code above does

  1. Create a SNS topic in AWS
    2017-10-29_23-57-01.png
  2. Create a subscription based on the email address on the lambda environment file
    2017-10-29_23-59-33.png
  3. Creates a dynamo table to keep track of the number of clicks. The name of the table will match your device DSN
    2017-10-30_00-03-30

    In the table I am keeping the unix time each a person clicks on the button.
    2017-10-30_00-17-59

    4. Will delete any instances on the account. If you dont want for this to execute change line 159 to

    delete_instances = ‘no’

  1. Will send an email with the information collected that will look like this when you press the button on your IoT device

2017-10-30_00-14-23.png

 

In summary this show you the potential of IoT and how is changing our lives. But more importantly that is within everybody’s reach and it is not rocket science

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s