Sparkfun Thing: How to Trigger IFTTT Event and Send Extra Email Data
by MikeC180 in Circuits > Wireless
2784 Views, 11 Favorites, 0 Comments
Sparkfun Thing: How to Trigger IFTTT Event and Send Extra Email Data
I was playing with Sparkfun ESP8266 Thing and IFTTT Maker Channel. I wanted to trigger an IFTTT event and then send an email to myself with extra data .
For example, trigger Home Security Event , sending extra data , a text string "Side Gate Open" .
But .. I could not find any examples on how to do this.
IFTTT does give you this information when you sign up:
1. The content is put after the HTTP headers. The format of an HTTP POST is to have the HTTP headers, followed by a blank line, followed by the request body. The POST variables are stored as key-value pairs in the body. With an optional JSON body of: { "value1" : "01", "value2" : "02", "value3" : "03" }
2. HTTP Post example:
POST /path/script.cgi HTTP/1.0
From: frog@jmarshall.com
User-Agent: HTTPTool/1.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 32
Body
Ok great .. now what ? After some googling and experimentation .. here are the key details:
1. Need to send HTTP POST request
2. The Content-Type must be application/json
3. The data must be a text string containing key value pairs: "{ "value1" : "01", "value2" : "02", "value3" : "03" }"
For Sparkfun Thing , you use the Arduino IDE and code in C/C++. I only going describe the IFTTT trigger code.
Basic Example of Sending Extra Data
Here extra data sent is 05 , 06 , 07
const char IFTTT_POST_DATA[] = "{\"value1\":\"05\",\"value2\":\"06\",\"value3\":\"07\"}";
int post_data_size = sizeof(IFTTT_POST_DATA);
String IFTTT_POST_DATA_SIZE = String(post_data_size);
client.print(String("POST ") + IFTTT_REQUEST + " HTTP/1.1\r\n"
+ "Host: " + IFTTT_HOST + "\r\n"
+ "Connection: close\r\n"
+ "Content-Type: application/json\r\n"
+ "Content-Length: " + IFTTT_POST_DATA_SIZE + "\r\n"
+ "\r\n"
+ IFTTT_POST_DATA + "\r\n");
Real Data Example
// Extra data to send
String v1 = "Backyard";
int v2 = 1;
float v3 = 73.6;
// Convert float value to a string named sv3
char sv3[16];
dtostrf(v3,8, 2, sv3);
String df1 = "{\"value1\":";
String df2 = "\"value2\":";
String df3 = "\"value3\":";
// Create body data string
String IFTTT_POST_DATA = df1 + "\"" + v1 + "\"" + "," + df2 + "\"" + String(v2) + "\"" + "," + df3 + "\"" + sv3 + "\"" + "}" ;
// Determine body data string size
String IFTTT_POST_DATA_SIZE = String(IFTTT_POST_DATA.length());
// Send HTTP POST Request to IFTTT
client.print(String("POST ") + IFTTT_REQUEST + " HTTP/1.1\r\n"
+ "Host: " + IFTTT_HOST + "\r\n"
+ "Connection: close\r\n"
+ "Content-Type: application/json\r\n"
+ "Content-Length: " + IFTTT_POST_DATA_SIZE + "\r\n"
+ "\r\n"
+ IFTTT_POST_DATA + "\r\n");
What Email Looks Like
The event named "Home_Security" occurred on the Maker Channel
From your email name
7:08 PM (35 minutes ago)
to me
What: Home_Security When: January 5, 2016 at 07:08PM Extra Data: Backyard, 1, 73.60,