ALARM CLOCK: Integrate RTC Widget with TimeAlarm library

My first post and just want to say Blynk is awesome!

I’m using an ESP8266 to control a sprinkler system on a timer. I’m using the RTC widget instead of a physical RTC to track real time and I want to use the TimeAlarm library to trigger sprinkler events at specific times.

The problem is in my setup:

/******** Connect Blynk ***********/
  Blynk.begin(auth, xxx, xxx);
  while (Blynk.connect() == false) 
  {}// Wait until connected
  rtc.begin();

 // Rear Sprinkler Times
  SprinkAlarm1 = Alarm.alarmRepeat(7,50,0, sprinkler1); 
  SprinkAlarm1 = Alarm.alarmRepeat(21,00,00, sprinkler1);
 // Front Sprinkler Times
  SprinkAlarm2 = Alarm.alarmRepeat(6,30,00, sprinkler2);
  SprinkAlarm2 = Alarm.alarmRepeat(20,30,00, sprinkler2);

As it is, the Sprinklers do not trigger. If I get rid of the rtc.begin and replace with a constant:

setTime(7, 49, 0, 3, 3, 2016);  // any date works

Then the alarm works

If I change to

rtc.begin();
setTime(hour(), minute(), second(), 4, 4, 2016); // any date works

it triggers all four alarms at startup (which is not what I want!), but then it triggers the proper alarm at the proper time.

If I change the set time to

setTime(hour(), minute(), second(), day(), month(), year());

or

setTime(now());

The alarm doesn’t work.

I can work around it by putting an if statement in the loop section:

if (hour() == 8 && minute() == 2 && second() == 30)
 {sprinkler1();}

But I’d rather declare the alarms at the setup and have it work

Any help is very much appreciated! Thanks

It looks like the time doesn’t sync with Blynk until the loop. Maybe that’s the problem. How can I get “time sync: OK” at the setup? Tried putting in Blynk.run() at setup but that didn’t work

Try Blynk.connect() first. but this still doesn’t guarantee the time is updated.
You can hack into WidgetRTC code to do that…

Thanks for the reply, blynk.connect doesn’t work. Been trying to wrap my head around library syntax looking at WidgetRTC.h, but it’s tricking me.

I know that the rtc.begin section needs to somehow include the code from the last function in the library, when it writes “time sync: OK”
I added the stuff below, but it’s not working clearly because library editing is new to me. Gotta start somewhere!

inline
void WidgetRTC::begin()
{
    setSyncProvider(WidgetRTC_impl::requestTimeSync);  //original Line

 // I added the stuff below, basically copied from the WidgetRTC::onWrite Function:
    Blynk.syncVirtual(WidgetRTC::mPin);
    BlynkReq& request;
    const BlynkParam& param;
    const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
    unsigned long blynkTime = param.asLong();
    if ( blynkTime >= DEFAULT_TIME) {   // Check the integer is a valid time (greater than Jan 1 2013)
        setTime(blynkTime);             // Sync Time library clock to the value received from Blynk
        BLYNK_LOG1(BLYNK_F("Time sync: OK"));
    }
 
}

Thanks for any help! It would be cool to be able to hard-code set alarms into a program like this

I ended up recoding the timed events using the timer widget, but the main limitation I’m finding is that the alarm only triggers when the app is running on my phone. If it’s not running, then the alarm doesn’t trigger. I need my irrigation to be dependable, does anyone have any suggestions?
This is really just a question about implementing a simple alarm clock that can run when the blynk app isn’t active
Thanks

the app runs by pressing teh play button on the app, not buy having your phone on.

it makes it run in the server, not on your phone.

if you turn your phone off it still runs, if it doesn’t then something is broken…

Oh ok well thanks, that’s good to know.
It would still be nice to know how to update the RTC library to accommodate the TimeAlarm library. It would easily allow alarms to be set on a specific day of the week, time of day, etc, embedded in the code.
Anyone out there willing to help guide me through the RTC library edits?

did you solve the problem? i woull also like the RTC library to accommodate the TimeAlarm library but unfortunately my programming skills are limited.

@Polikarpos have you looked at the Time Input widget as it was released after the post about scheduling made by @kcg?

to be honest after posting that message i found out that rtc widget along with Timer widget work fine.
i ve seen an older post where someone asked about the possiblity of a calendar to go along with RTC.for now i can only set the timer to activate in a specific time of day…i would like to set it in a different time every different day…have you done anything about that? thanx for the quick response…BLYNK is just awesome

@Polikarpos I have done a fair bit with Time Input widget.

You can have 7 widgets one for each day all with different times that you can change at will.

Or just use one and keep changing the times each day.

the second suggestion wont do for me…i need to set the time once
i ll play with the time input widget and see what happens…using so many widgets seems a waste of resources for just one timer …writing the necessary code is an option but i would like to find a more user friendly method via BLYNK and its timers…i hope you understand my point

The library is open-source and you can do ANYTHING with it.
Any contributions are welcome.