/ IFTTT_Door_Detector / IFTTT_Door_Detector.ino
IFTTT_Door_Detector.ino
1 // SPDX-FileCopyrightText: 2018 Tod Treece for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 // Adafruit IO IFTTT Door Detector 6 // 7 // Learn Guide: https://learn.adafruit.com/using-ifttt-with-adafruit-io 8 // 9 // Adafruit invests time and resources providing this open source code. 10 // Please support Adafruit and open source hardware by purchasing 11 // products from Adafruit! 12 // 13 // Written by Todd Treece for Adafruit Industries 14 // Copyright (c) 2018 Adafruit Industries 15 // Licensed under the MIT license. 16 // 17 // All text above must be included in any redistribution. 18 19 /************************** Configuration ***********************************/ 20 21 // edit the config.h tab and enter your Adafruit IO credentials 22 // and any additional configuration needed for WiFi, cellular, 23 // or ethernet clients. 24 #include "config.h" 25 #include <EEPROM.h> 26 /************************ Example Starts Here *******************************/ 27 28 // door gpio pin 29 #define DOOR 13 30 31 // how often to report battery level to adafruit IO (in minutes) 32 #define BATTERY_INTERVAL 5 33 34 // how long to sleep between checking the door state (in seconds) 35 #define SLEEP_LENGTH 3 36 37 void setup() { 38 39 // start the serial connection 40 Serial.begin(115200); 41 42 while (!Serial); 43 Serial.println("Adafruit IO Door Detector"); 44 45 EEPROM.begin(512); 46 pinMode(DOOR, INPUT_PULLUP); 47 48 // get the current count position from eeprom 49 byte battery_count = EEPROM.read(0); 50 51 // we only need this to happen once every X minutes, 52 // so we use eeprom to track the count between resets. 53 if(battery_count >= ((BATTERY_INTERVAL * 60) / SLEEP_LENGTH)) { 54 // reset counter 55 battery_count = 0; 56 // report battery level to Adafruit IO 57 battery_level(); 58 } 59 else { 60 // increment counter 61 battery_count++; 62 } 63 64 // save the current count 65 EEPROM.write(0, battery_count); 66 EEPROM.commit(); 67 68 // if door isn't open, we don't need to send anything 69 if(digitalRead(DOOR) == LOW) { 70 Serial.println("Door closed"); 71 // we don't do anything 72 } 73 else { 74 // the door is open if we have reached here, 75 // so we should send a value to Adafruit IO. 76 Serial.println("Door is open!"); 77 door_open(); 78 } 79 80 // we are done here. go back to sleep. 81 Serial.println("zzzz"); 82 ESP.deepSleep(SLEEP_LENGTH * 1000000); 83 } 84 85 void loop() { 86 // noop 87 } 88 89 void door_open(){ 90 // connect us to Adafruit IO... 91 connect_AIO(); 92 93 // grab the door feed 94 AdafruitIO_Feed *door = io.feed("door"); 95 96 Serial.println("Sending door value to door feed..."); 97 door->save(1); 98 io.run(); 99 } 100 101 void battery_level() { 102 // read the battery level from the ESP8266 analog in pin. 103 // analog read level is 10 bit 0-1023 (0V-1V). 104 // our 1M & 220K voltage divider takes the max 105 // lipo value of 4.2V and drops it to 0.758V max. 106 // this means our min analog read value should be 580 (3.14V) 107 // and the max analog read value should be 774 (4.2V). 108 int level = analogRead(A0); 109 110 // convert battery level to percent 111 level = map(level, 580, 774, 0, 100); 112 Serial.print("Battery level: "); Serial.print(level); Serial.println("%"); 113 114 // connect us to Adafruit IO 115 connect_AIO(); 116 // grab the battery feed 117 AdafruitIO_Feed *battery = io.feed("battery"); 118 119 // send battery level to AIO 120 battery->save(level); 121 io.run(); 122 } 123 124 void connect_AIO() { 125 Serial.println("Connecting to Adafruit IO..."); 126 io.connect(); 127 128 // wait for a connection 129 while (io.status() < AIO_CONNECTED) { 130 Serial.print("."); 131 delay(500); 132 } 133 134 // we are connected 135 Serial.println(); 136 Serial.println(io.statusText()); 137 } 138