Set and read virtual(?)

Hi

I’m new to this Blynk thing. I have a NodeMCU v3 which i’m using to build my project on. It’s ESP8266 and I’ve got everything up and running. For now I send data to ThingSpeak but this might change to Blynk as I get to know it better. My issue with the Arduino IDE is getting a variable (rain count) to reset daily at midnight.
I installed Blynk to be able to use the app’s scheduler where it can set a certain pin value at a certain time. The idea is to set a pin value to 0 in the Arduiono IDE setup. The app should change that value to 1 at midnight. Then the loop should check the pin value and reset the rain count when pin is 1. Afterwards the pin too should be reset to 0 and await next input from app some 24h later.
Unfortunately I’m in a bit of a hurry so I really welcome constructive examples a lot!
I think it’s simple but I just can’t get it working!

Kind regards,
Christina

you need to compare hour() to zero or currentTime to 0:00:00 and then reset the rain count

String currentTime= hour() + ":"  + minute() + ":" + second();

Whilst you’ll get away with that approach in many situations, it’s not the right approach in Blynk.
The correct approach is to use a timer to chech the pinvalue every x milliseconds, or use an interrupt on the pin. If you’re using s tipping rain gauge then the interrupt is the only viable approach and I’ve found that the reed switches in these rain gauges need some denounce code to ensure that you only get one interrupts pulse per tip of the bucket.

The problem with comparing the hour to zero is that it will continue to reset the counter to zero until 1:00am unless you also use a flag to indicate that the reset has taken place, which is reset when hour is no longer equal to zero.
Checking for time equals 00:00:00 is a better option, but the test needs to occur during a one second window for it to be true, so take care how you structure the timer that does that check.

I think you’ll need to include the RTC widget in your project to be able to check the current time in your code (not 100% sure on this, I don’t use Blynk in that way myself).

Pete.

you are right Pete

you need also to have some flags to test if hour()=0, is the first time.
it’s the same for currentime to avoid occur
I agree, there are many best solutions

Thank you for your kind replies.

In short:
I was told not to use millis as they drift quite a bit in just a day and that will of course accumulate even more over time.
Also time should be quite difficult to compare with if’ish statements.

At some point i was thinking about just if’ing hours and minutes as I upload data from sensor every 50 seconds. If it resets twice in a short while every now and then, it’s no problem.

I’d like something:

if hours = 00
if minutes = 00
variable = 0
else nothing

Really? Is your MCU steam powered or horse drawn? :grin:

Very constructive. But really, yeah, Gunner.
Also if something happens I’d have to reboot the MCU exactly at midnight to make it reset at that exact time in the future.

What is even more constructive then asking for rush help, without showing your own work, is searching and reading all the other “how do I do something same time every day” type projects… there are so many ways.

I use the simple Timer Widget to toggle a vPin two times a day (ON and OFF) and have a BlynkTimer checking every 5 mins to determine the value of said vPin, then act acordingly. Works like clockwork :stuck_out_tongue:

BLYNK_WRITE(vPin) {  // RGB lighting flag - toggled from Timer Widget
  RGBpower = param.asInt();
}
void someOtherTimedLoop() {
  if (RGBpower == 1) {  // Run RGB lighting if flag == 1
  // do stuff... 
  } else {
  // don't do stuff
  }
}

You could try something similar with START a few seconds to midnight and STOP at midnight.

At START you run an interval timer (Blynk timer based) every few ms or so to check for the STOP then reset your counter and delete the interval timer until the next time.

I agree, but not because of the very slight drift you’ll experience over time.
Millis uses an unsigned long variable type, which means that it resets to zero after around 49.7 days. This makes it very difficult to use in a scenario like yours. RTC is a much better approach in my opinion.
You will of course get a 25 hour and a 23 hour day day once per year, when the clocks change from summertime to wintertime, but I don’t think that’s going to cause you too many issues.

You may also want to think about the usefulness of simply knowing rainfall in the past 24 hours. Sure, it’s something that the weather forecasters use, but you may find it more useful to know how much rain fell per hour, then keep a rolling total of the last 24 hours.

I actually monitor rainfall in the past minute, 60 minutes and 24 hours. In the case of the 60 minute and 24 figures, these are rolling periods rather than periods defined by the clock.
There’s a more detailed discussion on the issue in this thread:

Pete.