• Hardware model: ESP8266-01 + communication type: Wifi.
• Smartphone OS (iOS or Android): Android + version: lolipop
• Blynk server or local server: Local sure
• Blynk Library version:0.5.4
Hi everybody;
I need an idea to mesure my relay stay open period daily.
Which one is viser?
Using “timelib” to calculate minute, hour, second from start to stop? for example;
Vich ever one you are more comfortable coding with… time is time… translating to your preferred feedback is all in how you program the output. But, using a time library usually has much of the day, hour, minute coding already done for you, while millis() is like making CPU’s from sand… lots of fiddly steps.
Then if you want a result that looks like this… calculating negative numbers is not the answer, a normal time library (and possibly some simple math) that accounts for the day of week (if it changed during the start/stop time) will do the job.
weekday(); // Day of the week (1-7), Sunday is day 1
But then you use an if() function so that if weekday at start i.e. Tuesday (3) == weekday at end, do nothing. Else if ! i.e. Wednesday (4) take answer (-22) and add 24 = 2… duration formula gives you “2”
Probably a mute point since the OP question is resolved… but out of my own curiosity…
While processing now() at the start and subtracting it from now() at the finish of an action should result in the proper elapsed time… but only in a UNIX time format (time as seconds since Jan 1 1970), so one still has to convert to a standard readable time format… I personally haven’t found a simple Arduino code that does that… and why try when the time libraries do all that for us.
So using the built in weekday(), hour(), minute(), second(), and a little simple math does the same thing.
Unless you have a code snippet using now() that will give a readable time duration between events? That might be good to add in the coding toolbox.
Dear @Gunner,
see the bellow and adjust it according your needs:
Psudo code:
startTime=now(); /// time_t
...
endTime=now(); /// time_t
durationTime=endTime-startTime; /// durationTime should be global time_t
int2Time(durationTime);
terminal.printf(" end @ %02d:%02d:%02d \n",hour(endTime), minute(endTime), second(endTime)); /// if you want endTime in human like format...
terminal.flush();
...
/// START of int2Time convert integer to time format ///
void int2Time(time_t duration)
{
if(duration >= 86400){ /// 86400
terminal.printf("%dD:", (duration / 86400)); /// how many days ? ///
duration = duration % 86400;
}
if(duration >= 3600){ /// 3600
terminal.printf("%02dH:",(duration / 3600)); /// how many hours ? ///
duration = duration % 3600;
}
if(duration >= 60){ /// 60
terminal.printf("%02dM:",(duration / 60)); /// how many minutes ? ///
duration = duration % 60;
}
terminal.printf("%02dS",(duration)); /// how many seconds ? ///
terminal.flush();
}
/// END of int2Time convert integer to time format ///
Yes, thanks… it is a very similar process to what I discovered earlier with the millis() translation I posted about above.
I was trying to figure out how to get days… and now I think what you did will work the same way for the Timelibrary/RTC free millis() option as well.
Makes sense now… seconds from Jan 1, 1970 or seconds from MCU boot is still just a lot of seconds that can be broken down into readable portions… doh
I am still learning the power of sprintf (and now how to use it with terminal ) Arithmetic Operators like % (remainder, not percent ) and waaayy too much about the necessity of my nemesis… math