Advertisement
Guest User

DoorAlarm

a guest
Dec 20th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. /*----------------------------------------------------------------------------
  2. A simple way to get a notification when a door is opened, written by Sunny Ahmed and Linnea Olsson
  3. ----------------------------------------------------------------------------*/
  4.  
  5. #include <ESP8266WiFi.h>
  6. #include <DNSServer.h>
  7. #include <WiFiClient.h>
  8.  
  9.  
  10. // Network Definitions
  11.  
  12. const char* SSID= "YOUR SSID";
  13. const char* PASS= "YOUR PASSWORD";
  14.  
  15. // IFTTT Definitions
  16. const char* IFTTT_URL= "maker.ifttt.com";
  17. const char* IFTTT_KEY= "YOUR IFTTT_KEY";
  18. const char* IFTTT_EVENT = "YOUR_IFTTT_EVENT";
  19.  
  20. // Pin Definitions
  21. const int MAG_PIN = D5;
  22.  
  23.  
  24. void setup() {
  25.  
  26. initHardware();
  27. Serial.println();
  28. Serial.println();
  29. Serial.print("Connecting to ");
  30. Serial.println(SSID);
  31.  
  32. WiFi.begin(SSID, PASS);
  33.  
  34. while (WiFi.status() != WL_CONNECTED) {
  35. delay(500);
  36. Serial.print(".");
  37. }
  38.  
  39. Serial.println("");
  40. Serial.println("WiFi connected");
  41. Serial.println("IP address: ");
  42. Serial.println(WiFi.localIP());
  43. }
  44.  
  45. void loop() {
  46. //simply check each loop if the magnet is still close enough to the door to overcome the digital in threshold
  47. if (digitalRead(MAG_PIN) == false) {
  48.  
  49. triggerMagnetEvent(IFTTT_EVENT);
  50.  
  51. }
  52. }
  53.  
  54. void initHardware()
  55. {
  56. // Serial
  57. Serial.begin(115200);
  58. delay(10);
  59. // Magnet
  60. pinMode(MAG_PIN, INPUT);
  61.  
  62. }
  63.  
  64.  
  65. // Magnet Functions
  66.  
  67. void triggerMagnetEvent(String eventName)
  68. {
  69.  
  70. //this somewhat messy code is taken from the ESP8266 WiFi Client example included in the library
  71. // Define the WiFi Client
  72. WiFiClient client;
  73. // Set the http Port
  74. const int httpPort = 80;
  75.  
  76. // Make sure we can connect
  77. if (!client.connect(IFTTT_URL, httpPort)) {
  78. return;
  79. }
  80.  
  81. // We now create a URI for the request
  82. String url = "/trigger/" + String(eventName) + "/with/key/" + String(IFTTT_KEY);
  83.  
  84. value_1 = True;
  85.  
  86. // Build JSON data string
  87. String data = "";
  88. data = data + "\n" + "{\"value1\"}";
  89.  
  90. // Post the button press to IFTTT
  91. if (client.connect(IFTTT_URL, httpPort)) {
  92.  
  93. // Sent HTTP POST Request with JSON data
  94. client.println("POST "+ url +" HTTP/1.1");
  95. Serial.println("POST "+ url +" HTTP/1.1");
  96. client.println("Host: "+ String(IFTTT_URL));
  97. Serial.println("Host: "+ String(IFTTT_URL));
  98. client.println("User-Agent: Arduino/1.0");
  99. Serial.println("User-Agent: Arduino/1.0");
  100. client.print("Accept: *");
  101. Serial.print("Accept: *");
  102. client.print("/");
  103. Serial.print("/");
  104. client.println("*");
  105. Serial.println("*");
  106. client.print("Content-Length: ");
  107. Serial.print("Content-Length: ");
  108. client.println(data.length());
  109. Serial.println(data.length());
  110. client.println("Content-Type: application/json");
  111. Serial.println("Content-Type: application/json");
  112. client.println("Connection: close");
  113. Serial.println("Connection: close");
  114. client.println();
  115. Serial.println();
  116. client.println(data);
  117. Serial.println(data);
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement