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:
Fri Jul 10 21:43:50 2015 +0000
Child:
1:4d2664ecc1e2
Commit message:
Initial Commit. Get and Post methods working. No Security.

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ifttt.cpp	Fri Jul 10 21:43:50 2015 +0000
@@ -0,0 +1,174 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "mbed.h"
+#include "ifttt.h"
+#include <string>
+
+#if 1
+#define DBG(x, ...)  printf("[IFTTT : DBG]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
+#define WARN(x, ...) printf("[IFTTT : WARN]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
+#define ERR(x, ...)  printf("[IFTTT : ERR]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
+#else
+#define DBG(x, ...) //wait_us(10);
+#define WARN(x, ...) //wait_us(10);
+#define ERR(x, ...)
+#endif
+
+#if 1
+#define INFO(x, ...) printf("[IFTTT : INFO]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
+#else
+#define INFO(x, ...)
+#endif
+
+//
+// Initialize object with Event, Key, and valid socket.
+// TODO: accept hostname parameter / implement DNS lookup
+//
+IFTTT::IFTTT(const char * event, const char * key, TCPSocketConnection * s)
+{
+    // Error Check
+    if(sizeof(event) > IFTTT_MAX_SIZE_EVENTNAME) {
+        ERR("Given event > IFTTT_MAX_SIZE_EVENTNAME, increase the max event string size in ifttt.h");
+    }
+    if(sizeof(key) > IFTTT_MAX_SIZE_SECRETKEY) {
+        ERR("Given key > IFTTT_MAX_SIZE_SECRETKEY, increase the max secret key string size in ifttt.h");
+    }
+    // Copy event name and secret key into object instance
+    strcpy(this->eventName,event);
+    strcpy(this->secretKey,key);
+
+    // Set up Socket
+    if(NULL == s) {
+        WARN("Given Socket Pointer is NULL, will try opening a socket.");
+    }
+    this->socket = s;
+
+    // Set up Host / Port
+    this->port = IFTTT_PORT;
+    this->host = IFTTT_IP;
+}
+
+//
+// Send data to maker.ifttt.org
+// currently only uses GET requests, unsecured
+//
+bool
+IFTTT::sendMaker( 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;
+}
+
+//
+// This function sends data to maker.ifttt.org via GET query commands
+// return true on sucess, false on fail
+//
+bool IFTTT::get()
+{
+    // Connect to maker.ifttt.org
+    int retry = 0;
+    for(retry=0; retry<IFTTT_MAX_RETRY; retry++) {
+        int ret = this->socket->connect(this->host, this->port);
+        if(ret == 0) {
+            DBG("Successfully Connected socket to host");
+            break ;
+        }
+    }
+    if(retry == IFTTT_MAX_RETRY) {
+        this->socket->close();
+        ERR("Could not connect socket to host\r\n");
+        return false;
+    }
+
+    // Prep data to send
+    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);
+
+    // Send Data
+    DBG("Sending GET data...");
+    int check = 0;
+    check = this->socket->send_all(str,sizeof(str));
+    if(check) {
+        DBG("Sent Sucessfully %d bytes",check);
+    } else {
+        ERR("Sending failed");
+        return false;
+    }
+    DBG("Waiting on reply ... \r\n");
+    int ret = this->socket->receive(str,50);
+    str[ret]=0;
+    DBG("Received String : %s",str);
+    this->socket->close();
+
+    return true;
+}
+
+//TODO: Implement
+bool IFTTT::post()
+{
+    // Connect to maker.ifttt.org
+    int retry = 0;
+    for(retry=0; retry<IFTTT_MAX_RETRY; retry++) {
+        int ret = this->socket->connect(this->host, this->port);
+        if(ret == 0) {
+            DBG("Successfully Connected socket to host");
+            break ;
+        }
+    }
+    if(retry == IFTTT_MAX_RETRY) {
+        this->socket->close();
+        ERR("Could not connect socket to host\r\n");
+        return false;
+    }
+
+    // Prep data to send
+    char header[100] = {0};
+    sprintf(header, "POST /trigger/%s/with/key/%s HTTP/1.1\r\n");
+    const char * host = "Host: maker.ifttt.com\r\n";
+    char contentLen[50] = {0};
+    //sprintf(contentLen,"Content-Length: %s\r\n");
+    const char * contentType = "Content-Type: application/json\r\n\r\n";
+    char str[IFTTT_MAX_SIZE_STRING] = {0};
+    //sprintf(str, "POST /trigger/%s/with/key/%s HTTP/1.1\r\nHost: maker.ifttt.com\r\nContent-Length: 50\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n{\"value1\":\"%s\",\"value2\":\"%s\",\"value3\":\"%s\"}\r\n\r\n",eventName,secretKey,v1,v2,v3);
+    int thingsize = 50;
+    sprintf(str, "POST /trigger/%s/with/key/%s HTTP/1.1\r\nHost: maker.ifttt.com\r\nContent-Length: %d\r\nContent-Type: application/json\r\n\r\n{\"value1\":\"%s\",\"value2\":\"%s\",\"value3\":\"data\"}\r\n",eventName,secretKey,thingsize,v1,v2,v3);
+    DBG("String to send is:\n\r%s",str);
+
+    // Send Data
+    DBG("Sending POST data...");
+    int check = 0;
+    check = this->socket->send_all(str,sizeof(str));
+    if(check) {
+        DBG("Sent Sucessfully %d bytes",check);
+    } else {
+        ERR("Sending failed");
+        return false;
+    }
+    DBG("Waiting on reply ... \r\n");
+    int ret = this->socket->receive(str,50);
+    str[ret]=0;
+    DBG("Received String : %s",str);
+    this->socket->close();
+
+    return true;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ifttt.h	Fri Jul 10 21:43:50 2015 +0000
@@ -0,0 +1,103 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef IFTTT_H
+#define IFTTT_H
+
+#include "mbed.h"
+#include "TCPSocketConnection.h"
+
+#define IFTTT_FAIL -1
+#define IFTTT_SUCCESS 0
+
+#define IFTTT_MAX_RETRY 5
+#define IFTTT_MAX_SIZE_SECRETKEY 50
+#define IFTTT_MAX_SIZE_EVENTNAME 50
+#define IFTTT_MAX_SIZE_STRING 512
+
+#define IFTTT_IP "107.22.235.178"
+#define IFTTT_PORT 80
+
+
+
+/**
+* The IFTTT class (if this then that)
+*/
+class IFTTT
+{
+
+public:
+
+    /**
+    * Constructor, initialize the Event Name and Secret Key to be used
+    *
+    * @param event event name of trigger
+    * @param key secret key provided by the maker channel for your event
+    */
+    IFTTT(const char * event, const char * key, TCPSocketConnection * s = NULL);
+
+    /**
+    * Send data to maker.ifttt.com
+    *
+    * @param v1 value 1 to send
+    * @param v2 value 2 to send
+    * @param v3 value 3 to send
+    *
+    * @return true if successful, false if failed
+    */
+    bool sendMaker(char * v1 = NULL, char * v2 = NULL, char * v3 = NULL);
+
+    /**
+    * Send data to maker.ifttt.com
+    *
+    * @param v1 value 1 to send
+    * @param v2 value 2 to send
+    * @param v3 value 3 to send
+    *
+    * @return true if successful, false if failed
+    */
+    bool sendMaker(int v1 = NULL, int v2 = NULL, int v3 = NULL);
+
+private:
+
+    /**
+    * Send data to maker.ifttt.com via GET Query parameters
+    *
+    * @param name explanation
+    *
+    * @return true if successful, false if failed
+    */
+    bool get();
+
+    /**
+    * Send data to maker.ifttt.com via POST payload, encoded as JSON
+    *
+    * @param name explanation
+    *
+    * @return true if successful, false if failed
+    */
+    bool post();
+
+    TCPSocketConnection * socket;
+    char eventName[IFTTT_MAX_SIZE_EVENTNAME];
+    char secretKey[IFTTT_MAX_SIZE_SECRETKEY];
+    const char * v1;
+    const char * v2;
+    const char * v3;
+    const char * host;
+    int port;
+};
+
+#endif // IFTTT_H