If This Then That interface library. Designed to hook up to various services provided by IFTTT.

Dependents:   IFTTT_Ethernet_Example IFTTT_WIZwiki-W7500 IFTTT_WizFi250 StopThief ... more

For more information please see the IFTTT Component page : https:developer.mbed.org/components/If-This-Then-That-IFTTT/

Files at this revision

API Documentation at this revision

Comitter:
mbedAustin
Date:
Mon Jul 13 17:46:37 2015 +0000
Parent:
2:b368358ab24c
Child:
4:6edd192323df
Child:
5:bef11de60a9b
Commit message:
Added POST capability and fixed bug that would send random numbers when not initialized properly.

Changed in this revision

ifttt.cpp Show annotated file Show diff for this revision Revisions of this file
ifttt.h Show annotated file Show diff for this revision Revisions of this file
--- a/ifttt.cpp	Fri Jul 10 22:12:41 2015 +0000
+++ b/ifttt.cpp	Mon Jul 13 17:46:37 2015 +0000
@@ -60,22 +60,24 @@
     // Set up Host / Port
     this->port = IFTTT_PORT;
     this->host = IFTTT_IP;
+    
+    // Initialize ingredient values to empty strings.
+    v1 = "";
+    v2 = "";
+    v3 = "";
 }
 
 //
-// Send data to maker.ifttt.org
-// currently only uses GET requests, unsecured
+// Add ingredients to be sent.
 //
 bool
-IFTTT::sendMaker( char * value1,  char * value2,  char * value3)
+IFTTT::addIngredients( char * value1,  char * value2,  char * value3)
 {
     // update internal pointers. If variable not given then pass an empty string
     v1 = (NULL == value1)?"":value1;
     v2 = (NULL == value2)?"":value2;
     v3 = (NULL == value3)?"":value3;
-    int ret = post();
-    DBG("Sending Data returned code : %d",ret);
-    return ret;
+    return true;
 }
 
 //
@@ -100,6 +102,7 @@
     }
 
     // Prep data to send
+    // TODO: verify / modify data to be query string compliant (convert spaces to '+', convert non-alpha-numberic characters to the proper encoding... etc)
     char str[IFTTT_MAX_SIZE_STRING] = {0};
     sprintf(str, "GET /trigger/%s/with/key/%s/?value1=%s&value2=%s&value3=%s HTTP/1.1\r\nHost: maker.ifttt.com\r\n\r\n",eventName,secretKey,v1,v2,v3);
     DBG("String to send is:\n\r%s",str);
@@ -123,7 +126,9 @@
     return true;
 }
 
-//TODO: Implement
+//
+// This function sends JSON encoded data encoded in a POST packet, 
+//
 bool IFTTT::post()
 {
     // Connect to maker.ifttt.org
@@ -183,3 +188,30 @@
 
     return true;
 }
+
+//
+// Send trigger and any values associated to maker.ifttt.com
+// currently  unsecured (sends over HTTP, not https)
+//
+bool
+IFTTT::trigger(int triggerType)
+{
+    int ret = 0;
+    switch(triggerType) {
+        case IFTTT_GET:
+            DBG("Sending Data as GET request");
+            ret = get();
+            break;
+        case IFTTT_POST:
+            DBG("Sending Data as POST request");
+            ret = post();
+            break;
+
+        default:
+            WARN("Invalid type, defaulting to sending data as POST request");
+            ret = post();
+            break;
+    }
+    DBG("Sending Data return code : %d",ret);
+    return ret;
+}
--- a/ifttt.h	Fri Jul 10 22:12:41 2015 +0000
+++ b/ifttt.h	Mon Jul 13 17:46:37 2015 +0000
@@ -30,6 +30,8 @@
 #define IFTTT_IP "107.22.235.178"
 #define IFTTT_PORT 80
 
+#define IFTTT_GET 0
+#define IFTTT_POST 1
 
 
 /**
@@ -49,7 +51,7 @@
     IFTTT(const char * event, const char * key, TCPSocketConnection * s = NULL);
 
     /**
-    * Send data to maker.ifttt.com
+    * Add ingredients (values) to be sent to maker.ifttt.com
     *
     * @param v1 value 1 to send
     * @param v2 value 2 to send
@@ -57,10 +59,10 @@
     *
     * @return true if successful, false if failed
     */
-    bool sendMaker(char * v1 = NULL, char * v2 = NULL, char * v3 = NULL);
+    bool addIngredients(char * v1 = NULL, char * v2 = NULL, char * v3 = NULL);
 
     /**
-    * Send data to maker.ifttt.com
+    * Add ingredients (values) to be sent to maker.ifttt.com
     *
     * @param v1 value 1 to send
     * @param v2 value 2 to send
@@ -68,7 +70,16 @@
     *
     * @return true if successful, false if failed
     */
-    bool sendMaker(int v1 = NULL, int v2 = NULL, int v3 = NULL);
+    bool addIngredients(int v1 = NULL, int v2 = NULL, int v3 = NULL);
+
+    /**
+    * Send data via POST or GET to maker.ifttt.com
+    *
+    * @param iftttType specifies how to send the data. POST by default, GET optional.
+    *
+    * @return true if successful, false if failed
+    */
+    bool trigger(int triggerType = IFTTT_POST);
 
 private:
 
@@ -98,6 +109,7 @@
     const char * v3;
     const char * host;
     int port;
+
 };
 
 #endif // IFTTT_H