[solved] Smart way to reset a variable every 24 hours?

I want to reset a variable for every new day.

I could make a simple.timer like

timer.setInterval(86400000, resetvalues);

void resetvalues()
{

  pulseCount24hour = 0;
}

And reboot the arduino at 0.00 hour.

But is it possible to use a more intelligent time/date function from blynk or anything like that?

or would it just complicate it alot?

1 Like

after googling, I can see that there is a ntp library called ntpclient

and that seems strait forward but then how do I work with the time afterwards?

the example sketch

#include <NTPClient.h>
// By default 'time.nist.gov' is used with 60 seconds update interval and
// no offset
NTPClient timeClient(ntpUDP);

// You can specify the time server pool and the offset, (in seconds)
// additionaly you can specify the update interval (in milliseconds).
// NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000);

void setup(){
  Serial.begin(115200);
  
  }

  timeClient.begin();
}

void loop() {
  timeClient.update();

  Serial.println(timeClient.getFormattedTime());

  delay(1000);
}

Or use the Blynk timer from the app, set it to midnight every night and trigger your reset :slight_smile:

1 Like

thx looking into the timer widget/code.

something like this?

Assign a timer widget to V0
Set a time when it has to be HIGH

#define timerwidget 0

void  setup()
{
Serial.begin(9600);
Blynk.begin(auth);
digitalWrite(timerwidget, LOW);
}


void loop()
{
  Blynk.run();
 

  if  (timerwidget == HIGH)
  {
     elapsedkWh24hour = 0;
     }
}

Not quite.

Check out the Blynk PushData example in your IDE. I would use that as a template (but make sure you set up the libraries correctly to your hardware).

Then you can see the way virtualPins work.

You need to put a BLYNK_WRITE(V0) function outside of the loop to catch the command from the app.

When the timer widget in your app triggers at midnight (or anytime you set it)… then the BLYNK_WRITE function above will be called once.

Inside that function you just need to set the global var to 0 or FALSE or what ever you need it to do.

I’ll give you an excerpt to insert in to the pushdata example.


int elapsedkWh24hour; // assuming your var is an int (change it to your own pref)

BLYNK_WRITE(V0){
  elapsedkWh24hour = 0;
}

Your loop() should never be more than Blynk.run() and timer.run() (if using simpletimer)

should’nt the V0 be set to LOW somewhere since the timer widget sets it to 1/HIGH when activated?

You’re only looking for a single trigger, right?

If you wanted to turn on a light, you would put this instead

BLYNK_WRITE(V0){
  if(param.asInt()){
    // turn on light because timer widget is HIGH or 1
    Serial.println("Timer is active");
  } else {
    // turn off light because timer widget is LOW or 0
    Serial.println("Timer is in-active");
  }
}

The timer simply sends a 1 or 0, because you dont need to know which is being sent, you can ignore the check.
You can then set your timer to turn on at 00:00:00 (midnight) and off at 00:00:01.
This way you kwh coutner will be reset at 00:00:01 each day.

thx for the explanation.

trying it on my powermeter project.

Check out my own Power Monitor project. Feel free to borrow the code or use it as study reference.

Interesting…

Im almost done with my Heatpump temp and power monitor.

Was just not happy with how the power is logged. Want a nice view of every 24 hour, also over weeks and months.

Thats why I want to log the power consumption for 24 hours and then reset the counter. then its easy to see the “max” for every day.

A heatpump dont have an even use of power over day, it can at one point almost use nothing and next it uses 1 kwh for 10 minutes.

1 Like

Might seem kind of silly, but using RTC in a sketch I’ve done something like:


void loop()
{
  Blynk.run();
  
  if (year() != 1970 && hour() == 0 && minute() == 0 && second() == 0){
    pulseCount24hour = 0;
  }
}

Not silly, this is how I do it too! :slight_smile:

dont have a RTC, but now im wanting one :slight_smile:

You could just use the RTC widget! Hardware RTC is soooo old-skool and inaccurate!

Man… didnt know it existed

1 Like

Look through the widget examples in the IDE! Loads of great things to try out! :wink:

even though the timer works, it could be cool to get the RTC to work too.

But it seems I just cant get it to reset the variable.

Hint on what I have done wrong?

I have left out some of the Blynk connection stuff in below.

#include <TimeLib.h>
#include <WidgetRTC.h>

WidgetRTC rtc;

long pulseCount24hour = 0;  

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);

  while (Blynk.connect() == false) 
     {
     // Wait until connected
     }
  rtc.begin();
}

void loop()
{
  Blynk.run();
  timer.run();
  

  if (hour() == 23 && minute() == 10 && second() == 0)
  {
    pulseCount24hour = 0;
  }

}

You must first check the time is actually correct and not showing Jan 1 1970 (which is does do often within the first 5min of boot).

You can update quicker by adding setSyncInterval(60); right after rtc.brgin(); … this will mean that your time is resync’d every minute instead of 5min

Check the time too by outputting to serial or terminal. Or add it to a virtual port.

#include <SimpleTimer.h>

SimpleTimer timer;

void setup(){ 
  timer.setInterval(1000,showCurrentTime);
}

void showCurrentTime(){
  String CurrentDate = String(day()) + '-' + monthShortStr(month()) + '-' + year();
  String CurrentTime = String(hour()) + ':' + minute() + ':' + second();
  String formattedDate = CurrentDate + String(" | ") + CurrentTime;
  Blynk.virtualWrite(V1,formattedDate);
}

void loop(){
  timer.run();
}

Think you’re right.

I only gave it test periods of 2-3 minutes, and then back to arduino ide to figure out why…

the problem I have to dismount, update with usb, mount again and test.

I’ll give a new try with more time and a serial clock to test with.

It might be time to look into OTA, so I can update across lan :slight_smile: