Send email at startup?

Is there an easy way to send out an email when a Flexy boots up? Obviously it would need to get a good WAN connection first. I wouldn’t want it to send every time the WAN connection drops and comes back though. Just on startup.

You could check the uptime using a script like mentioned in this post:

and send an email when it equals 120 seconds or so, for example, the timer may eventually roll over, but not for a long time. If this became an issue you could add a condition to check for that I suppose.

Seems a bit shoddy to me… Is there a system value I can read for system uptime?

Yes, it’s MSEC which is used in that code. It’s a read-only integer value.

Isn’t Msec the last scan time of the cyclic section though? Not some kind of system uptime clock?

It looked to me like that script is reading the last scan time of the cyclic section and adding it up manually.

Also is it really advised to run the code in the cyclic section as that user was doing?

No Tom, it’s not advised to run code in the cyclic section. Good call. I sent you the link to show you an example using the MSEC function, which represents the time since the device has booted.

Ok, so MSEC is actually the uptime, not the last scan time of the cyclic section. I should have read his code more closely.

So would something like this do the trick?

Tset 2, 5 //Get system uptime every 5 sec
ONTIMER 2, “@GET_UPTIME()”

FUNCTION GET_UPTIME()
UPTIME = GetSys Prg, “Msec”
ENDF

Tset 3, 10 //Check if up for 2 minutes yet
ONTIMER 3, “@CHECK_UPTIME()”

FUNCTION CHECK_UPTIME()
IF (UPTIME > 119000) AND (UPTIME <129000) THEN
SENDMAIL STUFF GOES HERE
ENDF

I figure the range of 119-129 will mean I only email once but I don’t have to worry about “catching” the MSEC value at exactly 120 seconds.

And since it’s millseconds I’d want 119k and 129k right?

That does look like it would work. The idea to do on a certain uptime was just the first thought that came to my head, but now I’m thinking it might end up being more complicated than it needs to be. It may actually work just to put the sendmail code in the Init section and set the BASIC to autorun because the Init section should only run once upon boot up. My only concern there is that the system won’t be ready to send the mail, so we’ll have to test it out and if it doesn’t work we could add a timer or code to retry.

Maybe I need a better understanding of the programming…

The init section only runs once on startup and never again? The code we did the other day for the alarming for Airgas had a timer that was supposed to run every 5 seconds. Will that happen?

Anyway, if init runs at startup couldn’t I just to a TSET 2, 120 and send the email once the timer is done? Will that timer repeat itself every 120 seconds or does it only run once?

Yes, the timer will keep repeating. Also, remember you would have to call the command with ONTIMER 2, SENDMAIL. To keep it from repeating, you can create another to turn it off:

TSET 3, 121
ONTIMER 3, “TSET 2, 0”

So does the init section only run once or not? I guess I don’t understand why my timer would repeat if init only runs once?

But yeah, it sounds like it would be simpler to just set like a 5 minute timer and sendmail, then have a second timer for maybe 6 minutes to disable the 5 minute timer.

Can I do an AND? Like

ONTIMER2 AND (BIT1=TRUE), SENDMAIL

ONTIMER 3, BIT1=TRUE

?

Yes, the init section only runs once after start up. The timer counts up to the number you specify ‘n’, and then restarts at zero, so you can use it to complete an action every ‘n’ seconds.

AND is a bit-wise operator in BASIC, so maybe use:

IF X THEN GOTO Y
ELSE GOTO Z
ENDIF

So could I just do one timer? Something like:

IF NOT X then GOTO Y
ELSE GOTO Z
ENDIF

Y
TSET 1, 120

ONTIMER 1, SENDMAIL, X=TRUE

Z

Tom,

I wouldn’t bother with the IF statements. If you want to send an email after the unit has been up for 5 minutes do this:

TSET 1, 300
ONTIMER 1, “SENDMAIL”
TSET 2, 360
ONTIMER 2, “TSET 1, 0”

Can you explain the line "ONTIMER 2, “TSET 1, 0” to me? Does the statement in quotes modify the original timer to have a preset of 0?

Yes, it sets the first timer to 0.

You can also check VPN status and wait for A$<>“0.0.0.0” before sending the email.

SETSYS INF,“load”
A$=GETSYS INF,“VPNIP”
IF (A$<>“0.0.0.0”) THEN
NETSTATUS$=“ONLINE” // ok to send email now
ENDIF

1 Like