Using Amazon IOT Buttons to Trigger IFTTT Actions

I had a lot of fun with Ted Benson’s Amazon Dash Button Hack, and setup a few of these around the house to trigger IFTTT actions (I’ll post the modified code I used at some point soon). Then, Amazon announced the IOT Button, which is basically meant to do what others have been hacking Dash Buttons to do – without the necessity of a Raspberry Pi or other device running your code.

It took me a few hours of tinkering, and I wanted to share this to save you that time if you’re a simple user like me trying to automate some tasks around the house or office.

(Canadian side note: Being a Canadian, living in Canada – I had to have a friend bring one of these up from the states. With the Dash buttons, it was a similar case – I paid someone to send them up after posting on Craigslist.)

Setup your IFTTT Maker Hooks

You’re going to need to setup your actions based on three types of presses on the Amazon IoT button: “SINGLE” (tap), “LONG” (hold down for ~2 seconds), and “DOUBLE” (classic double click).

If you haven’t already, setup your Maker Channel first, and record (or check) your “key” – you’ll need it later.

Create a new recipe with your maker channel as your trigger (“Receives a web request”) – using a prefix of your choosing, and then “SINGLE”, “LONG”, or “DOUBLE”. For instance, I chose a prefix of “iot_”, and so my first Maker trigger was “iot_SINGLE”. Make note of your prefix, as you’ll need it later too.

Setup your trigger with any of your other IFTTT channels – personally, I just had each send an email for testing.

Setup your Button

Now that you’ve got your IFTTT Maker key and prefix, the next thing you’re going to need to do is setup your button – and luckily, Amazon’s Lambda Wizard is going to help you do this pretty easily. Here’s the step-by-step directions with photos:

If you follow the link, "Step 1" should already be done – if it's not, pick "iot-button-email". The "Event Source Type" should be "AWS IoT", for "IOT Type", pick "IoT Button", and the serial number should be on the back of your device. Once that's done, click "Generate certificate and keys".

If you follow the link, “Step 1” should already be done – if it’s not, pick “iot-button-email”. The “Event Source Type” should be “AWS IoT”, for “IOT Type”, pick “IoT Button”, and the serial number should be on the back of your device. Once that’s done, click “Generate certificate and keys” (let it run for a few moments). NOTE: I did this part on Chrome, it didn’t seem to play nice with Safari.

 

Download both the certificate and private key (right click and save), and make note of the wifi password, endpoint subdomain, and endpoint region. Enable your IoT button’s pairing mode by pressing the button down for 5 seconds and you see it flashing blue – and then connect to it via WiFi. It will issue you an address via DHCP, and it’s address should be http://192.168.0.1.

 

Once you’ve connected to your IoT Button, fill out the settings you collected on the previous page (plus your own WiFi network settings), and upload your certificate and private key files. Click “Configure”, and switch back onto your WiFi network.

 

Configure your "function" (code below). Basically you can chose whatever you want for the title and description. Set the runtime to "Node.js 4.3", and for "Role" chose "Create New Role", "*Basic execution role", and follow the wizard.

Configure your “function” (code below). Basically you can chose whatever you want for the title and description. Set the runtime to “Node.js 4.3”, and for “Role” chose “Create New Role”, “*Basic execution role”, and follow the wizard.

 

"Allow" the new role, which will allow your button to execute code on press.

“Allow” the new role, which will allow your button to execute code on press. Then, on the main configuration page of Step 3, scroll to the bottom and click “Next”.

 

Finally, click "Create Function" on the review page.

Finally, click “Create Function” on the review page.

So the code is as follows (copy paste into the Lambda function editor – replace “X” with your IFTTT Maker key, and “Y” with your IFTTT hook prefix):


/**
* IFTTT Maker for Amazon IoT Button code by David Cloutier, 2016.
*/

const AWS = require('aws-sdk');
const iftttKey = 'X';
const iftttPrefix = 'Y';

/**
* IFTTT must have prefix in front of maker recipe - and then the clicktype SINGLE, LONG, or DOUBLE (e.g. prefix of 'iot_' with an event type would be an IFTTT maker name of iot_SINGLE)
*/

exports.handler = function getWebHook(event) {
var http = require('http');
var options = {
host: 'maker.ifttt.com',
path: '/trigger/'+iftttPrefix+event.clickType+'/with/key/'+iftttKey
};

var req = http.get(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));

var bodyChunks = [];
res.on('data', function(chunk) {
bodyChunks.push(chunk);
}).on('end', function() {
var body = Buffer.concat(bodyChunks);
console.log('BODY: ' + body);
})
});

req.on('error', function(e) {
console.log('ERROR: ' + e.message);
});
}

 

UPDATE: One more thing – you’re going to want to Enable the “Event Source” option. Hop into Lambda via the AWS Management Console, open your newly created Function, and switch to the “Event Sources” tab. If the “State” is “Disabled”, click it, and switch it to “Enabled”. For those of you experiencing issues, that may quickly turn a light paperweight into a fully functional IoT Button.

And that’s it! You should be able to use your IoT button to do anything that IFTTT can do.

Leave a Reply

Your email address will not be published. Required fields are marked *