rjconcepcion
Published © CC BY-NC-ND

Watch Your Fridge

Check how many times and for how long your fridge is open every half an hour. All that data is sent to Adafruit IO. Enjoy it.

IntermediateProtip3 hours599
Watch Your Fridge

Things used in this project

Hardware components

Espressif ESP8266 ESP-01
×1
ESP-01 breadboard adapter
×1
Door Magnetic Sensor
×1
Power Supply 12 Vdc
×1
Breadboard Power Supply
×1
Resistor 10K ohm
×1
Resistor 330 ohm
×1
Breadboard
×1
5mm Led
×1
Breadboard jumpers
×1
Breadboard flexible jumpers
×1

Software apps and online services

Arduino IDE
Arduino IDE
Adafruit IO

Story

Read more

Schematics

Schematic

Remember. ESP-01 works with 3.3 V.
GPIO2 pin needs to be high at boot.

Code

Tiempo_apertura_nevera_Adafruit_IO_V2_Ingles.ino

Arduino
// Information about Adafruit IO
// Link: https://learn.adafruit.com/welcome-to-adafruit-io/overview

// 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) 2016 Adafruit Industries
// Licensed under the MIT license.
//
// All text above must be included in any redistribution.

/************************** Configuration ***********************************/

// In the config.h you should set the Adafruit IO credentials and configure the 
// network settings. 

#include "config.h"

/************************ Example Starts Here *******************************/


// Variables
bool valor;
int count = 0;
bool flag = false;
unsigned long previo;
unsigned long actual;
unsigned long tiempo;
unsigned long ttotal = 0;
unsigned long intervalo = 1000;

unsigned long duracion = 0;
const int puerta = 2; // GPIO2
const int led = 0; // GPIO0


// set up the feeds
AdafruitIO_Feed *tempo = io.feed("tempo");
AdafruitIO_Feed *cuenta = io.feed("cuenta");

void setup() {

  // start the serial connection
  Serial.begin(115200);

  pinMode(puerta, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led_conteo, OUTPUT);
  pinMode(led_puerta, OUTPUT);

  // wait for serial monitor to open
  while(! Serial);

  // connect to io.adafruit.com
  Serial.print("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());

  

}

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();

  digitalWrite(led, LOW);

  valor = digitalRead(puerta); // Read the sensor. If the door is close valor = High or true. If the door is open valor = low or false.

  if (valor == false & flag == false) {
    
    delay(200);

    valor = digitalRead(puerta);
          
    if (valor == false){
      count++;            // Increase the opening counter
      flag = true;
      previo = millis();  // Get the time at that moment
      
      }
   }
      
  if (valor == true & flag == true) {
    
    delay(200);

    valor = digitalRead(puerta);

    if (valor == true){
      flag = false;
      actual = millis();  // Get the time at that moment.
    
      // Increase the time variable after the door was closed.    
      if (actual - previo > intervalo){
        tiempo = actual - previo;

        tiempo = tiempo/1000;  // Change time from miliseconds to seconds.

        ttotal = tiempo + ttotal;  // Add time to the total timer.
        }
              
      }
    }
  
  
  duracion++; // increase by one the duracion variable
  delay(400);
  

  if (duracion > 3800){ //Set time between data sending.

    // Show the variable ttotal in the serial monitor and send it to Adafruit
    Serial.print("sending -> ");
    Serial.println(ttotal);
    tempo->save(ttotal);

    // Show the variable count in the serial monitor and send it to Adafruit
    Serial.print("sending -> ");
    Serial.println(count);
    cuenta->save(count);
    
    // Reset variables
    duracion = 0;
    ttotal = 0;
    count = 0;

    digitalWrite(led, HIGH);  // turn on the light to show sending data

    delay(1000); //Delay 1 second
    
  }
}

Config file

Arduino
/************************ 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   "rjconcepcion"
#define IO_KEY        "ce712b7e28e641ada9ce2757ccf00bcb"

/******************************* 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 HUZZAH ESP32 -> https://www.adafruit.com/product/3405
//   - Feather M0 WiFi -> https://www.adafruit.com/products/3010
//   - Feather WICED -> https://www.adafruit.com/products/3056
//   - Adafruit PyPortal -> https://www.adafruit.com/product/4116
//   - Adafruit Metro M4 Express AirLift Lite -> https://www.adafruit.com/product/4000
//   - Adafruit AirLift Breakout -> https://www.adafruit.com/product/4201
//   - Adafruit AirLift Shield -> https://www.adafruit.com/product/4285
//   - Adafruit AirLift FeatherWing -> https://www.adafruit.com/product/4264



#define WIFI_SSID   "Your SSID"
#define WIFI_PASS   "Wifi Password"

// uncomment the following line if you are using airlift
// #define USE_AIRLIFT

// uncomment the following line if you are using winc1500
// #define USE_WINC1500



// comment out the following lines if you are using fona or ethernet
#include "AdafruitIO_WiFi.h"

#if defined(USE_AIRLIFT) || defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE)
  // Configure the pins used for the ESP32 connection
  #if !defined(SPIWIFI_SS) // if the wifi definition isnt in the board variant
    // Don't change the names of these #define's! they match the variant ones
    #define SPIWIFI SPI
    #define SPIWIFI_SS 10  // Chip select pin
    #define NINA_ACK 9    // a.k.a BUSY or READY pin
    #define NINA_RESETN 6 // Reset pin
    #define NINA_GPIO0 -1 // Not connected
  #endif
  AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS, SPIWIFI_SS, NINA_ACK, NINA_RESETN, NINA_GPIO0, &SPIWIFI);
#else
  AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
#endif
/******************************* 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);

Credits

rjconcepcion

rjconcepcion

11 projects • 8 followers
Electronic is my passion. I like to work with programming devices like Arduino, ESP8266, Raspberry Pi. I enjoy design electronic projects.

Comments