IFTTT IP -> URL

Dependents:   IFTTT_Ethernet_LM61

Fork of IFTTT by Austin Blackstone

Committer:
mbedAustin
Date:
Fri Jul 10 21:43:50 2015 +0000
Revision:
0:4f7b5d6048b3
Child:
1:4d2664ecc1e2
Initial Commit. Get and Post methods working. No Security.

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 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 0:4f7b5d6048b3 144 // Prep data to send
mbedAustin 0:4f7b5d6048b3 145 char header[100] = {0};
mbedAustin 0:4f7b5d6048b3 146 sprintf(header, "POST /trigger/%s/with/key/%s HTTP/1.1\r\n");
mbedAustin 0:4f7b5d6048b3 147 const char * host = "Host: maker.ifttt.com\r\n";
mbedAustin 0:4f7b5d6048b3 148 char contentLen[50] = {0};
mbedAustin 0:4f7b5d6048b3 149 //sprintf(contentLen,"Content-Length: %s\r\n");
mbedAustin 0:4f7b5d6048b3 150 const char * contentType = "Content-Type: application/json\r\n\r\n";
mbedAustin 0:4f7b5d6048b3 151 char str[IFTTT_MAX_SIZE_STRING] = {0};
mbedAustin 0:4f7b5d6048b3 152 //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);
mbedAustin 0:4f7b5d6048b3 153 int thingsize = 50;
mbedAustin 0:4f7b5d6048b3 154 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);
mbedAustin 0:4f7b5d6048b3 155 DBG("String to send is:\n\r%s",str);
mbedAustin 0:4f7b5d6048b3 156
mbedAustin 0:4f7b5d6048b3 157 // Send Data
mbedAustin 0:4f7b5d6048b3 158 DBG("Sending POST data...");
mbedAustin 0:4f7b5d6048b3 159 int check = 0;
mbedAustin 0:4f7b5d6048b3 160 check = this->socket->send_all(str,sizeof(str));
mbedAustin 0:4f7b5d6048b3 161 if(check) {
mbedAustin 0:4f7b5d6048b3 162 DBG("Sent Sucessfully %d bytes",check);
mbedAustin 0:4f7b5d6048b3 163 } else {
mbedAustin 0:4f7b5d6048b3 164 ERR("Sending failed");
mbedAustin 0:4f7b5d6048b3 165 return false;
mbedAustin 0:4f7b5d6048b3 166 }
mbedAustin 0:4f7b5d6048b3 167 DBG("Waiting on reply ... \r\n");
mbedAustin 0:4f7b5d6048b3 168 int ret = this->socket->receive(str,50);
mbedAustin 0:4f7b5d6048b3 169 str[ret]=0;
mbedAustin 0:4f7b5d6048b3 170 DBG("Received String : %s",str);
mbedAustin 0:4f7b5d6048b3 171 this->socket->close();
mbedAustin 0:4f7b5d6048b3 172
mbedAustin 0:4f7b5d6048b3 173 return true;
mbedAustin 0:4f7b5d6048b3 174 }