Last active
January 25, 2020 02:45
-
-
Save liz-miller/00cc59402ace7d51dbb0e30d50dc80da to your computer and use it in GitHub Desktop.
Arduino to IFTTT using Wemos D1 Mini - POST methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <ESP8266WiFi.h> | |
// WiFi parameters | |
const char* ssid = "your-ssid"; //replace with your ssid | |
const char* password = "your-pw";//replace with your pw | |
// IFTTT Maker parameters: | |
char MakerIFTTT_Key[] = "your-ifttt-key"; // Obtained when setting up/connecting the Maker channel in IFTTT | |
char MakerIFTTT_Event[] = "wemos-trigger"; // Arbitrary name for the event; used in the IFTTT recipe. | |
WiFiClient client; | |
#define pot A0 // will be reported as "value1" | |
#define LOOP_DELAY_MSEC (300*1000L) // 5 minutes | |
void setupWiFi(){ | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.print(ssid); | |
Serial.println("..."); | |
WiFi.begin(ssid, password); | |
int retries = 0; | |
while ((WiFi.status() != WL_CONNECTED) && (retries < 15)){ | |
retries++; | |
delay(500); | |
Serial.print("."); | |
} | |
if(retries>14){ | |
Serial.println(F("WiFi conenction FAILED")); | |
} | |
if (WiFi.status() == WL_CONNECTED){ | |
Serial.println(F("WiFi connected!")); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
Serial.println(F("Setup ready")); | |
} | |
void setup() { | |
Serial.begin(115200); //set the baud rate for Wemos D1 mini | |
setupWiFi(); //call our WiFi method to connect to the internet | |
// the input pin for this example | |
pinMode(pot, INPUT); | |
} | |
// *** 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)); | |
} | |
// Send a POST to trigger the IFTTT/Maker event | |
void updateEvent() { | |
// 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: "); | |
char *content_length_here = p; // we need to remember where the content length will go, which is: | |
p = append_str(p, "NN\r\n"); // it's always two digits, so reserve space for them (the NN) | |
p = append_str(p, "\r\n"); // end of headers | |
char *json_start = p; // construct the JSON; remember where we started so we will know len | |
// As described - this example reports a pin, uptime, and "hello world" | |
Serial.println("Sending values to IFTTT"); | |
p = append_str(p, "{\"value1\":\""); | |
p = append_ul(p, analogRead(pot)); | |
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(); | |
} | |
void loop(){ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment