rjconcepcion
Published © CC BY-NC-SA

Measure Your Time in Front of The PC

I propose reducing the time that I spend in front of the PC. For that goal, I need to measure that time, so I decided to design a device!

BeginnerFull instructions provided1 hour246
Measure Your Time in Front of The PC

Things used in this project

Hardware components

ESP8266
×1
Ultrasonic Module HC-SR04 Distance Sensor
×1
.3V 5V Power Supply Module for MB102 102 Prototype Breadboard
×1
Breadboard Jumper Wire Ribbon Cables kit
×1
Solderless Breadboard
×1

Software apps and online services

adafruit IO

Story

Read more

Schematics

Schematic

Code

Time meter code

Arduino
/* Create by: Ricardo Concepcion
website: https://www.rjconcepcion.com

*/

// Libraries
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include "config.h"

const char *ssid     = "YOUR_WIFI_NAME"; 
const char *password = "YOUR_WIFI_PASSWORD"; 

// Define NTP Client
WiFiUDP ntp;
NTPClient timeClient(ntp, "pool.ntp.org");

const int trigger = 4;  // D2 Pin connect to sensor trigger pin
const int echo = 5;     // D1 pin connected to sensor echo pin
const int max_dist = 200; // max distance in cm

// Variables
int hour;
int minutes;
int dist = 0; 
float tiempo = 0;

AdafruitIO_Feed *tiempo_feed = io.feed("tiempo_feed");

void setup() {

  // Initialize Serial Monitor
  Serial.begin(9600);

   // Connect to Wi-Fi
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);

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

  // Initialize NTPClient
  timeClient.begin();

  // Offset time is the different between 0 GMT and your time zone. In my case is -4 GMT. It is equal to -14400.
  timeClient.setTimeOffset(-14400); 

}

void loop() {
  // 

  io.run();
  timeClient.update();

  // Get hour
  hour = timeClient.getHours();
  Serial.print("Hour: ");
  Serial.println(hour);

  // Get minutes
  minutes = timeClient.getMinutes();
  Serial.print("Minutes: ");
  Serial.println(minutes);

  // get the distance between the sensor and the object
  dist = ping(trigger, echo); 
  Serial.print(dist);     
  Serial.println("cm");

  // if the distance is lower than 100 cm then count the time
  if(dist < 100) {
    while (dist < 100) {
      delay(60000); // wait for one minute
      tiempo += 1;  // increase the counter
      dist = ping(trigger, echo);
      Serial.println(dist);

      timeClient.update();
      hour = timeClient.getHours();
      minutes = timeClient.getMinutes();
      io.run();

      // if the time is 00:00 then send the data to Adafruit IO
      if((hour == 0 && minutes == 0)){
        Serial.println(tiempo);
        Serial.println("Se envia a Adafruit");
        tiempo_feed->save(tiempo);
        tiempo = 0; // restart the counter
      }
      
    }
  }

  // if the time is 00:00 then send the data to Adafruit IO
  if(hour == 0 && minutes == 0){
    tiempo_feed->save(tiempo);
    tiempo = 0;
  }

  Serial.println(tiempo);
  delay(60000); 
  
}

int ping(int TriggerPin, int EchoPin) {
   long duration, distanceCm;
   
   digitalWrite(TriggerPin, LOW);  // 4us LOW pulse
   delayMicroseconds(4);
   digitalWrite(TriggerPin, HIGH);  // 10us HIGH pulse
   delayMicroseconds(10);
   digitalWrite(TriggerPin, LOW);
   
   duration = pulseIn(EchoPin, HIGH);  // mesure time between pulses, result in microseconds
   
   distanceCm = duration * 10 / 292/ 2;   // Change distance to centimeters
   return distanceCm;
}

Adafruit credentials files

C Header File
/************************ 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   "ADAFRUIT_IO_USERNAME"
#define IO_KEY        "IO_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 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  "NOMBRE_WIFI"
#define WIFI_PASS   "CLAVE_WIFI"

// 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
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.
Contact

Comments

Please log in or sign up to comment.