Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: IFTTT_Ethernet_Example IFTTT_WIZwiki-W7500 IFTTT_WizFi250 StopThief ... more
ifttt.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 #include "ifttt.h" 00019 #include <string> 00020 00021 #if 0 00022 #define DBG(x, ...) printf("[IFTTT : DBG]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); 00023 #define WARN(x, ...) printf("[IFTTT : WARN]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); 00024 #define ERR(x, ...) printf("[IFTTT : ERR]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); 00025 #else 00026 #define DBG(x, ...) //wait_us(10); 00027 #define WARN(x, ...) //wait_us(10); 00028 #define ERR(x, ...) 00029 #endif 00030 00031 #if 1 00032 #define INFO(x, ...) printf("[IFTTT : INFO]"x" \r\n",##__VA_ARGS__); 00033 #else 00034 #define INFO(x, ...) 00035 #endif 00036 00037 // 00038 // Initialize object with Event, Key, and valid socket. 00039 // TODO: accept hostname parameter / implement DNS lookup 00040 // 00041 IFTTT::IFTTT(const char * event, const char * key, TCPSocketConnection * s) 00042 { 00043 // Error Check 00044 if(sizeof(event) > IFTTT_MAX_SIZE_EVENTNAME) { 00045 ERR("Given event > IFTTT_MAX_SIZE_EVENTNAME, increase the max event string size in ifttt.h"); 00046 } 00047 if(sizeof(key) > IFTTT_MAX_SIZE_SECRETKEY) { 00048 ERR("Given key > IFTTT_MAX_SIZE_SECRETKEY, increase the max secret key string size in ifttt.h"); 00049 } 00050 // Copy event name and secret key into object instance 00051 strcpy(this->eventName,event); 00052 strcpy(this->secretKey,key); 00053 00054 // Set up Socket 00055 if(NULL == s) { 00056 WARN("Given Socket Pointer is NULL, will try opening a socket."); 00057 } 00058 this->socket = s; 00059 00060 // Set up Host / Port 00061 this->port = IFTTT_PORT; 00062 this->host = IFTTT_IP; 00063 00064 // Initialize ingredient values to empty strings. 00065 v1 = ""; 00066 v2 = ""; 00067 v3 = ""; 00068 } 00069 00070 // 00071 // Add ingredients to be sent. 00072 // 00073 bool 00074 IFTTT::addIngredients( char * value1, char * value2, char * value3) 00075 { 00076 // update internal pointers. If variable not given then pass an empty string 00077 v1 = (NULL == value1)?"":value1; 00078 v2 = (NULL == value2)?"":value2; 00079 v3 = (NULL == value3)?"":value3; 00080 return true; 00081 } 00082 00083 // 00084 // This function sends data to maker.ifttt.org via GET query commands 00085 // return true on sucess, false on fail 00086 // 00087 bool IFTTT::get() 00088 { 00089 // Connect to maker.ifttt.org 00090 int retry = 0; 00091 for(retry=0; retry<IFTTT_MAX_RETRY; retry++) { 00092 int ret = this->socket->connect(this->host, this->port); 00093 if(ret == 0) { 00094 DBG("Successfully Connected socket to host"); 00095 break ; 00096 } 00097 } 00098 if(retry == IFTTT_MAX_RETRY) { 00099 this->socket->close(); 00100 ERR("Could not connect socket to host\r\n"); 00101 return false; 00102 } 00103 00104 // Prep data to send 00105 // TODO: verify / modify data to be query string compliant (convert spaces to '+', convert non-alpha-numberic characters to the proper encoding... etc) 00106 char str[IFTTT_MAX_SIZE_STRING] = {0}; 00107 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); 00108 DBG("String to send is:\n\r%s",str); 00109 00110 // Send Data 00111 DBG("Sending GET data..."); 00112 int check = 0; 00113 check = this->socket->send_all(str,sizeof(str)); 00114 if(check) { 00115 DBG("Sent Sucessfully %d bytes",check); 00116 } else { 00117 ERR("Sending failed"); 00118 return false; 00119 } 00120 DBG("Waiting on reply ... \r\n"); 00121 int ret = this->socket->receive(str,50); 00122 str[ret]=0; 00123 DBG("Received String : %s",str); 00124 this->socket->close(); 00125 00126 return true; 00127 } 00128 00129 // 00130 // This function sends JSON encoded data encoded in a POST packet, 00131 // 00132 bool IFTTT::post() 00133 { 00134 // Connect to maker.ifttt.org 00135 int retry = 0; 00136 for(retry=0; retry<IFTTT_MAX_RETRY; retry++) { 00137 int ret = this->socket->connect(this->host, this->port); 00138 if(ret == 0) { 00139 DBG("Successfully Connected socket to host"); 00140 break ; 00141 } 00142 } 00143 if(retry == IFTTT_MAX_RETRY) { 00144 this->socket->close(); 00145 ERR("Could not connect socket to host\r\n"); 00146 return false; 00147 } 00148 00149 // Prep data to send, the Assembled POST packet should look like this 00150 // 00151 // 00152 //POST /trigger/<eventName>/with/key/<secretKey> HTTP/1.1 00153 //Host: maker.ifttt.com 00154 //Content-Length: <length of POST data> 00155 //Content-Type: application/json 00156 // 00157 //{"value1":"<v1>","value2":"<v2>","value3":"<v3>"} 00158 // 00159 // 00160 char str[IFTTT_MAX_SIZE_STRING] = {0}; 00161 char header[100] = {0}; 00162 sprintf(header, "POST /trigger/%s/with/key/%s HTTP/1.1\r\n",eventName,secretKey); 00163 const char * host = "Host: maker.ifttt.com\r\n"; 00164 char contentLen[50] = {0}; 00165 const char * contentType = "Content-Type: application/json\r\n\r\n"; 00166 char valueData [150] = {0}; 00167 sprintf(valueData,"{\"value1\":\"%s\",\"value2\":\"%s\",\"value3\":\"%s\"}\r\n",v1,v2,v3); 00168 sprintf(contentLen,"Content-Length: %d\r\n",strlen(valueData)); 00169 sprintf(str,"%s%s%s%s%s",header,host,contentLen,contentType,valueData); 00170 00171 DBG("String to send is:\n\r%s",str); 00172 00173 // Send Data 00174 DBG("Sending POST data..."); 00175 int check = 0; 00176 check = this->socket->send_all(str,strlen(str)); 00177 if(check) { 00178 DBG("Sent Sucessfully %d bytes",check); 00179 } else { 00180 ERR("Sending failed"); 00181 return false; 00182 } 00183 DBG("Waiting on reply ... \r\n"); 00184 int ret = this->socket->receive(str,IFTTT_MAX_SIZE_STRING); 00185 str[ret]=0; 00186 DBG("Received String : %s",str); 00187 this->socket->close(); 00188 00189 return true; 00190 } 00191 00192 // 00193 // Send trigger and any values associated to maker.ifttt.com 00194 // currently unsecured (sends over HTTP, not https) 00195 // 00196 bool 00197 IFTTT::trigger(int triggerType) 00198 { 00199 int ret = 0; 00200 switch(triggerType) { 00201 case IFTTT_GET: 00202 DBG("Sending Data as GET request"); 00203 ret = get(); 00204 break; 00205 case IFTTT_POST: 00206 DBG("Sending Data as POST request"); 00207 ret = post(); 00208 break; 00209 00210 default: 00211 WARN("Invalid type, defaulting to sending data as POST request"); 00212 ret = post(); 00213 break; 00214 } 00215 DBG("Sending Data return code : %d",ret); 00216 if(ret){ 00217 INFO("Successfully triggered event: '%s' with v1='%s', v2='%s', v3='%s' !",eventName,v1,v2,v3); 00218 } 00219 return ret; 00220 }
Generated on Tue Jul 12 2022 19:29:50 by
1.7.2
If This Then That (IFTTT)