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/

Committer:
mbedAustin
Date:
Sun Jan 03 02:04:07 2016 +0000
Revision:
8:935972c2f628
Parent:
6:15c6c9f87c32
Updating IFTTT IP Address to reflect change in DNS Servers

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 0:4f7b5d6048b3 1 /* mbed Microcontroller Library
mbedAustin 0:4f7b5d6048b3 2 * Copyright (c) 2006-2013 ARM Limited
mbedAustin 0:4f7b5d6048b3 3 *
mbedAustin 0:4f7b5d6048b3 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbedAustin 0:4f7b5d6048b3 5 * you may not use this file except in compliance with the License.
mbedAustin 0:4f7b5d6048b3 6 * You may obtain a copy of the License at
mbedAustin 0:4f7b5d6048b3 7 *
mbedAustin 0:4f7b5d6048b3 8 * http://www.apache.org/licenses/LICENSE-2.0
mbedAustin 0:4f7b5d6048b3 9 *
mbedAustin 0:4f7b5d6048b3 10 * Unless required by applicable law or agreed to in writing, software
mbedAustin 0:4f7b5d6048b3 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbedAustin 0:4f7b5d6048b3 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbedAustin 0:4f7b5d6048b3 13 * See the License for the specific language governing permissions and
mbedAustin 0:4f7b5d6048b3 14 * limitations under the License.
mbedAustin 0:4f7b5d6048b3 15 */
mbedAustin 0:4f7b5d6048b3 16
mbedAustin 0:4f7b5d6048b3 17 #include "mbed.h"
mbedAustin 0:4f7b5d6048b3 18 #include "ifttt.h"
mbedAustin 0:4f7b5d6048b3 19 #include <string>
mbedAustin 0:4f7b5d6048b3 20
mbedAustin 4:6edd192323df 21 #if 0
mbedAustin 0:4f7b5d6048b3 22 #define DBG(x, ...) printf("[IFTTT : DBG]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
mbedAustin 0:4f7b5d6048b3 23 #define WARN(x, ...) printf("[IFTTT : WARN]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
mbedAustin 0:4f7b5d6048b3 24 #define ERR(x, ...) printf("[IFTTT : ERR]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
mbedAustin 0:4f7b5d6048b3 25 #else
mbedAustin 0:4f7b5d6048b3 26 #define DBG(x, ...) //wait_us(10);
mbedAustin 0:4f7b5d6048b3 27 #define WARN(x, ...) //wait_us(10);
mbedAustin 0:4f7b5d6048b3 28 #define ERR(x, ...)
mbedAustin 0:4f7b5d6048b3 29 #endif
mbedAustin 0:4f7b5d6048b3 30
mbedAustin 0:4f7b5d6048b3 31 #if 1
mbedAustin 5:bef11de60a9b 32 #define INFO(x, ...) printf("[IFTTT : INFO]"x" \r\n",##__VA_ARGS__);
mbedAustin 0:4f7b5d6048b3 33 #else
mbedAustin 0:4f7b5d6048b3 34 #define INFO(x, ...)
mbedAustin 0:4f7b5d6048b3 35 #endif
mbedAustin 0:4f7b5d6048b3 36
mbedAustin 0:4f7b5d6048b3 37 //
mbedAustin 0:4f7b5d6048b3 38 // Initialize object with Event, Key, and valid socket.
mbedAustin 0:4f7b5d6048b3 39 // TODO: accept hostname parameter / implement DNS lookup
mbedAustin 0:4f7b5d6048b3 40 //
mbedAustin 0:4f7b5d6048b3 41 IFTTT::IFTTT(const char * event, const char * key, TCPSocketConnection * s)
mbedAustin 0:4f7b5d6048b3 42 {
mbedAustin 0:4f7b5d6048b3 43 // Error Check
mbedAustin 0:4f7b5d6048b3 44 if(sizeof(event) > IFTTT_MAX_SIZE_EVENTNAME) {
mbedAustin 0:4f7b5d6048b3 45 ERR("Given event > IFTTT_MAX_SIZE_EVENTNAME, increase the max event string size in ifttt.h");
mbedAustin 0:4f7b5d6048b3 46 }
mbedAustin 0:4f7b5d6048b3 47 if(sizeof(key) > IFTTT_MAX_SIZE_SECRETKEY) {
mbedAustin 0:4f7b5d6048b3 48 ERR("Given key > IFTTT_MAX_SIZE_SECRETKEY, increase the max secret key string size in ifttt.h");
mbedAustin 0:4f7b5d6048b3 49 }
mbedAustin 0:4f7b5d6048b3 50 // Copy event name and secret key into object instance
mbedAustin 0:4f7b5d6048b3 51 strcpy(this->eventName,event);
mbedAustin 0:4f7b5d6048b3 52 strcpy(this->secretKey,key);
mbedAustin 0:4f7b5d6048b3 53
mbedAustin 0:4f7b5d6048b3 54 // Set up Socket
mbedAustin 0:4f7b5d6048b3 55 if(NULL == s) {
mbedAustin 0:4f7b5d6048b3 56 WARN("Given Socket Pointer is NULL, will try opening a socket.");
mbedAustin 0:4f7b5d6048b3 57 }
mbedAustin 0:4f7b5d6048b3 58 this->socket = s;
mbedAustin 0:4f7b5d6048b3 59
mbedAustin 0:4f7b5d6048b3 60 // Set up Host / Port
mbedAustin 0:4f7b5d6048b3 61 this->port = IFTTT_PORT;
mbedAustin 0:4f7b5d6048b3 62 this->host = IFTTT_IP;
mbedAustin 3:c916e13a269a 63
mbedAustin 3:c916e13a269a 64 // Initialize ingredient values to empty strings.
mbedAustin 3:c916e13a269a 65 v1 = "";
mbedAustin 3:c916e13a269a 66 v2 = "";
mbedAustin 3:c916e13a269a 67 v3 = "";
mbedAustin 0:4f7b5d6048b3 68 }
mbedAustin 0:4f7b5d6048b3 69
mbedAustin 0:4f7b5d6048b3 70 //
mbedAustin 3:c916e13a269a 71 // Add ingredients to be sent.
mbedAustin 0:4f7b5d6048b3 72 //
mbedAustin 0:4f7b5d6048b3 73 bool
mbedAustin 3:c916e13a269a 74 IFTTT::addIngredients( char * value1, char * value2, char * value3)
mbedAustin 0:4f7b5d6048b3 75 {
mbedAustin 0:4f7b5d6048b3 76 // update internal pointers. If variable not given then pass an empty string
mbedAustin 0:4f7b5d6048b3 77 v1 = (NULL == value1)?"":value1;
mbedAustin 0:4f7b5d6048b3 78 v2 = (NULL == value2)?"":value2;
mbedAustin 0:4f7b5d6048b3 79 v3 = (NULL == value3)?"":value3;
mbedAustin 3:c916e13a269a 80 return true;
mbedAustin 0:4f7b5d6048b3 81 }
mbedAustin 0:4f7b5d6048b3 82
mbedAustin 0:4f7b5d6048b3 83 //
mbedAustin 0:4f7b5d6048b3 84 // This function sends data to maker.ifttt.org via GET query commands
mbedAustin 0:4f7b5d6048b3 85 // return true on sucess, false on fail
mbedAustin 0:4f7b5d6048b3 86 //
mbedAustin 0:4f7b5d6048b3 87 bool IFTTT::get()
mbedAustin 0:4f7b5d6048b3 88 {
mbedAustin 0:4f7b5d6048b3 89 // Connect to maker.ifttt.org
mbedAustin 0:4f7b5d6048b3 90 int retry = 0;
mbedAustin 0:4f7b5d6048b3 91 for(retry=0; retry<IFTTT_MAX_RETRY; retry++) {
mbedAustin 0:4f7b5d6048b3 92 int ret = this->socket->connect(this->host, this->port);
mbedAustin 0:4f7b5d6048b3 93 if(ret == 0) {
mbedAustin 0:4f7b5d6048b3 94 DBG("Successfully Connected socket to host");
mbedAustin 0:4f7b5d6048b3 95 break ;
mbedAustin 0:4f7b5d6048b3 96 }
mbedAustin 0:4f7b5d6048b3 97 }
mbedAustin 0:4f7b5d6048b3 98 if(retry == IFTTT_MAX_RETRY) {
mbedAustin 0:4f7b5d6048b3 99 this->socket->close();
mbedAustin 0:4f7b5d6048b3 100 ERR("Could not connect socket to host\r\n");
mbedAustin 0:4f7b5d6048b3 101 return false;
mbedAustin 0:4f7b5d6048b3 102 }
mbedAustin 0:4f7b5d6048b3 103
mbedAustin 0:4f7b5d6048b3 104 // Prep data to send
mbedAustin 3:c916e13a269a 105 // TODO: verify / modify data to be query string compliant (convert spaces to '+', convert non-alpha-numberic characters to the proper encoding... etc)
mbedAustin 0:4f7b5d6048b3 106 char str[IFTTT_MAX_SIZE_STRING] = {0};
mbedAustin 0:4f7b5d6048b3 107 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);
mbedAustin 0:4f7b5d6048b3 108 DBG("String to send is:\n\r%s",str);
mbedAustin 0:4f7b5d6048b3 109
mbedAustin 0:4f7b5d6048b3 110 // Send Data
mbedAustin 0:4f7b5d6048b3 111 DBG("Sending GET data...");
mbedAustin 0:4f7b5d6048b3 112 int check = 0;
mbedAustin 0:4f7b5d6048b3 113 check = this->socket->send_all(str,sizeof(str));
mbedAustin 0:4f7b5d6048b3 114 if(check) {
mbedAustin 0:4f7b5d6048b3 115 DBG("Sent Sucessfully %d bytes",check);
mbedAustin 0:4f7b5d6048b3 116 } else {
mbedAustin 0:4f7b5d6048b3 117 ERR("Sending failed");
mbedAustin 0:4f7b5d6048b3 118 return false;
mbedAustin 0:4f7b5d6048b3 119 }
mbedAustin 0:4f7b5d6048b3 120 DBG("Waiting on reply ... \r\n");
mbedAustin 0:4f7b5d6048b3 121 int ret = this->socket->receive(str,50);
mbedAustin 0:4f7b5d6048b3 122 str[ret]=0;
mbedAustin 0:4f7b5d6048b3 123 DBG("Received String : %s",str);
mbedAustin 0:4f7b5d6048b3 124 this->socket->close();
mbedAustin 0:4f7b5d6048b3 125
mbedAustin 0:4f7b5d6048b3 126 return true;
mbedAustin 0:4f7b5d6048b3 127 }
mbedAustin 0:4f7b5d6048b3 128
mbedAustin 3:c916e13a269a 129 //
mbedAustin 3:c916e13a269a 130 // This function sends JSON encoded data encoded in a POST packet,
mbedAustin 3:c916e13a269a 131 //
mbedAustin 0:4f7b5d6048b3 132 bool IFTTT::post()
mbedAustin 0:4f7b5d6048b3 133 {
mbedAustin 0:4f7b5d6048b3 134 // Connect to maker.ifttt.org
mbedAustin 0:4f7b5d6048b3 135 int retry = 0;
mbedAustin 0:4f7b5d6048b3 136 for(retry=0; retry<IFTTT_MAX_RETRY; retry++) {
mbedAustin 0:4f7b5d6048b3 137 int ret = this->socket->connect(this->host, this->port);
mbedAustin 0:4f7b5d6048b3 138 if(ret == 0) {
mbedAustin 0:4f7b5d6048b3 139 DBG("Successfully Connected socket to host");
mbedAustin 0:4f7b5d6048b3 140 break ;
mbedAustin 0:4f7b5d6048b3 141 }
mbedAustin 0:4f7b5d6048b3 142 }
mbedAustin 0:4f7b5d6048b3 143 if(retry == IFTTT_MAX_RETRY) {
mbedAustin 0:4f7b5d6048b3 144 this->socket->close();
mbedAustin 0:4f7b5d6048b3 145 ERR("Could not connect socket to host\r\n");
mbedAustin 0:4f7b5d6048b3 146 return false;
mbedAustin 0:4f7b5d6048b3 147 }
mbedAustin 0:4f7b5d6048b3 148
mbedAustin 1:4d2664ecc1e2 149 // Prep data to send, the Assembled POST packet should look like this
mbedAustin 1:4d2664ecc1e2 150 //
mbedAustin 1:4d2664ecc1e2 151 //
mbedAustin 1:4d2664ecc1e2 152 //POST /trigger/<eventName>/with/key/<secretKey> HTTP/1.1
mbedAustin 1:4d2664ecc1e2 153 //Host: maker.ifttt.com
mbedAustin 1:4d2664ecc1e2 154 //Content-Length: <length of POST data>
mbedAustin 1:4d2664ecc1e2 155 //Content-Type: application/json
mbedAustin 1:4d2664ecc1e2 156 //
mbedAustin 1:4d2664ecc1e2 157 //{"value1":"<v1>","value2":"<v2>","value3":"<v3>"}
mbedAustin 1:4d2664ecc1e2 158 //
mbedAustin 1:4d2664ecc1e2 159 //
mbedAustin 1:4d2664ecc1e2 160 char str[IFTTT_MAX_SIZE_STRING] = {0};
mbedAustin 0:4f7b5d6048b3 161 char header[100] = {0};
mbedAustin 1:4d2664ecc1e2 162 sprintf(header, "POST /trigger/%s/with/key/%s HTTP/1.1\r\n",eventName,secretKey);
mbedAustin 0:4f7b5d6048b3 163 const char * host = "Host: maker.ifttt.com\r\n";
mbedAustin 0:4f7b5d6048b3 164 char contentLen[50] = {0};
mbedAustin 0:4f7b5d6048b3 165 const char * contentType = "Content-Type: application/json\r\n\r\n";
mbedAustin 2:b368358ab24c 166 char valueData [150] = {0};
mbedAustin 1:4d2664ecc1e2 167 sprintf(valueData,"{\"value1\":\"%s\",\"value2\":\"%s\",\"value3\":\"%s\"}\r\n",v1,v2,v3);
mbedAustin 2:b368358ab24c 168 sprintf(contentLen,"Content-Length: %d\r\n",strlen(valueData));
mbedAustin 1:4d2664ecc1e2 169 sprintf(str,"%s%s%s%s%s",header,host,contentLen,contentType,valueData);
mbedAustin 1:4d2664ecc1e2 170
mbedAustin 0:4f7b5d6048b3 171 DBG("String to send is:\n\r%s",str);
mbedAustin 0:4f7b5d6048b3 172
mbedAustin 0:4f7b5d6048b3 173 // Send Data
mbedAustin 0:4f7b5d6048b3 174 DBG("Sending POST data...");
mbedAustin 0:4f7b5d6048b3 175 int check = 0;
mbedAustin 2:b368358ab24c 176 check = this->socket->send_all(str,strlen(str));
mbedAustin 0:4f7b5d6048b3 177 if(check) {
mbedAustin 0:4f7b5d6048b3 178 DBG("Sent Sucessfully %d bytes",check);
mbedAustin 0:4f7b5d6048b3 179 } else {
mbedAustin 0:4f7b5d6048b3 180 ERR("Sending failed");
mbedAustin 0:4f7b5d6048b3 181 return false;
mbedAustin 0:4f7b5d6048b3 182 }
mbedAustin 0:4f7b5d6048b3 183 DBG("Waiting on reply ... \r\n");
mbedAustin 1:4d2664ecc1e2 184 int ret = this->socket->receive(str,IFTTT_MAX_SIZE_STRING);
mbedAustin 0:4f7b5d6048b3 185 str[ret]=0;
mbedAustin 0:4f7b5d6048b3 186 DBG("Received String : %s",str);
mbedAustin 0:4f7b5d6048b3 187 this->socket->close();
mbedAustin 0:4f7b5d6048b3 188
mbedAustin 0:4f7b5d6048b3 189 return true;
mbedAustin 0:4f7b5d6048b3 190 }
mbedAustin 3:c916e13a269a 191
mbedAustin 3:c916e13a269a 192 //
mbedAustin 3:c916e13a269a 193 // Send trigger and any values associated to maker.ifttt.com
mbedAustin 3:c916e13a269a 194 // currently unsecured (sends over HTTP, not https)
mbedAustin 3:c916e13a269a 195 //
mbedAustin 3:c916e13a269a 196 bool
mbedAustin 3:c916e13a269a 197 IFTTT::trigger(int triggerType)
mbedAustin 3:c916e13a269a 198 {
mbedAustin 3:c916e13a269a 199 int ret = 0;
mbedAustin 3:c916e13a269a 200 switch(triggerType) {
mbedAustin 3:c916e13a269a 201 case IFTTT_GET:
mbedAustin 3:c916e13a269a 202 DBG("Sending Data as GET request");
mbedAustin 3:c916e13a269a 203 ret = get();
mbedAustin 3:c916e13a269a 204 break;
mbedAustin 3:c916e13a269a 205 case IFTTT_POST:
mbedAustin 3:c916e13a269a 206 DBG("Sending Data as POST request");
mbedAustin 3:c916e13a269a 207 ret = post();
mbedAustin 3:c916e13a269a 208 break;
mbedAustin 3:c916e13a269a 209
mbedAustin 3:c916e13a269a 210 default:
mbedAustin 3:c916e13a269a 211 WARN("Invalid type, defaulting to sending data as POST request");
mbedAustin 3:c916e13a269a 212 ret = post();
mbedAustin 3:c916e13a269a 213 break;
mbedAustin 3:c916e13a269a 214 }
mbedAustin 3:c916e13a269a 215 DBG("Sending Data return code : %d",ret);
mbedAustin 5:bef11de60a9b 216 if(ret){
mbedAustin 5:bef11de60a9b 217 INFO("Successfully triggered event: '%s' with v1='%s', v2='%s', v3='%s' !",eventName,v1,v2,v3);
mbedAustin 5:bef11de60a9b 218 }
mbedAustin 3:c916e13a269a 219 return ret;
mbedAustin 3:c916e13a269a 220 }