IFTTT IP -> URL
Dependents: IFTTT_Ethernet_LM61
Fork of IFTTT by
ifttt.cpp@2:b368358ab24c, 2015-07-10 (annotated)
- Committer:
- mbedAustin
- Date:
- Fri Jul 10 22:12:41 2015 +0000
- Revision:
- 2:b368358ab24c
- Parent:
- 1:4d2664ecc1e2
- Child:
- 3:c916e13a269a
Updated Post method to handle dynamic sizing of values.
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:4f7b5d6048b3 | 21 | #if 1 |
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 | 0:4f7b5d6048b3 | 32 | #define INFO(x, ...) printf("[IFTTT : INFO]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); |
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 | 0:4f7b5d6048b3 | 63 | } |
mbedAustin | 0:4f7b5d6048b3 | 64 | |
mbedAustin | 0:4f7b5d6048b3 | 65 | // |
mbedAustin | 0:4f7b5d6048b3 | 66 | // Send data to maker.ifttt.org |
mbedAustin | 0:4f7b5d6048b3 | 67 | // currently only uses GET requests, unsecured |
mbedAustin | 0:4f7b5d6048b3 | 68 | // |
mbedAustin | 0:4f7b5d6048b3 | 69 | bool |
mbedAustin | 0:4f7b5d6048b3 | 70 | IFTTT::sendMaker( char * value1, char * value2, char * value3) |
mbedAustin | 0:4f7b5d6048b3 | 71 | { |
mbedAustin | 0:4f7b5d6048b3 | 72 | // update internal pointers. If variable not given then pass an empty string |
mbedAustin | 0:4f7b5d6048b3 | 73 | v1 = (NULL == value1)?"":value1; |
mbedAustin | 0:4f7b5d6048b3 | 74 | v2 = (NULL == value2)?"":value2; |
mbedAustin | 0:4f7b5d6048b3 | 75 | v3 = (NULL == value3)?"":value3; |
mbedAustin | 0:4f7b5d6048b3 | 76 | int ret = post(); |
mbedAustin | 0:4f7b5d6048b3 | 77 | DBG("Sending Data returned code : %d",ret); |
mbedAustin | 0:4f7b5d6048b3 | 78 | return ret; |
mbedAustin | 0:4f7b5d6048b3 | 79 | } |
mbedAustin | 0:4f7b5d6048b3 | 80 | |
mbedAustin | 0:4f7b5d6048b3 | 81 | // |
mbedAustin | 0:4f7b5d6048b3 | 82 | // This function sends data to maker.ifttt.org via GET query commands |
mbedAustin | 0:4f7b5d6048b3 | 83 | // return true on sucess, false on fail |
mbedAustin | 0:4f7b5d6048b3 | 84 | // |
mbedAustin | 0:4f7b5d6048b3 | 85 | bool IFTTT::get() |
mbedAustin | 0:4f7b5d6048b3 | 86 | { |
mbedAustin | 0:4f7b5d6048b3 | 87 | // Connect to maker.ifttt.org |
mbedAustin | 0:4f7b5d6048b3 | 88 | int retry = 0; |
mbedAustin | 0:4f7b5d6048b3 | 89 | for(retry=0; retry<IFTTT_MAX_RETRY; retry++) { |
mbedAustin | 0:4f7b5d6048b3 | 90 | int ret = this->socket->connect(this->host, this->port); |
mbedAustin | 0:4f7b5d6048b3 | 91 | if(ret == 0) { |
mbedAustin | 0:4f7b5d6048b3 | 92 | DBG("Successfully Connected socket to host"); |
mbedAustin | 0:4f7b5d6048b3 | 93 | break ; |
mbedAustin | 0:4f7b5d6048b3 | 94 | } |
mbedAustin | 0:4f7b5d6048b3 | 95 | } |
mbedAustin | 0:4f7b5d6048b3 | 96 | if(retry == IFTTT_MAX_RETRY) { |
mbedAustin | 0:4f7b5d6048b3 | 97 | this->socket->close(); |
mbedAustin | 0:4f7b5d6048b3 | 98 | ERR("Could not connect socket to host\r\n"); |
mbedAustin | 0:4f7b5d6048b3 | 99 | return false; |
mbedAustin | 0:4f7b5d6048b3 | 100 | } |
mbedAustin | 0:4f7b5d6048b3 | 101 | |
mbedAustin | 0:4f7b5d6048b3 | 102 | // Prep data to send |
mbedAustin | 0:4f7b5d6048b3 | 103 | char str[IFTTT_MAX_SIZE_STRING] = {0}; |
mbedAustin | 0:4f7b5d6048b3 | 104 | 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 | 105 | DBG("String to send is:\n\r%s",str); |
mbedAustin | 0:4f7b5d6048b3 | 106 | |
mbedAustin | 0:4f7b5d6048b3 | 107 | // Send Data |
mbedAustin | 0:4f7b5d6048b3 | 108 | DBG("Sending GET data..."); |
mbedAustin | 0:4f7b5d6048b3 | 109 | int check = 0; |
mbedAustin | 0:4f7b5d6048b3 | 110 | check = this->socket->send_all(str,sizeof(str)); |
mbedAustin | 0:4f7b5d6048b3 | 111 | if(check) { |
mbedAustin | 0:4f7b5d6048b3 | 112 | DBG("Sent Sucessfully %d bytes",check); |
mbedAustin | 0:4f7b5d6048b3 | 113 | } else { |
mbedAustin | 0:4f7b5d6048b3 | 114 | ERR("Sending failed"); |
mbedAustin | 0:4f7b5d6048b3 | 115 | return false; |
mbedAustin | 0:4f7b5d6048b3 | 116 | } |
mbedAustin | 0:4f7b5d6048b3 | 117 | DBG("Waiting on reply ... \r\n"); |
mbedAustin | 0:4f7b5d6048b3 | 118 | int ret = this->socket->receive(str,50); |
mbedAustin | 0:4f7b5d6048b3 | 119 | str[ret]=0; |
mbedAustin | 0:4f7b5d6048b3 | 120 | DBG("Received String : %s",str); |
mbedAustin | 0:4f7b5d6048b3 | 121 | this->socket->close(); |
mbedAustin | 0:4f7b5d6048b3 | 122 | |
mbedAustin | 0:4f7b5d6048b3 | 123 | return true; |
mbedAustin | 0:4f7b5d6048b3 | 124 | } |
mbedAustin | 0:4f7b5d6048b3 | 125 | |
mbedAustin | 0:4f7b5d6048b3 | 126 | //TODO: Implement |
mbedAustin | 0:4f7b5d6048b3 | 127 | bool IFTTT::post() |
mbedAustin | 0:4f7b5d6048b3 | 128 | { |
mbedAustin | 0:4f7b5d6048b3 | 129 | // Connect to maker.ifttt.org |
mbedAustin | 0:4f7b5d6048b3 | 130 | int retry = 0; |
mbedAustin | 0:4f7b5d6048b3 | 131 | for(retry=0; retry<IFTTT_MAX_RETRY; retry++) { |
mbedAustin | 0:4f7b5d6048b3 | 132 | int ret = this->socket->connect(this->host, this->port); |
mbedAustin | 0:4f7b5d6048b3 | 133 | if(ret == 0) { |
mbedAustin | 0:4f7b5d6048b3 | 134 | DBG("Successfully Connected socket to host"); |
mbedAustin | 0:4f7b5d6048b3 | 135 | break ; |
mbedAustin | 0:4f7b5d6048b3 | 136 | } |
mbedAustin | 0:4f7b5d6048b3 | 137 | } |
mbedAustin | 0:4f7b5d6048b3 | 138 | if(retry == IFTTT_MAX_RETRY) { |
mbedAustin | 0:4f7b5d6048b3 | 139 | this->socket->close(); |
mbedAustin | 0:4f7b5d6048b3 | 140 | ERR("Could not connect socket to host\r\n"); |
mbedAustin | 0:4f7b5d6048b3 | 141 | return false; |
mbedAustin | 0:4f7b5d6048b3 | 142 | } |
mbedAustin | 0:4f7b5d6048b3 | 143 | |
mbedAustin | 1:4d2664ecc1e2 | 144 | // Prep data to send, the Assembled POST packet should look like this |
mbedAustin | 1:4d2664ecc1e2 | 145 | // |
mbedAustin | 1:4d2664ecc1e2 | 146 | // |
mbedAustin | 1:4d2664ecc1e2 | 147 | //POST /trigger/<eventName>/with/key/<secretKey> HTTP/1.1 |
mbedAustin | 1:4d2664ecc1e2 | 148 | //Host: maker.ifttt.com |
mbedAustin | 1:4d2664ecc1e2 | 149 | //Content-Length: <length of POST data> |
mbedAustin | 1:4d2664ecc1e2 | 150 | //Content-Type: application/json |
mbedAustin | 1:4d2664ecc1e2 | 151 | // |
mbedAustin | 1:4d2664ecc1e2 | 152 | //{"value1":"<v1>","value2":"<v2>","value3":"<v3>"} |
mbedAustin | 1:4d2664ecc1e2 | 153 | // |
mbedAustin | 1:4d2664ecc1e2 | 154 | // |
mbedAustin | 1:4d2664ecc1e2 | 155 | char str[IFTTT_MAX_SIZE_STRING] = {0}; |
mbedAustin | 0:4f7b5d6048b3 | 156 | char header[100] = {0}; |
mbedAustin | 1:4d2664ecc1e2 | 157 | sprintf(header, "POST /trigger/%s/with/key/%s HTTP/1.1\r\n",eventName,secretKey); |
mbedAustin | 0:4f7b5d6048b3 | 158 | const char * host = "Host: maker.ifttt.com\r\n"; |
mbedAustin | 0:4f7b5d6048b3 | 159 | char contentLen[50] = {0}; |
mbedAustin | 0:4f7b5d6048b3 | 160 | const char * contentType = "Content-Type: application/json\r\n\r\n"; |
mbedAustin | 2:b368358ab24c | 161 | char valueData [150] = {0}; |
mbedAustin | 1:4d2664ecc1e2 | 162 | sprintf(valueData,"{\"value1\":\"%s\",\"value2\":\"%s\",\"value3\":\"%s\"}\r\n",v1,v2,v3); |
mbedAustin | 2:b368358ab24c | 163 | sprintf(contentLen,"Content-Length: %d\r\n",strlen(valueData)); |
mbedAustin | 1:4d2664ecc1e2 | 164 | sprintf(str,"%s%s%s%s%s",header,host,contentLen,contentType,valueData); |
mbedAustin | 1:4d2664ecc1e2 | 165 | |
mbedAustin | 0:4f7b5d6048b3 | 166 | DBG("String to send is:\n\r%s",str); |
mbedAustin | 0:4f7b5d6048b3 | 167 | |
mbedAustin | 0:4f7b5d6048b3 | 168 | // Send Data |
mbedAustin | 0:4f7b5d6048b3 | 169 | DBG("Sending POST data..."); |
mbedAustin | 0:4f7b5d6048b3 | 170 | int check = 0; |
mbedAustin | 2:b368358ab24c | 171 | check = this->socket->send_all(str,strlen(str)); |
mbedAustin | 0:4f7b5d6048b3 | 172 | if(check) { |
mbedAustin | 0:4f7b5d6048b3 | 173 | DBG("Sent Sucessfully %d bytes",check); |
mbedAustin | 0:4f7b5d6048b3 | 174 | } else { |
mbedAustin | 0:4f7b5d6048b3 | 175 | ERR("Sending failed"); |
mbedAustin | 0:4f7b5d6048b3 | 176 | return false; |
mbedAustin | 0:4f7b5d6048b3 | 177 | } |
mbedAustin | 0:4f7b5d6048b3 | 178 | DBG("Waiting on reply ... \r\n"); |
mbedAustin | 1:4d2664ecc1e2 | 179 | int ret = this->socket->receive(str,IFTTT_MAX_SIZE_STRING); |
mbedAustin | 0:4f7b5d6048b3 | 180 | str[ret]=0; |
mbedAustin | 0:4f7b5d6048b3 | 181 | DBG("Received String : %s",str); |
mbedAustin | 0:4f7b5d6048b3 | 182 | this->socket->close(); |
mbedAustin | 0:4f7b5d6048b3 | 183 | |
mbedAustin | 0:4f7b5d6048b3 | 184 | return true; |
mbedAustin | 0:4f7b5d6048b3 | 185 | } |