Merge pull request #281 from brentru/doordetect
Add AIO Door Detector Guide
This commit is contained in:
commit
d358e63e46
2 changed files with 177 additions and 0 deletions
134
IFTTT_Door_Detector/IFTTT_Door_Detector.ino
Normal file
134
IFTTT_Door_Detector/IFTTT_Door_Detector.ino
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
// Adafruit IO IFTTT Door Detector
|
||||
//
|
||||
// Learn Guide: https://learn.adafruit.com/using-ifttt-with-adafruit-io
|
||||
//
|
||||
// Adafruit invests time and resources providing this open source code.
|
||||
// Please support Adafruit and open source hardware by purchasing
|
||||
// products from Adafruit!
|
||||
//
|
||||
// Written by Todd Treece for Adafruit Industries
|
||||
// Copyright (c) 2018 Adafruit Industries
|
||||
// Licensed under the MIT license.
|
||||
//
|
||||
// All text above must be included in any redistribution.
|
||||
|
||||
/************************** Configuration ***********************************/
|
||||
|
||||
// edit the config.h tab and enter your Adafruit IO credentials
|
||||
// and any additional configuration needed for WiFi, cellular,
|
||||
// or ethernet clients.
|
||||
#include "config.h"
|
||||
#include <EEPROM.h>
|
||||
/************************ Example Starts Here *******************************/
|
||||
|
||||
// door gpio pin
|
||||
#define DOOR 13
|
||||
|
||||
// how often to report battery level to adafruit IO (in minutes)
|
||||
#define BATTERY_INTERVAL 5
|
||||
|
||||
// how long to sleep between checking the door state (in seconds)
|
||||
#define SLEEP_LENGTH 3
|
||||
|
||||
void setup() {
|
||||
|
||||
// start the serial connection
|
||||
Serial.begin(115200);
|
||||
|
||||
while (!Serial);
|
||||
Serial.println("Adafruit IO Door Detector");
|
||||
|
||||
EEPROM.begin(512);
|
||||
pinMode(DOOR, INPUT_PULLUP);
|
||||
|
||||
// get the current count position from eeprom
|
||||
byte battery_count = EEPROM.read(0);
|
||||
|
||||
// we only need this to happen once every X minutes,
|
||||
// so we use eeprom to track the count between resets.
|
||||
if(battery_count >= ((BATTERY_INTERVAL * 60) / SLEEP_LENGTH)) {
|
||||
// reset counter
|
||||
battery_count = 0;
|
||||
// report battery level to Adafruit IO
|
||||
battery_level();
|
||||
}
|
||||
else {
|
||||
// increment counter
|
||||
battery_count++;
|
||||
}
|
||||
|
||||
// save the current count
|
||||
EEPROM.write(0, battery_count);
|
||||
EEPROM.commit();
|
||||
|
||||
// if door isn't open, we don't need to send anything
|
||||
if(digitalRead(DOOR) == LOW) {
|
||||
Serial.println("Door closed");
|
||||
// we don't do anything
|
||||
}
|
||||
else {
|
||||
// the door is open if we have reached here,
|
||||
// so we should send a value to Adafruit IO.
|
||||
Serial.println("Door is open!");
|
||||
door_open();
|
||||
}
|
||||
|
||||
// we are done here. go back to sleep.
|
||||
Serial.println("zzzz");
|
||||
ESP.deepSleep(SLEEP_LENGTH * 1000000);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// noop
|
||||
}
|
||||
|
||||
void door_open(){
|
||||
// connect us to Adafruit IO...
|
||||
connect_AIO();
|
||||
|
||||
// grab the door feed
|
||||
AdafruitIO_Feed *door = io.feed("door");
|
||||
|
||||
Serial.println("Sending door value to door feed...");
|
||||
door->save(1);
|
||||
io.run();
|
||||
}
|
||||
|
||||
void battery_level() {
|
||||
// read the battery level from the ESP8266 analog in pin.
|
||||
// analog read level is 10 bit 0-1023 (0V-1V).
|
||||
// our 1M & 220K voltage divider takes the max
|
||||
// lipo value of 4.2V and drops it to 0.758V max.
|
||||
// this means our min analog read value should be 580 (3.14V)
|
||||
// and the max analog read value should be 774 (4.2V).
|
||||
int level = analogRead(A0);
|
||||
|
||||
// convert battery level to percent
|
||||
level = map(level, 580, 774, 0, 100);
|
||||
Serial.print("Battery level: "); Serial.print(level); Serial.println("%");
|
||||
|
||||
// connect us to Adafruit IO
|
||||
connect_AIO();
|
||||
// grab the battery feed
|
||||
AdafruitIO_Feed *battery = io.feed("battery");
|
||||
|
||||
// send battery level to AIO
|
||||
battery->save(level);
|
||||
io.run();
|
||||
}
|
||||
|
||||
void connect_AIO() {
|
||||
Serial.println("Connecting to Adafruit IO...");
|
||||
io.connect();
|
||||
|
||||
// wait for a connection
|
||||
while (io.status() < AIO_CONNECTED) {
|
||||
Serial.print(".");
|
||||
delay(500);
|
||||
}
|
||||
|
||||
// we are connected
|
||||
Serial.println();
|
||||
Serial.println(io.statusText());
|
||||
}
|
||||
|
||||
43
IFTTT_Door_Detector/config.h
Normal file
43
IFTTT_Door_Detector/config.h
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/************************ Adafruit IO Config *******************************/
|
||||
|
||||
// visit io.adafruit.com if you need to create an account,
|
||||
// or if you need your Adafruit IO key.
|
||||
#define IO_USERNAME "your_username"
|
||||
#define IO_KEY "your_key"
|
||||
|
||||
/******************************* WIFI **************************************/
|
||||
|
||||
// the AdafruitIO_WiFi client will work with the following boards:
|
||||
// - HUZZAH ESP8266 Breakout -> https://www.adafruit.com/products/2471
|
||||
// - Feather HUZZAH ESP8266 -> https://www.adafruit.com/products/2821
|
||||
// - Feather M0 WiFi -> https://www.adafruit.com/products/3010
|
||||
// - Feather WICED -> https://www.adafruit.com/products/3056
|
||||
|
||||
#define WIFI_SSID "your_ssid"
|
||||
#define WIFI_PASS "your_pass"
|
||||
|
||||
// comment out the following two lines if you are using fona or ethernet
|
||||
#include "AdafruitIO_WiFi.h"
|
||||
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
|
||||
|
||||
|
||||
/******************************* FONA **************************************/
|
||||
|
||||
// the AdafruitIO_FONA client will work with the following boards:
|
||||
// - Feather 32u4 FONA -> https://www.adafruit.com/product/3027
|
||||
|
||||
// uncomment the following two lines for 32u4 FONA,
|
||||
// and comment out the AdafruitIO_WiFi client in the WIFI section
|
||||
// #include "AdafruitIO_FONA.h"
|
||||
// AdafruitIO_FONA io(IO_USERNAME, IO_KEY);
|
||||
|
||||
|
||||
/**************************** ETHERNET ************************************/
|
||||
|
||||
// the AdafruitIO_Ethernet client will work with the following boards:
|
||||
// - Ethernet FeatherWing -> https://www.adafruit.com/products/3201
|
||||
|
||||
// uncomment the following two lines for ethernet,
|
||||
// and comment out the AdafruitIO_WiFi client in the WIFI section
|
||||
// #include "AdafruitIO_Ethernet.h"
|
||||
// AdafruitIO_Ethernet io(IO_USERNAME, IO_KEY);
|
||||
Loading…
Reference in a new issue