-
-
Save RuiSantosdotme/911e9cf50c04d2922da25cb26edb20ec to your computer and use it in GitHub Desktop.
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 <WiFi.h> | |
#include <WiFiMulti.h> | |
#include <HTTPClient.h> | |
#include <WiFiClientSecure.h> | |
WiFiMulti WiFiMulti; | |
const char* ssid = "REPLACE_WTIH_YOUR_SSID"; | |
const char* password = "REPLACE_WTIH_YOUR_PASSWORD"; | |
void setup() { | |
Serial.begin(115200); | |
// Serial.setDebugOutput(true); | |
Serial.println(); | |
Serial.println(); | |
Serial.println(); | |
WiFi.mode(WIFI_STA); | |
WiFiMulti.addAP(ssid, password); | |
// wait for WiFi connection | |
Serial.print("Waiting for WiFi to connect..."); | |
while ((WiFiMulti.run() != WL_CONNECTED)) { | |
Serial.print("."); | |
} | |
Serial.println(" connected"); | |
} | |
void loop() { | |
WiFiClientSecure *client = new WiFiClientSecure; | |
if(client) { | |
//client -> setCACert(rootCACertificate); | |
{ | |
// Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is | |
HTTPClient https; | |
Serial.print("[HTTPS] begin...\n"); | |
if (https.begin(*client, "https://maker.ifttt.com/trigger/REPLACE_WITH_YOUR_EVENT_NAME/with/key/REPLACE_WITH_YOUR_WEBHOOKS_IFTTT_API_KEY")) { // HTTPS | |
Serial.print("[HTTPS] POST...\n"); | |
// Specify content-type header | |
https.addHeader("Content-Type", "application/x-www-form-urlencoded"); | |
// Data to send with HTTP POST | |
String httpsRequestData = "value1=" + String(random(40)) + "&value2=" + String(random(40))+ "&value3=" + String(random(40)); | |
// Send HTTP POST request | |
int httpCode = https.POST(httpsRequestData); | |
// httpCode will be negative on error | |
if (httpCode > 0) { | |
// HTTP header has been send and Server response header has been handled | |
Serial.printf("[HTTPS] POST... code: %d\n", httpCode); | |
// file found at server | |
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { | |
String payload = https.getString(); | |
Serial.println(payload); | |
} | |
} else { | |
Serial.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpCode).c_str()); | |
} | |
https.end(); | |
} else { | |
Serial.printf("[HTTPS] Unable to connect\n"); | |
} | |
// End extra scoping block | |
} | |
delete client; | |
} else { | |
Serial.println("Unable to create client"); | |
} | |
Serial.println(); | |
Serial.println("Waiting 10s before the next round..."); | |
delay(10000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment