Hardware components | ||||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
Software apps and online services | ||||||
| ||||||
|
This device check how many times and for how long you open your door fridge, then every 30 minutes, it sends that data to the IoT platform Adafruit IO.
I want to measure how many times and for how long my fridge was opened every day.
The device is based in the ESP-01 which is able to connect via wifi to an Internet network. It has a door magnetic sensor. When the door is open, the device increase a count an gets the ESP-01 clock time. Then after the door is closed, it gets the ESP-01 clock time and makes a subtraction. That time is stored in a variable and send every 30 minutes.
You can change the interval time between data is sent.
The image below shows the dashboard with some data.
For more information about Adafruit IO check the link below:
https://learn.adafruit.com/welcome-to-adafruit-io/overview
Note: If you are having issues connecting, please ensure you have the latest Adafruit IO Arduino library.
// 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
}
}
/************************ 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);
Comments