Nag Mode

MyDoorOpener custom/homebrew "Arduino" backend enhancements sharing and discussions.

Nag Mode

Postby cmot » Sun Jan 11, 2015 11:17 pm

Hi all

I actually want my garage door to nag me when it is open for too long - that is, not only do I want a watchdog notification after some period of time, I want the system to keep bugging me over some interval until I close the garage door. Might be annoying now and then, but leaving our door open overnight would be potentially very bad...

So here is some code that seems to be working nicely:

Code: Select all
// new "nag" code to implement repeat notifications
// if defined, will fire a repeating notification when any door/device is still open for more than the specified number of minutes
#define NOTIFICATIONS_NAG_INTERVAL  15 //NEW
...
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
#if defined(NOTIFICATIONS_WATCHDOG_MINUTES)

//----------------------------------------------------------------------------------------------------
void watchDogNotificationsHandler()
{
  static time_t initialOpen = NULL;
  static time_t latestNotification = NULL; //NEW
  time_t latestOpen = NULL;

  static boolean notificationSent = false;
  boolean openDetected = false;
 
  for (int i = 0; i < sizeof(statusPins); ++i)
  {
    if (isOpen(statusPins[i]))
    {
      if (!initialOpen)
        initialOpen = now();

      latestOpen = now();

      if((latestOpen - initialOpen) > NOTIFICATIONS_WATCHDOG_MINUTES * 60)
      {
        #if defined(NOTIFICATIONS_SERIAL_DEBUGGING)
          Serial.print(F("*** watchdog notification handler - detected opened device/door @ pin #"));
          Serial.print(statusPins[i]);
          Serial.println(F(" ***"));
        #endif
       
        if (!notificationSent)
        {
          #if defined(NOTIFICATIONS_SERIAL_DEBUGGING)
            Serial.println(F("*** watchdog notification handler - sending notification ***"));
          #endif

          char subject[] = "wants to tell you that";
          char body[100] = "";
          sprintf(body, "the garage has been open for more than %i minute(s)!", NOTIFICATIONS_WATCHDOG_MINUTES);

          #if defined(PUSH_NOTIFICATIONS)
            notifyViaPush(subject, body);
          #endif

          #if defined(SMS_NOTIFICATIONS)
            notifyViaSms(subject, body);
          #endif

          #if defined(SMTP_NOTIFICATIONS)
            notifyViaEmail(subject, body);
          #endif

          notificationSent = true;
          latestNotification = now(); //NEW: Get the time the most recent notification was sent
        }
        else
        {
          #if defined(NOTIFICATIONS_SERIAL_DEBUGGING)
            Serial.println(F("*** watchdog notification handler - NOT sending notification ***"));
          #endif
         
         
          //NEW: Checks where sufficient time has passed to nag, if so, then get the loop to send another notification
          //Loop above will run fine because (latestOpen - initialOpen) is always greater than WATCHDOG_MINUTES
          //if the door has remained open
          #if defined(NOTIFICATIONS_NAG_INTERVAL)
            if((latestOpen - latestNotification) > NOTIFICATIONS_NAG_INTERVAL * 60)
            {
              notificationSent = false;
            }
          #endif
        }
      }

      openDetected = true;
      break;
    }
  }

  // if all door/devices are closed, reset notificationSent semaphore and timer so that further notifications
  // can be sent. This is done to avoid floading recipient with notifications for a same event occurrence.

  if (!openDetected)
  {
    notificationSent = false;
    initialOpen = NULL;
    latestNotification = NULL; //NEW
    delay(500);
  }
}

#endif



Your mileage may vary.

Best, Mike
cmot
 
Posts: 8
Joined: Wed Jan 07, 2015 11:36 pm

Re: Nag Mode

Postby support » Mon Jan 12, 2015 10:10 am

Thanks for sharing Mike. I moved your post to the "Arduino Backend Enhancements" forum.
support
Site Admin
 
Posts: 384
Joined: Thu Aug 22, 2013 7:30 pm


Return to Arduino Backend Enhancements



cron