IFTTT for Energia Tutorial

In this tutorial we will be posting to the cloud service If This Then That (IFTTT). IFTTT is trying to make it dead simple to connect up different web applications together. It works by letting users create recipes that let them define triggers based on behavior of other apps.  For example if you wanted to create an SMS alert whenever your stock price changes, you could do this by using their if-then logic. If this stock price goes down, then send an SMS to my number. You can do the same for alerts to your wearable devices, email, calendars, and more. Besides alerts, you can also use IFTTT to potentially automate different tasks by triggering actions in one app that is based on the behavior of another.

IFTTT recently added a channel called Maker which let’s you talk to IFTTT with HTTP POST and GET. This gives us access to trigger IFTTT events using an internet connected TI LaunchPad.

In this example we will be sending ourselves an SMS, sending ourselves an Email, and posting a tweet on twitter. You will first need to sign up for IFTTT where you will be able to register your email and phone number.

You will also need to have a Twitter account. You can link this to your IFTTT account when you create the Twitter recipe.

After you have successfully signed up for an IFTTT account, we can create our first recipe.

 

There are two ways we can use the IFTTT Maker Channel: sending data to it or receiving data from it via HTTP.  This first section will cover sending data to cause an action in another service.

Send Data

Our first recipe will involve something basic, SMS, commonly known as text messaging. IFTTT will happily send you text messages to your personal phone number as much as you like. If you need to look into texting more people, check out services like Twilio.

Go to IFTTT and create a new recipe.

createrecipe

Under If this, select the Maker Channel. If you have not used it before, you may need to connect to the channel.

this

maker

 

Now select a trigger, the only option being Receive web request. You can name the event something relevant to what you are triggering. In our case push button 1 will be pressed.

trigger

trigger2

Now select “that” and look for the SMS channel. If you have not used it before you may need to connect with the channel to enter you phone number.

that

sms

Now you can set the action, send an SMS, and say what will be in the body of the text.

trigger3

trigger4

Now you can finish up the recipe. Click to create your recipe.

recipefinish

 

For our second recipe we will go through a similar process but this time with email. Again create a new recipe. Select the maker channel, select receive a web request for your trigger and name it. Select the email channel, you may have to connect the channel if you haven’t used it before. Select send an email for your action and put in the fields of your email. Click to create your recipe.

For our third recipe we will go through the same steps but this time with Twitter. You will need a twitter account to be able to do this recipe. Again create a new recipe. Select the maker channel, select receive a web request for your trigger and name it. Select the twitter channel, you may have to connect the channel if you haven’t used it before to enter your twitter login information. Select send a tweet for your action and put in the fields of your tweet. Click to create your recipe.

That’s what we need on the cloud side for this demo, now let’s focus on the LaunchPad. Our LaunchPad will need to be connected to the internet with Wi-Fi or Ethernet. This example is using Wi-Fi.

The example code is pretty straight forward. We are first going to connect to our local Wi-Fi hotspot. Next we will wait for our push buttons to be pressed. Each button is tied to a function that will trigger our recipes. If you want to use the twitter function, remember to un-comment it out.

We will need to modify the code below with our Wi-Fi router information and our IFTTT maker channel key. You can find your key at maker.ifttt.com.

key

/* 
   IFTTTLaunchPadRecipe.ino 

   Author: Mark Easley
   This code example released to the public domain.
*/
// Include application, user and local libraries
#include "SPI.h"
#include "WiFi.h"

// Variables
char ssid[] = "<SSID>"; //  your network SSID (name)
char pass[] = "<PASSWORD>";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

int btn1; // variable to hold button 1 state
int btn2; // variable to hold button 2 state

char * server_name = "maker.ifttt.com"; // domain of the server

WiFiClient client; // new Wi-Fi client

void setup() {

    Serial.begin(115200);
    pinMode(PUSH1, INPUT_PULLUP);
    pinMode(PUSH2, INPUT_PULLUP);
    
    // attempt to connect to Wifi network:
    Serial.print("Attempting to connect to Network named: ");
    // print the network name (SSID);
    Serial.println(ssid); 
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    
    WiFi.begin(ssid, pass);
    while ( WiFi.status() != WL_CONNECTED) {
      // print dots while we wait to connect
      Serial.print(".");
      sleep(300);
      Serial.print(".");
      sleep(300);
    }

    Serial.println("\nYou're connected to the network");
    Serial.println("Waiting for an ip address");
  
    while (WiFi.localIP() == INADDR_NONE) {
      // print dots while we wait for an ip addresss
      Serial.print(".");
      sleep(300);
    }

    Serial.println("\nIP Address obtained");
  
    // you're connected now, so print out the status  
    printWifiStatus();
}

void loop() {

  btn1 = digitalRead(PUSH1); // store value of push 1
  btn2 = digitalRead(PUSH2); // store value of push 2
  Serial.print("btn1: ");
  Serial.println(btn1);
  Serial.print("btn2: ");
  Serial.println(btn2);
  
  if(btn1 == 0) {
    IFTTTemail(); // run email recipe
    //IFTTTtweet(); // run tweet recipe
  };
  if(btn2 == 0) {
    IFTTTsms(); // run sms recipe
  };

  sleep(3000); // sleep mode for 3 seconds between polling
}

//***** User defined functions *****//
//** WiFi **//
void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}
void IFTTTemail() {
  String PostData = "someDataToPost";
  
  // call HTTP POST or GET to domain
  if (client.connect(server_name, 80)) {
    // using GET you can put data values directly in the URL per the IFTTT API
    // IFTTTEVENTNAME = button_pressed or whatever is in the recipe, IFTTTKEY = unique IFTTT key
    client.print("GET /trigger/<IFTTTEVENTNAME>/with/key/<IFTTTKEY>");
    client.print("?value1=");
    client.print("extradata");
    client.print("&value2=");
    client.print("moredata");
    client.print("&value3=");
    client.print("otherrandomdata");
    client.println(" HTTP/1.1");
    client.println("Host: maker.ifttt.com");
    client.println("User-Agent: Energia/1.0");
    client.println("Connection: close");
    client.print("Content-Length: ");
    client.println(PostData.length());
    client.println(); // need this new line for HTTP to work properly!!!
    client.println(PostData);
  } else {
    Serial.print("Connection to ");
    Serial.print(server_name);
    Serial.println(" Failed");
  }
  Serial.println("Reading answer...");
  while (client.connected()) {
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
      }
    }
  Serial.println("");
}

void IFTTTsms() {
  String PostData = "someDataToPost";
  
  // call HTTP POST or GET to domain
  if (client.connect(server_name, 80)) {
    client.println("POST /trigger//with/key/ HTTP/1.1");
    client.println("Host: maker.ifttt.com");
    client.println("User-Agent: Energia/1.0");
    client.println("Connection: close");
    client.print("Content-Length: ");
    client.println(PostData.length());
    client.println();
    client.println(PostData);
  } else {
    Serial.print("Connection to ");
    Serial.print(server_name);
    Serial.println(" Failed");
  }
  Serial.println("Reading answer...");
  while (client.connected()) {
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
      }
    }
  Serial.println("");
}

void IFTTTtweet() {
  // you can setup a recipe on IFTTT to connect the maker channel 
  String twitterhandle = "@energiaproject"; // you can mention a twitter handle here for example @energiaproject
  String tweet = "I can tweet on IFTTT thanks to Energia! Learn more at http://www.energia.nu/ifttt"; // tweet body here
  String PostData = "someData";

  if (client.connect(server_name, 80)) {
    // make sure to update with your IFTTT Maker channel recipe key and event name
    // using GET you can put data values directly in the URL per the IFTTT API
    // IFTTTEVENTNAME = button_pressed or whatever is in the recipe, IFTTTKEY = unique IFTTT key
    client.print("GET /trigger//with/key/?"); // Don't forget ? mark
    client.print("value1=");
    client.print(twitterhandle);
    client.print("&");
    client.print("value2=");
    client.print(tweet);
    client.println(" HTTP/1.1");
    client.println("Host: maker.ifttt.com");
    client.println("User-Agent: Energia/1.0");
    client.println("Connection: close");
    //client.println("Content-Type: application/json");
    client.print("Content-Length: ");
    client.println(PostData.length());
    client.println();
    client.println(PostData);
  } else {
    Serial.print("Connection to ");
    Serial.print(server_name);
    Serial.println(" Failed");
  }
  Serial.println("Reading answer...");
  while (client.connected()) {
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
      }
    }
  Serial.println("");
}

 

 

Receive Data

In this section we will cover receiving data from IFTTT Maker Channel. Note: Because of how SimpleLink Wi-Fi works for the CC3200 and CC3100, it is not possible for an outside server to send a post directly to the LaunchPad via HTTP. This is because of how the Wi-Fi module connects to the local network.  So in this case we won’t be able to receive data from IFTTT using HTTP as the method at this time. This is where protocals like CoAP and MQTT can help us play a role for bi-directional communication. At this time IFTTT does not support a way to utilize alternative protocols. This means you will have to look into other cloud services to find a way to send data to the LaunchPad.