Monday, June 29, 2015

Connecting the IoT and Node.js to IFTTT

IFTTT has huge potential in its ability to connect devices quickly and easily. There was one thing it had been missing for a while - the ability to send and receive generic HTTP GET and POST requests. If you wanted to use IFTTT for something, the specific use for it had to be defined by IFTTT and published on their site within a channel. That is, until now!

IFTTT recently released the Maker Channel. It is exactly what developers have been waiting for! It allows you to define triggers that are set off when they receive a HTTP request, along with actions that can make a HTTP request to a defined location. This opens up IFTTT to be used for virtually anything. It is now completely up to the imagination of the developer community.

To show what the Maker channel is capable of, we are going to set up a simple Arduino to communicate back and forth with IFTTT via Node.js. To experiment with sending triggers to IFTTT, we will toggle a LIFX lightbulb on and off via an Arduino powered light switch. To try out an IFTTT Maker action, we will connect an RGB LED to our Arduino which will change color any time we are mentioned on Twitter. Don't like Twitter or don't have a LIFX bulb? Not a problem at all, switch out the action/trigger with something else on IFTTT. Work with the Facebook channel instead of Twitter or trigger actions on your phone instead of a lightbulb. This is a lot of fun to tinker with.

If you are new to IFTTT, I previously covered the basics in my article on Connecting LIFX Light Bulbs to the IoT Using IFTTT. This article assumes you know all about triggers and actions, and now are ready to take it to the next level!

The code

If you're keen to get straight into the code and try it out, you can find it here on GitHub.

How This Will Work

We are going to have a local Node.js server running with an Arduino connected via USB. Pressing a button on the Arduino set up will trigger a HTTP request to IFTTT to tell it to toggle our LIFX light. Our Node.js server will also have Express running to handle any incoming HTTP requests. Whenever IFTTT sees a new mention of us on Twitter, it'll make a POST request to our server to trigger our Arduino's LED.

Our Arduino Sketch

We'll be connecting up a simple button and an RGB LED to an Arduino.

Button and RGB LED Sketch

Setting Up The Maker Channel

To get started, we will need to go to the Maker Channel on IFTTT and click "Connect". Once it is set up, you'll reach a screen that looks like so:

Finding your Maker key

On this screen, it provides your secret key you'll need to trigger IFTTT actions via HTTP commands. Of course, you'll need a bit more info than just the key, we need a URL to POST to that'll trigger the action. To find this, click the link that says "How to Trigger Events". It'll open up a page that contains the URL you'll want to use, conveniently with your key attached to it.

Triggering maker events

The URL we'll be using for our LIFX bulb looks like this: http://ift.tt/1GIlwv9}}. The URL part which says light_switch is our event name. We use this when putting together our IFTTT recipe. If you're not triggering a LIFX bulb, choose a name that matches your event more accurately.

Toggling IFTTT Using Maker Triggers

Our Maker channel is active and ready to be used. We will begin by looking at how to toggle our IFTTT action - toggling a LIFX lightbulb on and off using a button from an Arduino.

Making the recipe itself is quite simple:

  1. We go to the Create New Recipe page on IFTTT.
  2. Select "Maker" as our Trigger Channel.
  3. Click on "Receive a web request" as our trigger type.
  4. Enter our event name of light_switch.
  5. Choose "LIFX" as our Action Channel.
  6. Click on "Toggle lights on/off" as our action type.
  7. Choose the LIFX lights we'd like this to affect.
  8. Give the recipe a title and click "Create Recipe"!

In our Node.js code, we use johnny-five to access our Arduino. When the board is ready, we define our button connected to pin 7 in the btn variable:

[code language="js"]
board.on('ready', function() {
console.log('Board ready');

btn = new five.Button(7);
[/code]

Continue reading %Connecting the IoT and Node.js to IFTTT%


by Patrick Catanzariti via SitePoint

No comments:

Post a Comment