Skip to content

Instantly share code, notes, and snippets.

@ayan4m1
Last active May 23, 2021 03:53
Show Gist options
  • Save ayan4m1/a3e26fff546f11ead2a553189699763b to your computer and use it in GitHub Desktop.
Save ayan4m1/a3e26fff546f11ead2a553189699763b to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <ESP8266WiFi.h>
#define WIFI_SSID "bar"
#define WIFI_PSK "changeme"
#define IFTTT_WEBHOOK_URL "https://maker.ifttt.com/trigger/YOUR EVENT NAME HERE/with/key/YOUR KEY HERE"
#define IFTTT_WEBHOOK_HOST "maker.ifttt.com"
#define IFTTT_WEBHOOK_PORT 80
#define HTTP_TIMEOUT 5 // 5 seconds
#define PIR_PIN D7
#define SERIAL_BAUD 115200
#define POLLING_INTERVAL 30000 // 30 seconds
bool currentState = false;
WiFiClient client;
void notify() {
client.connect(IFTTT_WEBHOOK_HOST, IFTTT_WEBHOOK_PORT);
client.print(String("GET ") + IFTTT_WEBHOOK_URL +
" HTTP/1.1\r\n" +
"Host: " + IFTTT_WEBHOOK_HOST + "\r\n" +
"Connection: close\r\n\r\n");
client.stop();
}
void setup() {
Serial.begin(SERIAL_BAUD);
while (!Serial) {
delay(10);
}
pinMode(PIR_PIN, INPUT);
WiFi.begin(WIFI_SSID, WIFI_PSK);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println(F("Connected!"));
}
void loop() {
long state = digitalRead(PIR_PIN);
if (state == HIGH && !currentState) {
currentState = true;
Serial.println(F("Motion detected!"));
notify();
} else if (state == LOW && currentState) {
currentState = false;
Serial.println(F("Motion stopped!"));
}
delay(POLLING_INTERVAL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment