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
// Tutorial Link: https://learn.adafruit.com/using-ifttt-with-adafruit-io
// 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
// modified by Brent Rubell 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 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
// how long to sleep between checking the door state (in seconds)
#define SLEEP_LENGTH 30
// 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");
#define SLEEP_LENGTH 3
void setup() {
// start the serial connection
Serial.begin(115200);
// wait for serial monitor to open
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
Serial.println("Connecting to Adafruit IO");
io.connect();
// get the current count position from eeprom
byte battery_count = EEPROM.read(0);
// wait for a connection
while(io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}
// 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
// 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 {
loop_cycles++;
// increment counter
battery_count++;
}
if (digitalRead(DOOR_PIN) == LOW) {
// if the door isn't open, we don't need to send anything.
Serial.println("\tDoor Closed");
// 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 {
// door open, let's send a value to adafruit io
Serial.println("\tDoor Open");
door->save(1);
// 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();
}
// delay the loop by SLEEP_LENGTH seconds
delay(SLEEP_LENGTH*1000);
// 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() {
@ -101,9 +102,33 @@ void battery_level() {
// 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());
}