Interface with the If This Then That (IFTTT). This example uses both POST and GET to interface with IFTTT. Up to 3 values can be sent along with the trigger event name.

Dependencies:   EthernetInterface IFTTT mbed-rtos mbed

What Is This

This is an example program for using the If This Then That (IFTTT) Maker Channel Service. For more information please see https:developer.mbed.org/components/If-This-Then-That-IFTTT/

How Does It Work

There are two ways to use the channel, you can send either POST or GET requests with up to 3 values per request. Note that the values must have the names "value1", "value2", and "value3", if you call them anything else they will be ignored.

IFTTT

You will need an IFTTT account and enable the maker channel. Once you have registered for this channel you will get a channel key and will be able to register event trigger names.

GET

The GET requests pass the variables through query parameters.

?value1=some&value2=random&vlaue3=values

POST

The POST request will pass the variables through a JSON formatted form.

How To Use It

Register for an IFTTT Account, add the maker channel, add a trigger to the maker channel. Fill in the event name and the key fields in your code with the keys from the ifttt account. Create a recipe that uses the events as triggers, compile the code, load and run it. Now you should be able to trigger a recipe from your mbed device!!!

This is an example of how to use the If This Then That Maker Channel service. There are two ways to access the service, one via a GET request and the other via a POST request. You can send up to 3 variables with each interaction. These variables must be names variable1, variable2, variable 3. The GET requests will send the varaibles in the query string, so it looks like "?variable1=somevalue&variable2=somevalue&variable3=somevalue" . The POST request will send the values in a JSON formatted form request.

// Initialize ifttt object, add up to 3 optional values, trigger event.
IFTTT ifttt("YourEventName","ChangeToYourSecretKey", &socket); // EventName, Secret Key, socket to use
ifttt.addIngredients("this is awesome","test-ing","data!!!");     // 3 optional Values to send along with trigger.
ifttt.trigger();

You can find the IFTTT recipe here.

/media/uploads/mbedAustin/ifttthelloworldrecipe.png

Committer:
mbedAustin
Date:
Sun Jun 28 03:41:06 2015 +0000
Revision:
0:0f0676c43e4b
Child:
1:3010b44f07ff
Initial Commit of working GET/ POST interface with IFTT service;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 0:0f0676c43e4b 1 #include "mbed.h"
mbedAustin 0:0f0676c43e4b 2 #include "EthernetInterface.h"
mbedAustin 0:0f0676c43e4b 3 #include "HTTPClient.h"
mbedAustin 0:0f0676c43e4b 4
mbedAustin 0:0f0676c43e4b 5 EthernetInterface eth;
mbedAustin 0:0f0676c43e4b 6 HTTPClient http;
mbedAustin 0:0f0676c43e4b 7 char str[512] = {};
mbedAustin 0:0f0676c43e4b 8 char json[125] = {};
mbedAustin 0:0f0676c43e4b 9 char eventName[] = "helloworld";
mbedAustin 0:0f0676c43e4b 10 char key[] = "ChangeThisToTheKeyProvidedByIFTTonTheMakerChannel";
mbedAustin 0:0f0676c43e4b 11 char value1[] = {"A"}, value2[] = {"B"}, value3[] = {"C"};
mbedAustin 0:0f0676c43e4b 12
mbedAustin 0:0f0676c43e4b 13 int main()
mbedAustin 0:0f0676c43e4b 14 {
mbedAustin 0:0f0676c43e4b 15 eth.init(); //Use DHCP
mbedAustin 0:0f0676c43e4b 16 eth.connect();
mbedAustin 0:0f0676c43e4b 17 printf("IP Address is %s \n\r", eth.getIPAddress());
mbedAustin 0:0f0676c43e4b 18
mbedAustin 0:0f0676c43e4b 19 //GET
mbedAustin 0:0f0676c43e4b 20 printf("GETing data... \n\r");
mbedAustin 0:0f0676c43e4b 21 sprintf(str, "https://maker.ifttt.com/trigger/%s/with/key/%s?value1=%s&value2=%s&value3=%s",eventName,key,value1,value2,value3);
mbedAustin 0:0f0676c43e4b 22 printf("String is : %s\n\r",str);
mbedAustin 0:0f0676c43e4b 23 int ret = http.get(str, str, 128);
mbedAustin 0:0f0676c43e4b 24 if (!ret)
mbedAustin 0:0f0676c43e4b 25 {
mbedAustin 0:0f0676c43e4b 26 printf("Page fetched successfully - read %d characters\n\r", strlen(str));
mbedAustin 0:0f0676c43e4b 27 printf("Result: %s\n", str);
mbedAustin 0:0f0676c43e4b 28 }
mbedAustin 0:0f0676c43e4b 29 else
mbedAustin 0:0f0676c43e4b 30 {
mbedAustin 0:0f0676c43e4b 31 printf("Error - ret = %d - HTTP return code = %d \n\r", ret, http.getHTTPResponseCode());
mbedAustin 0:0f0676c43e4b 32 }
mbedAustin 0:0f0676c43e4b 33
mbedAustin 0:0f0676c43e4b 34 //POST
mbedAustin 0:0f0676c43e4b 35 HTTPMap map;
mbedAustin 0:0f0676c43e4b 36 HTTPText inText(str, 512);
mbedAustin 0:0f0676c43e4b 37 map.put("value1", value1);
mbedAustin 0:0f0676c43e4b 38 map.put("value2", value2);
mbedAustin 0:0f0676c43e4b 39 map.put("value3", value3);
mbedAustin 0:0f0676c43e4b 40 sprintf(str, "https://maker.ifttt.com/trigger/%s/with/key/%s",eventName,key);
mbedAustin 0:0f0676c43e4b 41 printf("String is : %s\n\r",str);
mbedAustin 0:0f0676c43e4b 42 printf("POSTing data ....\n\r");
mbedAustin 0:0f0676c43e4b 43 ret = http.post(str, map, &inText);
mbedAustin 0:0f0676c43e4b 44 if (!ret)
mbedAustin 0:0f0676c43e4b 45 {
mbedAustin 0:0f0676c43e4b 46 printf("Executed POST successfully - read %d characters \n\r", strlen(str));
mbedAustin 0:0f0676c43e4b 47 printf("Result: %s \n\r", str);
mbedAustin 0:0f0676c43e4b 48 }
mbedAustin 0:0f0676c43e4b 49 else
mbedAustin 0:0f0676c43e4b 50 {
mbedAustin 0:0f0676c43e4b 51 printf("Error - ret = %d - HTTP return code = %d \n\r", ret, http.getHTTPResponseCode());
mbedAustin 0:0f0676c43e4b 52 }
mbedAustin 0:0f0676c43e4b 53
mbedAustin 0:0f0676c43e4b 54
mbedAustin 0:0f0676c43e4b 55
mbedAustin 0:0f0676c43e4b 56 eth.disconnect();
mbedAustin 0:0f0676c43e4b 57
mbedAustin 0:0f0676c43e4b 58 while(1) {
mbedAustin 0:0f0676c43e4b 59 }
mbedAustin 0:0f0676c43e4b 60 }