Want AGENT to act if it does NOT hear from IMP in x minutes

Hi,

I would like my agent code to generate an email (via mailgun) if it does not hear from the IMP in a set time period.
I currently have the IMP sending data to the agent every 5 minutes. If the IMP goes down or loses power I would like the AGENT to generate an email alert to that effect.

So it seems I need to start a timer in the AGENT that the IMP resets periodically. If the IMP fails to reset the timer then the AGENT would go ahead and generate the email.

this is my agent code that generates the email when the IMP properly checks in to the AGENT…

device.on("mailgun", function(data) { send_mailgun_email(data); });

so how can I have the AGENT keep track of time and generate an email when it does NOT hear from the IMP??

thanks for any guidance.

dwight

imp.wakeup() is listed as available in both the Imp and the Agent. so i guess you could have a timer that you clear and set again every time you hear from the imp and/or when its connected and take action when the timer function is called

I wrote a bit about the function I made for when I check if the device is online.

All you would have to do, is to add this to yours, and run a timer on the agent which checks the function.

Another, less responsive but easier to implement, method would be to use device.ondisconnect(). See this link for more detail:
https://electricimp.com/docs/api/device/ondisconnected/

It seems to take circa 5 minutes to detect the loss of connection.

Side note: curious to know why the url is device/ondisconnected while the method is device.ondisconnect. Perhaps there’s been an internal debate at Electric Imp over semantics for events and states. :slight_smile:

Here’s a really quick snippet for you to work with:

Device Code:
`const INTERVAL = 60; // checkin every 60 seconds

// send a “stillhere” message every INTERVAL seconds
function watchdog() {
imp.wakeup(INTERVAL, watchdog);
agent.send(“stillhere”, null);
}

// start the watchdog
watchdog();`

Agent Code:
`const INTERVAL = 5; // needs to be same as device
const MISSED_MESSAGES_BEFORE_TRIGGERING = 2; // must be >= 2

watchdogTimer <- null;
missedMessages <- 0;

function watchdog() {
// increment missed message counter
missedMessages++;

// if we've missed too many messages
if (missedMessages > MISSED_MESSAGES_BEFORE_TRIGGERING) {
    // execute your function here:
    server.log("SENDING EMAIL");
} else {
    // scheudle next watchdog
    watchdogTimer = imp.wakeup(INTERVAL, watchdog);
}

}

device.on(“stillhere”, function(nulldata) {
// clear the watchdog timer if it exists
if (watchdogTimer != null) {
imp.cancelwakeup(watchdogTimer);
watchdogTimer = null;
}
// clear missedMessages
missedMessages = 0;
// reschedule the watchdog timer
watchdogTimer = imp.wakeup(INTERVAL, watchdog);
});`

The device code is pretty simple - it just checks in every seconds (or however often you want… probably much longer than that).

The agent code runs a watchdog function every 5 seconds that increments a counter. When the counter exceeds a certain number (in this case 2) it triggers the email function (you need to fill that part in, I’m just logging right now).

Whenever the agent gets one of the messages from the device, it resets the counter, clears the current timer, and creates a new one.

Since we’re checking in every 5 seconds, server.log(“SENDING EMAIL”); will execute after the device has failed to checkin for 10 seconds (2 missed checkins).

This is also setup so that the “SENDING EMAIL” will only happen the FIRST time the device goes offline (it needs to come back online before the watchdog restarts… although that is a fairly easy change if you don’t want that behaviour).

Let me know if you have any questions :slight_smile:

thank you all for good suggestions…
now to give them a go.
I’m sure there is something here I can make work.

dwight

so I tried to just use a simple
device.ondisconnect
to send an email but it was sending me an email too often. As if the IMP was going offline for just a moment and then back on line.

So I want to implement a timer of say 5 minutes to eliminate false “disconnected” emails.

but I am getting an error in the log that says…
[Agent] ERROR: bad parameters to device.onconnect(callback)

Below is my code…
Agent Code:
`device.ondisconnect(function()
{
server.log(“imp disconnected”)
discon_flag = 1
})

function is_imp_disconnected()
{
if (discon_flag == 1)
{
send_mailgun_email(“Off Line”)
discon_flag = 2;
server.log(“offline email sent”)
}
imp.wakeup(300, is_imp_disconnected);
}

device.onconnect(function(){
server.log(“Device connected to agent”)
if (discon_flag == 2)
{
send_mailgun_emial(“Back On Line”)
}}
discon_flag = 0
)`

What I am trying to do is set a flag “discon_flag” when the imp initially goes off line and then increment the flag (to 2) if the imp does not come back on line withing 5 minutes and then test the flag, and send an email if the flag=2
or clear the flag if the IMP comes back on line within 5 minutes.

Can anyone please help me understand why I am getting the error in the log??
[Agent] ERROR: bad parameters to device.onconnect(callback)

You have misplaced the closing brace for you callback function. The line di d con_flag = 0 is not in the function definition. The code should look like:
`device.ondisconnect(function()
{
server.log(“imp disconnected”)
discon_flag = 1
})

function is_imp_disconnected()
{
if (discon_flag == 1)
{
send_mailgun_email(“Off Line”)
discon_flag = 2;
server.log(“offline email sent”)

imp.wakeup(300, is_imp_disconnected);
}

device.onconnect(function(){
server.log(“Device connected to agent”)
if (discon_flag == 2)
{
send_mailgun_emial(“Back On Line”)
}
discon_flag = 0
}
)`

Thanks gsunada… yep those brackets are really a challenge for an old BASIC coder…

Here is what I have come up with, and it seems to be working quite well.
It dosen’t require any code on the Device side.
It has about a 10 minute window(or what ever you choose for the imp.wakeup) to allow the IMP to fail and come back on line without bothering me with excessive nuisance email.

I just want to be notified if the IMP really goes off line for long periods of time, which would indicate a power failure or wifi outage, etc.

Does anybody know why IMP’s randomly disconnect and reconnect for no apparent reason???

Agent Code:
`
discon_flag <- 0 //clear the flag used for tracking IMP connection status

//-------------------------------------------
device.ondisconnect(function()
{
server.log(“imp disconnected”)
discon_flag = 1 //IMP disconnected
imp.wakeup(600, is_imp_disconnected) //set timer to recheck IMP status
})
//-------------------------------------------
function is_imp_disconnected()
{
if (discon_flag == 1) //indicates IMP still disconnected
{
send_mailgun_email(“Off Line”)
discon_flag = 2 //indicates “off line” email sent
server.log(“offline email sent”)
}
}
//-------------------------------------------
device.onconnect(function()
{
server.log(“Device connected to agent”);
if (discon_flag == 2) //only send “back on line” email if needed
{
send_mailgun_email(“Back On Line”)
}
discon_flag = 0 //clear flag as IMP is back on line
})
//-------------------------------------------
is_imp_disconnected()
`

thanks to all for good suggestions and help.
dwight