Count door opening + elapsed time opened sum each day

NodeMCU on Wifi

Hi mates!
I already have a project that measures my cold chambers temperature by I want to add a feature that gives me the openings during day and how many minutes the door remains opened (sum of all openings). I don’t even know what libraries to use and how to start coding these new features. I don’t want any buttons to reset. I want the reset to occur automaticaly at 00:00 each day and the #s and minutes to be stored in a graph.

I think of an algorithm like this (input: limit switch: opened 1, closed 0)

when day starts (00:01), counter = 0, elapsed = 0

void loop
{
if switch = 1; counter++

if switch = 1, 
    starts chrono
when switch = 0,
    stops chrono
elapsed = elapsed + chrono
}

Any help?

Many thanks!!
– Rodrigo

You can use two counters. one to count the number of opens and one to count the number of closes, but not in the void loop. You should read this
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

You can use the millis() function to calculate the elapsed time after any open/close event. Or you can use an RTC module or NTP client library to track time and date instead.

That’s so easy. You can use the RTC module or NTP client to track time and perform the reset at 00:00:00 every day.

1 Like

I think I’d use global variables to track the following:

  • Count of door openings
  • Previous door state (open/closed)
  • Elapsed door open time in seconds

I’d then use a BlynkTimer to call a function once every second.
That timed function would do the following…

If door is closed, set the “Previous door state” variable to closed (0)

If the door is open then:

  • If the “Previous door state” variable was closed (0) the add one to the “Count of door openings” variable (because this is a new door open event) and set the “Previous door state” variable to open(1) so that the next time this timed event runs in 1 second and the door still open it’s not counted as a new door open event.
  • Increment the “Elapsed door open time in seconds” timer by 1
  • Send updated values for “Count of door openings” and “Elapsed door open time in seconds” to Blynk

As far as the reset to zero at midnight part is concerned, I’d use a “Schedule” Automation to trigger a virtual pin at midnight every day. The BLYNK_WRITE(vPin) handler for this virtual pin would set the “Count of door openings” and “Elapsed door open time in seconds” variables to zero. I’d also set the “Previous door state” variable to closed (0) so that if the door is open when the counters are reset a new door open event is immediately triggered and the elapsed time counted.

Pete.

Gentlemen, many thanks!
I’ll come back in some days

Thanks!
-Rodrigo

I’m happy to share my little project with you!
For now, only “[variable] 1” are implemented. “Aberturas” means “openings” and it’s reseting to 0 by 10pm. Working flawlessly!

I’m still strugling with the elapsed time feature: couldn’t figure out how would be the code without flooding blynk server.

Thanks!!
Rodrigo

PS: understanding the graph: the compressor turns ON, that’s when curve starts to fall, removing heat from the cold chamber. After a given time interval, it shuts OFF and the heat resistance turns ON, to prevent forming ice and blocking the evaporator. that’s why air temperature rises so fast. After 30 minutes, the cycle restarts. And it goes on and on and on, working nonstop for the past 24 years in my company

You wont flood the server if you use timers as I suggested previously. At most you’ll be sending two pieces of data to Blynk per second…

and then this data would only be sent when the door is actually open.

If you posted your entire current sketch we could point you in the right direction.

Pete.

Hi Pete!

Code (credentials hidden):

/* TIMER */
BlynkTimer timer;
BlynkTimer timer2;

/* DS18B20 Temperature Sensor */
#include <OneWire.h>
#include <DallasTemperature.h> 
#define ONE_WIRE_BUS 12 // DS18B20 on arduino pin2 corresponds to D4 on physical board
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
float temp;

/*TRECHO CONTADOR DE ABERTURAS*/
const char BUTTON_PIN = 4;
int counter = 0;
int prestate = 0;

void read_button() 
{
bool currentState = digitalRead(BUTTON_PIN);
 
  if (currentState == HIGH && prestate == 0)
  {
    counter++;
    Serial.print("Counter = ");
    Serial.println(counter);
    Blynk.virtualWrite(4, counter);
    prestate = 1;
  }
  else if (currentState == LOW)
  {
    prestate = 0;
  }
}
/*FIM TRECHO CONTADOR ABERTURAS*/

void setup() 
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  DS18B20.begin();
  timer.setInterval(1000L, getSendData);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  timer2.setInterval(500L, read_button);
}

void loop() 
{
 timer.run(); // Initiates SimpleTimer
 timer2.run();
 Blynk.run();
}

/***************************************************
 * Send Sensor data to Blynk
 **************************************************/
void getSendData()
{
      DS18B20.requestTemperatures(); 
      temp = DS18B20.getTempCByIndex(0);
      Serial.println(temp);
      Blynk.virtualWrite(10, temp); //virtual pin V10
}

It’s better to leave those lines of code in, and replace the sensitive info with the word “REDACTED”.

You should delete this…

and this…

and replace this…

with this…
timer.setInterval(500L, read_button);

because each timer object can support up to 16 timers.

Pete.

To add-in a counter for the accumulated time in seconds that the door has been open during the day, I’d make these changes as well as those listed above…

timer.setInterval(1000L, read_button); // This timer in vopid setup needs to run once per second for the seconds counter to work

long int elapsed_time_secs;


void read_button() 
{
bool currentState = digitalRead(BUTTON_PIN);
 
  if (currentState)
  {
    // we get here if the door is open
    elapsed_time_secs ++; // add 1 to the total number of seconds that the door has been open
    Serial.print("Door has been open for = ");
    Serial.print(elapsed_time_secs);
    Serial.println(" seconds");    
    //Blynk.virtualWrite(??, elapsed_time_secs); // choose a virtual pin for this
    
    // If you wanted an indicator of the door's current state (LED widget maybe)
    // you could do a Blynk.virtualWrite(vPin,1) in here
     
    if(prestate == 0)
    {
      // we get here if the door is open, but 1 second ago it was closed
      counter++; // add 1 to the total number of times that the door has been opened
      Serial.print("Door has been opened = ");
      Serial.print(counter);
      Serial.println(" times");
      Blynk.virtualWrite(4, counter);
      prestate = 1;
    }
  }
  else
  {
    // we get here if the door is closed
    prestate = 0;

    // If youre adding the current state indicator LED then you'd need a
    // Blynk.virtualWrite(vPin,0) in here.
  }
}

It’s not clear how you are resetting your door opening counter to zero at 10pm, but you’d also need to do the same for the elapsed_time_secs as well.

In the comments I’ve suggested a widget (an LED maybe) that shows you the current state of the door. If this is something you’d find useful then its easy to implement.

Pete.