Skip to content

Instantly share code, notes, and snippets.

@outofmbufs
Last active May 21, 2021 21:44
Show Gist options
  • Save outofmbufs/d6ced37b49a484c495f0 to your computer and use it in GitHub Desktop.
Save outofmbufs/d6ced37b49a484c495f0 to your computer and use it in GitHub Desktop.
Arduino code to trigger an IFTTT/Maker event
// The MIT License
//
// Copyright (c) 2015 Neil Webber
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include <SPI.h>
#include <Ethernet.h>
#include <EEPROM.h>
//
// Ethernet MAC address to use.
// Usually Arduino Ethernet shields come with an address printed on
// them (sometimes on underside of the board). Use that one here.
//
byte mac[] = { 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 }; // REPLACE WITH REAL ADDR
//
// IFTTT Maker parameters:
// Key -- Obtained when setting up/connecting the Maker channel in IFTTT
// Event -- Arbitrary name for the event; used in the IFTTT recipe.
//
char MakerIFTTT_Key[] = "r8X-blahblahYourKeyGoesHere";
char MakerIFTTT_Event[] = "arduino_example";
//
// The IFTTT/Maker channel allows for three values to be reported
// which can be used as "ingredients" in the IFTTT recipe.
//
// In this simple example the values come from:
// reading a PIN (reported as value1)
// reporting uptime in milliseconds (reported as value2)
// the string "hello, world!" (reported as value3)
//
//
#define READ_THIS_PIN 3 // will be reported as "value1"
// helper functions for constructing the POST data
// append a string or int to a buffer, return the resulting end of string
char *append_str(char *here, char *s) {
while (*here++ = *s++)
;
return here-1;
}
char *append_ul(char *here, unsigned long u) {
char buf[20]; // we "just know" this is big enough
return append_str(here, ultoa(u, buf, 10));
}
//
// This is called once per iteration to read the pin
// and send a POST to trigger the IFTTT/Maker event
//
void update_event() {
EthernetClient client = EthernetClient();
// connect to the Maker event server
client.connect("maker.ifttt.com", 80);
// construct the POST request
char post_rqst[256]; // hand-calculated to be big enough
char *p = post_rqst;
p = append_str(p, "POST /trigger/");
p = append_str(p, MakerIFTTT_Event);
p = append_str(p, "/with/key/");
p = append_str(p, MakerIFTTT_Key);
p = append_str(p, " HTTP/1.1\r\n");
p = append_str(p, "Host: maker.ifttt.com\r\n");
p = append_str(p, "Content-Type: application/json\r\n");
p = append_str(p, "Content-Length: ");
// we need to remember where the content length will go, which is:
char *content_length_here = p;
// it's always two digits, so reserve space for them (the NN)
p = append_str(p, "NN\r\n");
// end of headers
p = append_str(p, "\r\n");
// construct the JSON; remember where we started so we will know len
char *json_start = p;
// As described - this example reports a pin, uptime, and "hello world"
p = append_str(p, "{\"value1\":\"");
p = append_ul(p, analogRead(READ_THIS_PIN));
p = append_str(p, "\",\"value2\":\"");
p = append_ul(p, millis());
p = append_str(p, "\",\"value3\":\"");
p = append_str(p, "hello, world!");
p = append_str(p, "\"}");
// go back and fill in the JSON length
// we just know this is at most 2 digits (and need to fill in both)
int i = strlen(json_start);
content_length_here[0] = '0' + (i/10);
content_length_here[1] = '0' + (i%10);
// finally we are ready to send the POST to the server!
client.print(post_rqst);
client.stop();
}
// called once at system reset/startup time
void setup() {
// this sets up the network connection, including IP addr via DHCP
Ethernet.begin(mac);
// the input pin for this example
pinMode(READ_THIS_PIN, INPUT_PULLUP);
}
// how often to read the pins and update IFTTT
#define LOOP_DELAY_MSEC (300*1000L) // 5 minutes
// main body; called over and over if it ever returns
void loop() {
// DHCP lease check/renewal (library only sends request if expired)
Ethernet.maintain();
// read the pins, send to IFTTT/Maker
update_event();
// only "this often"
delay(LOOP_DELAY_MSEC);
}
@bkkrunal
Copy link

Hi.
Thanks for sharing your work...
I was trying to make a call using GET request but could not achieve anything.
Your code worked for me, but I don't understand why GET was not working. Can you please suggest.
This works when I make GET request using hurl.it website...
Here is my loop function:

if(client.connect("maker.ifttt.com", 80))
{

//Key = kkkkkkkkkkkkkkkkkxxxxxxxxxx

strcat( post_rqst ,"GET maker.ifttt.com/trigger/TemperatureLog/with/key/kkkkkkkkkkkkkkkkkxxxxxxxxxx?value1=12");

Serial.println(post_rqst);
client.println(post_rqst); 
client.stop();

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment