Skip to content

Instantly share code, notes, and snippets.

@polluxlabs
Last active March 27, 2020 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save polluxlabs/efe1b5633e18e2615c057349f9a33faf to your computer and use it in GitHub Desktop.
Save polluxlabs/efe1b5633e18e2615c057349f9a33faf to your computer and use it in GitHub Desktop.
Alarm System for the ESP8266
/*********
Pollux Labs
https://polluxlabs.net
*********/
#include <ESP8266WiFi.h>
// WLAN-Zugangsdaten
const char* ssid = "Name deines WLAN-Netzes";
const char* password = "Dein WLAN-Passwort";
const char* host = "maker.ifttt.com";
const char* apiKey = "Dein Api Key von IFTTT";
const char* object = "Dein Überwachungsobjekt";
int contentLength = 7 + String(object).length(); //Content-Length für den Request berechnen (value1= entspricht 7)
void setup() {
pinMode (13, INPUT); //Radar Pin (am ESP8266 D7)
Serial.begin(115200); //Verbindung zum Seriellen Monitor
}
void loop() {
delay(100); //100 Millisekunden warten
if (digitalRead(13) == 1) { //Sobald das Radar eine Bewegung registriert, startet die Internet-Verbindung
WiFi.begin(ssid, password); //Internet-Verbindung starten
while (WiFi.status() != WL_CONNECTED) { //Statusnachricht "Verbindung herstellen" solange nicht verbunden
Serial.println("Connecting to WiFi...");
delay(1000);
}
delay(1000);
Serial.println("Hello, world!"); //Ausgabe, sobald die Verbindung zum Internet steht
Serial.println("Connecting to host");
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
String url = "/trigger/alarm/with/key/";
url += apiKey;
Serial.print("Requesting URL: ");
Serial.println(url);
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: " + contentLength + "\r\n\r\n" +
"value1=" + object + "\r\n");
delay(10000); //Sensor für die nächsten 10 Sekunden pausieren
} else {
WiFi.disconnect(); //Wenn keine Bewegung registriert wird Internet-Verbindung beenden...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment