Alarm Script Recipes


 

Bryan Mayland

TVWBB Hall of Fame
The information in this post is outdated please refer to the Alarm Script Recipes wiki page for up-to-date information.

As of last weekend, LinkMeter has the ability to fire off user scripts when an alarm goes off. This is in addition to beeping the piezo buzzer. While I get the documentation together, I figured I'd start with some script recipes and information on how it works. Note this requires some degree of linux shell scripting expertise, but using some of these recipes might make things a little easier to understand.

First of all, alarm scripts are located in /usr/share/linkmeter/. Do not edit the script named "alarm", as this file will be replaced every time linkmeter is upgraded and you will lose all changes. Instead, create new files for your scripts. The system looks for a file named alarm-{probeidx}{alarmtype}. For example if the Pit probe high alarm is going off, it will look for alarm-0H. If this file is not found, then a generic alarm-all is called. These files can be a shell scripts, lua scripts, or a regular ARM ELF binaries. These recipes can be used for either an alarm-specific or alarm-all file. The files must be executable, i.e. `chmod +x` or they will not be run.

There are many variables available to use in your scripts. From a command prompt execute `lmclient LMCF` for a list. In addition, the alarm system adds al_probe, al_type, al_thresh variables.

NOTE: msmtp is only available on RasberryPi firmware due to size limitations.

Common Code
Most recipes are going to start with the same code. #1 turn the alarm off. #2 get the name and current value of the probe that's causing the alarm. All the recipes below start with this common code.
Code:
#!/bin/sh

# Silence all alarms
lmclient LMST,al,0,0,0,0,0,0,0,0

# Get current ringing name and temperature
PN=`eval echo \\$pn${al_probe}`
PCUR=`eval echo \\$pcurr${al_probe}`

Sending an Email
This requires that /etc/msmtprc be configured with the proper email server information. Note that many ISPs will block regular email traffic (on port 25) so you'll probably need to use SMTP over SSL on port 465.
Code:
sendmail bmayland@myemail.net << EOF
From: Bryan Mayland <bmayland@myemail.net>
To: Bryan Mayland <bmayland@myemail.net>
Subject: [HM] $PN Alert

Alarm $al_type outside threshold $al_thresh, currently $PCUR.

$pn0: $pcurr0
$pn1: $pcurr1
$pn2: $pcurr2
$pn3: $pcurr3

--
http://your.heatermeter.address/luci/lm/
EOF

SMS Message
SMS relies on going through your cell provider's email to SMS gateway so, again, the /etc/msmtprc must be configured for proper email delivery.
Code:
echo "$PN Alert -- $PCUR \($al_type\)" | sendmail 2125551212@messaging.sprintpcs.com

HeaterMeter Control - Shutdown
Often you want to turn off the Pit when your food is done. This is easily done using lmclient to send a command to HeaterMeter to change the setpoint.
Code:
# Lower the setpoint to 100
lmclient LMST,sp,100

HeaterMeter Control - Ramp Down
You can do more complicated setpoint control with a ramp down script.
Code:
case $al_thresh in
  180) NEWSP=215; NEWAL=190; ;;
  190) NEWSP=205; NEWAL=200; ;;
  200) NEWSP=100; NEWAL=0; ;;
esac

lmclient LMST,sp,$NEWSP
# Assumes food probe 1 is the alarm generator
lmclient LMST,al,,,$NEWAL

HeaterMeter Control - Super Ramp Down
10 degrees too much? That's ok you can ramp down one degree for every degree the meat climbs if you like
Code:
if [ "$al_thresh" -gt 199 ] ; then
  # done
  lmclient LMST,sp,100
  exit
fi

NEWSP=$((sp-1)
NEWAL=$((al_thresh+1))
lmclient LMST,sp,$NEWSP
# Assumes food probe 1 is the alarm generator
lmclient LMST,al,,,$NEWAL

Time Delays
Often you may not want be notified when the alarm happens but some time after it happens. Cron can be used. Note cron only has per-minute resolution so don't try to make things happen every N seconds.
Code:
# If no parameter, this is a regular alarm
if [ -z "$1" ] ; then
  NOW=`date +%s`
  # Set target for 1 hour from now 3600=seconds
  WHEN=$((NOW+3600))

  TARGET=`date -D "%s" -d $WHEN +"%M %H %d %m"`
  echo "$TARGET * /usr/share/linkmeter/alarm-all RING${al_probe}" | crontab -
else
  # This is the cron callback
  # Don't fire again
  crontab -r

  # Do whatever you want here, using another recipe
fi
 
Last edited:
Yeah if you can figure out a way to do it with a web request, wget is available on the WRT54GL. I just can't fit msmtp into the image because it needs OpenSSL which is pretty big and there's not a whole lot of space on the Linksys.
 
Ah, easy enough then. I'll just toss a php script up on my website that I can get with an ultra-secure password parameter that pages my phone.
 
Willing to share that? I'm not ready to upgrade to Pi yet and would love some sort of alarm while away.

I haven't actually implemented this yet, but here's 30s of fiddling. On some webserver somewhere, you'd toss up a script called alert.php that contained something like:

Code:
<?

$message = <<< EOM
Alarm $_REQUEST[al_type] outside threshold $_REQUEST[al_thresh], currently $_REQUEST[PCUR].

$_REQUEST[pn0]: $_REQUEST[pcurr0]
$_REQUEST[pn1]: $_REQUEST[pcurr1]
$_REQUEST[pn2]: $_REQUEST[pcurr2]
$_REQUEST[pn3]: $_REQUEST[pcurr3]

--
http://192.168.2.12/luci/lm/
EOM;

if ($_REQUEST['p'] == 'ef665e18e39057297a7ababb4325882a')
    mail('your@emailhere.com', "[HM] $_REQUEST[PN] Alert", $message);

?>

Note: your@emailhere.com could very well be your phone's sms alert email, etc.

Then, on the HM side, your alarm script might look something like:

Code:
wget http://yoururl.com/alert.php\?p=ef665e18e39057297a7ababb4325882a\&al_type=$al_type\&al_thres=$al_thresh\&PCUR=$PCUR\&PN=$PN\&pn0=$pn0\&pcurr0=$pcurr0\&pn0=$pn0\&pcurr0=$pcurr0\&pn1=$pn1\&pcurr2=$pcurr2\&pn3=$pn3\&pcurr4=$pcurr4

This is obviously very rough around the edges and totally untested, but based on my understanding of how the HM alarm stuff works, it might work. :)
 
Last edited:
If I understand this correctly the alarms and alarm setpoints are set via files that are downloaded. Is this correct? Can the setpoints be changed via the configuration screen?
 
Last edited:
What the alarms do, apart from making the HeaterMeter go beep beep beep is set using these files which need to either be uploaded or edited on the device. The alarm thresholds can be changed via the configuration page as of a week or two ago. It may not be in the last firmware snapshot though.
 
What the alarms do, apart from making the HeaterMeter go beep beep beep is set using these files which need to either be uploaded or edited on the device. The alarm thresholds can be changed via the configuration page as of a week or two ago. It may not be in the last firmware snapshot though.

By latest firmware snapshot I assume you're referring to the pre-built images located here?

http://capnbry.net/linkmeter/snapshots/bcm2708/

If so, I installed the more current image but cannot find alarm information under the configuration tab.

Also, if the HeaterMeter does go beep beep beep is there a way to silence it at the HeaterMeter?

Thanks
 
Yeah I guess it isn't in the snapshot yet then. Next one will have it!

The beeping can be silenced by pressing any button on the HeaterMeter (it will be displaying alarm information on the display as well).
 
I know right? I set up to smoke some asian-style ribs over tea leaves this weekend and suddenly I get an email and I'm all like "What?!" but it was an alarm telling me my fire was over 400F because I forgot to put the lid on the grill (it was still heating up). I kinda like this feature!
 
I'm struggling to get alarms sent to my phone and I suspect it's my setup in the /etc/msmtprc file. I have gmail, hotmail, qwest (my isp), fastmail, and tmomail but this setup is different from anything I'm used to. I tried googling some on the smart host but still couldn't figure it out. What should the contents of this file look like? The default contents are below.

Code:
# Example for a system wide configuration file

# A system wide configuration file is optional.
# If it exists, it usually defines a default account.
# This allows msmtp to be used like /usr/sbin/sendmail.
account default


# The SMTP smarthost.
host mailhub.oursite.example

# Construct envelope-from addresses of the form "user@oursite.example".
#auto_from on
#maildomain oursite.example

# Use TLS.
#tls on
#tls_trust_file /etc/ssl/certs/ca-certificates.crt

# Syslog logging with facility LOG_MAIL instead of the default LOG_USER.

syslog LOG_MAIL

Also, I created a file named alarm-0H under the linkmeter folder and it contains the following contents (I was trying to get either SMS or email to work, both of which failed). It did seem to partially work though (after changing permissions) because the alarm beeps a couple of times and then automatically silences when triggered instead of continuing to beep.


Code:
#!/bin/sh


# Silence all alarms
lmclient LMST,al,0,0,0,0,0,0,0,0

# Get current ringing name and temperature
PN=`eval echo \\$pn${al_probe}`
PCUR=`eval echo \\$pcurr${al_probe}`

sendmail [EMAIL="me@q.com"]me@q.com[/EMAIL] << EOF

From: Alan Erickson <me@q.com>
To: Alan Erickson <me@q.com>
Subject: [HM] $PN Alert


Alarm $al_type outside threshold $al_thresh, currently $PCUR.


$pn0: $pcurr0
$pn1: $pcurr1
$pn2: $pcurr2
$pn3: $pcurr3

echo $PN Alert -- $PCUR ($al_type) | sendmail [EMAIL="me@tmomail.net"]me@tmomail.net[/EMAIL]

--

[url]http://192.168.0.84/luci/lm/[/url]

EOF

Thanks

Edit: Tried adding the following to msmtprc plus a couple of other variations I found on the net and still no joy.

Code:
account        gmail
host           smtp.gmail.com
port           587
from           username@gmail.com
user           username@gmail.com
password       password
tls_trust_file /etc/ssl/certs/ca-certificates.crt
 
Last edited:
Upgraded both the openwrt and the HM firmware. Works really nicely. Initially I didn't upgrade both, just openwrt and I could not set anything, but upgrading the firmware fixed it.

dave
 
My /etc/msmtprc has this in it:
Code:
account default

# The SMTP smarthost.
host mail.ln9.net
port 465
user mayland
auth on
password password
tls on
tls_certcheck off
tls_starttls off
auto_from on
My mail server is just SSL with no starttls and has a self-signed certificate so I turn off cert checks.

What I'd do is create a file for testing with an email in it like /tmp/mymail:
Code:
From: Alan Erickson <me@q.com>
To: Alan Erickson <me@q.com>
Subject: [HM] $PN Alert


Alarm $al_type outside threshold $al_thresh, currently $PCUR.
Then try your mail configuration by invoking mail from the command line `sendmail you@gmail.com < /tmp/mymail`. I'll try to get it working with gmail though over the next couple of days and see if I can post a working msmtprc setup but maybe you'll get it working before then.
 
Oh yeah you're right. You've got your commands mixed in with the email text. The "<< EOF" means "take everything from here to the line that begins with EOF and pipe it to this command". It is called a 'heredoc'. So you don't want to put the "echo $PN Alert -- $PCUR ($al_type) | sendmail me@tmomail.net" inside of that because that's basically in the body of the email you're sending. Move that line either above your other sendmail line or after the EOF line
 
The last two programming courses I took were Fortran and Basic. Guess my profession.

Still struggling to get this going. Do I have to do anything with certificates to get this to work?

Edit: OK. Got it going with my Qwest / Centurylink account. WooHoo.

After doing quite a bit of reading I believe that in order to get Gmail working you do need a certificate, but I wouldn't bet the farm on that.

My msmtprc file looks like this:

Code:
 Example for a system wide configuration file

# A system wide configuration file is optional.
# If it exists, it usually defines a default account.
# This allows msmtp to be used like /usr/sbin/sendmail.
account default

# The SMTP smarthost
host smtp.q.com
port 587
auth login
user xxxxxx@q.com
password xxxxxxxxxxxxxxx
tls off
tls_certcheck off
tls_starttls on
auto_from off
from xxxxxxxx@q.com

# Construct envelope-from addresses of the form "user@oursite.example".
#auto_from on
#maildomain oursite.example

# Use TLS.
#tls on
#tls_trust_file /etc/ssl/certs/ca-certificates.crt

# Syslog logging with facility LOG_MAIL instead of the default LOG_USER.
syslog LOG_MAIL

And help from this posting here.

http://pastebin.com/VfchxBjL

And my alarm-0H file looks like this:

Code:
#!/bin/sh

# Silence all alarms
lmclient LMST,al,0,0,0,0,0,0,0,0

# Get current ringing name and temperature
PN=`eval echo \\$pn${al_probe}`
PCUR=`eval echo \\$pcurr${al_probe}`

sendmail xxxxxx@tmomail.com << EOF
From: Alan Erickson <xxxxxxn@q.com>
To: Alan Erickson <xxxxxxxn@q.com>
Subject: [HM] $PN Alert

Alarm $al_type outside threshold $al_thresh, currently $PCUR.

$pn0: $pcurr0
$pn1: $pcurr1
$pn2: $pcurr2
$pn3: $pcurr3

--

http://192.168.0.84/luci/lm/

EOF

sendmail xxxxxxxxx@q.com << EOF
From: Alan Erickson <xxxxxxxxxx@q.com>
To: Alan Erickson <xxxxxxxx@q.com>
Subject: [HM] $PN Alert

Alarm $al_type outside threshold $al_thresh, currently $PCUR.

$pn0: $pcurr0
$pn1: $pcurr1
$pn2: $pcurr2
$pn3: $pcurr3

--

http://192.168.0.84/luci/lm/

EOF

For whatever reason I couldn't get the echo to work so I just put the SENDMAIL in their twice and it seemed to work fine.

And the command line SENDMAIL was pretty helpful in getting things figured out. Used Putty for the command line and WinSCP to create and modify the files.
 
Last edited:

 

Back
Top