Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*----------------------------------------------------------------------------
- A simple way to get a notification when a door is opened, written by Sunny Ahmed and Linnea Olsson
- ----------------------------------------------------------------------------*/
- #include <ESP8266WiFi.h>
- #include <DNSServer.h>
- #include <WiFiClient.h>
- // Network Definitions
- const char* SSID= "YOUR SSID";
- const char* PASS= "YOUR PASSWORD";
- // IFTTT Definitions
- const char* IFTTT_URL= "maker.ifttt.com";
- const char* IFTTT_KEY= "YOUR IFTTT_KEY";
- const char* IFTTT_EVENT = "YOUR_IFTTT_EVENT";
- // Pin Definitions
- const int MAG_PIN = D5;
- void setup() {
- initHardware();
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(SSID);
- WiFi.begin(SSID, PASS);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void loop() {
- //simply check each loop if the magnet is still close enough to the door to overcome the digital in threshold
- if (digitalRead(MAG_PIN) == false) {
- triggerMagnetEvent(IFTTT_EVENT);
- }
- }
- void initHardware()
- {
- // Serial
- Serial.begin(115200);
- delay(10);
- // Magnet
- pinMode(MAG_PIN, INPUT);
- }
- // Magnet Functions
- void triggerMagnetEvent(String eventName)
- {
- //this somewhat messy code is taken from the ESP8266 WiFi Client example included in the library
- // Define the WiFi Client
- WiFiClient client;
- // Set the http Port
- const int httpPort = 80;
- // Make sure we can connect
- if (!client.connect(IFTTT_URL, httpPort)) {
- return;
- }
- // We now create a URI for the request
- String url = "/trigger/" + String(eventName) + "/with/key/" + String(IFTTT_KEY);
- value_1 = True;
- // Build JSON data string
- String data = "";
- data = data + "\n" + "{\"value1\"}";
- // Post the button press to IFTTT
- if (client.connect(IFTTT_URL, httpPort)) {
- // Sent HTTP POST Request with JSON data
- client.println("POST "+ url +" HTTP/1.1");
- Serial.println("POST "+ url +" HTTP/1.1");
- client.println("Host: "+ String(IFTTT_URL));
- Serial.println("Host: "+ String(IFTTT_URL));
- client.println("User-Agent: Arduino/1.0");
- Serial.println("User-Agent: Arduino/1.0");
- client.print("Accept: *");
- Serial.print("Accept: *");
- client.print("/");
- Serial.print("/");
- client.println("*");
- Serial.println("*");
- client.print("Content-Length: ");
- Serial.print("Content-Length: ");
- client.println(data.length());
- Serial.println(data.length());
- client.println("Content-Type: application/json");
- Serial.println("Content-Type: application/json");
- client.println("Connection: close");
- Serial.println("Connection: close");
- client.println();
- Serial.println();
- client.println(data);
- Serial.println(data);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement