adding deepsleep

This commit is contained in:
brentru 2018-07-30 15:24:09 -04:00
parent 802971fa5f
commit ccccf0fd8d

View file

@ -1,96 +1,97 @@
// Adafruit IO IFTTT Door Detector Example // Adafruit IO IFTTT Door Detector
// Tutorial Link: https://learn.adafruit.com/using-ifttt-with-adafruit-io //
// Learn Guide: https://learn.adafruit.com/using-ifttt-with-adafruit-io
// //
// Adafruit invests time and resources providing this open source code. // Adafruit invests time and resources providing this open source code.
// Please support Adafruit and open source hardware by purchasing // Please support Adafruit and open source hardware by purchasing
// products from Adafruit! // products from Adafruit!
// //
// Written by Todd Treece for Adafruit Industries // Written by Todd Treece for Adafruit Industries
// modified by Brent Rubell for Adafruit Industries
// Copyright (c) 2018 Adafruit Industries // Copyright (c) 2018 Adafruit Industries
// Licensed under the MIT license. // Licensed under the MIT license.
// //
// All text above must be included in any redistribution. // All text above must be included in any redistribution.
/************************** Configuration ***********************************/ /************************** Configuration ***********************************/
// edit the config.h tab and enter your Adafruit IO credentials // edit the config.h tab and enter your Adafruit IO credentials
// and any additional configuration needed for WiFi, cellular, // and any additional configuration needed for WiFi, cellular,
// or ethernet clients. // or ethernet clients.
#include "config.h" #include "config.h"
#include <EEPROM.h>
/************************ Example Starts Here *******************************/ /************************ Example Starts Here *******************************/
// door pin
#define DOOR_PIN 13
// how often to report battery level to Adafruit IO (in minutes) // door gpio pin
#define DOOR 13
// how often to report battery level to adafruit IO (in minutes)
#define BATTERY_INTERVAL 5 #define BATTERY_INTERVAL 5
// how long to sleep between checking the door state (in seconds) // how long to sleep between checking the door state (in seconds)
#define SLEEP_LENGTH 30 #define SLEEP_LENGTH 3
// holds the count of the battery check (in minutes)
int loop_cycles = 0;
// set up the `door` feed
AdafruitIO_Feed *door = io.feed("door");
// set up the `battery` feed
AdafruitIO_Feed *battery = io.feed("battery");
void setup() { void setup() {
// start the serial connection // start the serial connection
Serial.begin(115200); Serial.begin(115200);
// wait for serial monitor to open
while (!Serial); while (!Serial);
Serial.println("IFTTT Door Detector"); Serial.println("Adafruit IO Door Detector");
pinMode(DOOR_PIN, INPUT_PULLUP); EEPROM.begin(512);
pinMode(DOOR, INPUT_PULLUP);
// connect to io.adafruit.com // get the current count position from eeprom
Serial.println("Connecting to Adafruit IO"); byte battery_count = EEPROM.read(0);
io.connect();
// wait for a connection // we only need this to happen once every X minutes,
while(io.status() < AIO_CONNECTED) { // so we use eeprom to track the count between resets.
Serial.print("."); if(battery_count >= ((BATTERY_INTERVAL * 60) / SLEEP_LENGTH)) {
delay(500); // reset counter
} battery_count = 0;
// report battery level to Adafruit IO
// we are connected
Serial.println();
Serial.println(io.statusText());
}
void loop() {
// io.run(); is required for all sketches.
// it should always be present at the top of your loop
// function. it keeps the client connected to
// io.adafruit.com, and processes any incoming data.
io.run();
// check the loop interval against the battery check interval
if (loop_cycles == BATTERY_INTERVAL) {
// reset the counter
loop_cycles = 0;
// report the battery level to the IO 'battery' feed
battery_level(); battery_level();
} }
else { else {
loop_cycles++; // increment counter
battery_count++;
} }
if (digitalRead(DOOR_PIN) == LOW) { // save the current count
// if the door isn't open, we don't need to send anything. EEPROM.write(0, battery_count);
Serial.println("\tDoor Closed"); 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 { else {
// door open, let's send a value to adafruit io // the door is open if we have reached here,
Serial.println("\tDoor Open"); // so we should send a value to Adafruit IO.
door->save(1); Serial.println("Door is open!");
door_open();
} }
// delay the loop by SLEEP_LENGTH seconds // we are done here. go back to sleep.
delay(SLEEP_LENGTH*1000); 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() { void battery_level() {
@ -101,9 +102,33 @@ void battery_level() {
// this means our min analog read value should be 580 (3.14V) // this means our min analog read value should be 580 (3.14V)
// and the max analog read value should be 774 (4.2V). // and the max analog read value should be 774 (4.2V).
int level = analogRead(A0); int level = analogRead(A0);
// convert battery level to percent // convert battery level to percent
level = map(level, 580, 774, 0, 100); level = map(level, 580, 774, 0, 100);
Serial.print("Battery level: "); Serial.print(level); Serial.println("%"); 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); 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());
} }