Skip to content

Instantly share code, notes, and snippets.

@maanenson
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maanenson/4a2bc050ee87b08f1fc8 to your computer and use it in GitHub Desktop.
Save maanenson/4a2bc050ee87b08f1fc8 to your computer and use it in GitHub Desktop.
Connected LaunchPad Trigger On Pushbutton
local push_button1 = alias['usrsw1'] -- Dataport pushes sent to (counter value)
local emailaddr = nil -- format is 'me@here.com'
local phone_number = nil -- format is: '+1aaabbbcccc' if you have SMS and want to try it out
local timeformat = '%F %T %Z'
-- note, if alert sent not long ago, will skip so it doesn't waste your available alert messgaes (emails/day, sms bucket)
local last_alert = 0
local last_value = push_button1.value or 0
local alert_period_threshold = 15 -- seconds to limit alerts from sending too often (i.e. won't send two in this time window)
-- Main Loop --
debug('starting')
while true do
local ts = push_button1.wait() -- this blocks until new value comes into dataport
local push_counter = push_button1[ts] --get value at timestamp
-- Check if different (since value is sent no matter change on device)
if push_counter ~= nil and push_counter > last_value then
local alert_msg = 'Connected LaunchPad Alert, new button pushes count: '.. tostring(push_counter)
debug('alert message: '..tostring(alert_msg))
-- Check if we should send alert or if it is in the last alert window
if last_alert + alert_period_threshold < now then
debug('send alerts')
-- Send Email if emailaddr supplied
if emailaddr ~= nil then
local emailheader = 'Connected LaunchPad Alert Message'
debug('sending email alert to: '..tostring(emailaddr))
email(emailaddr,emailheader,'Alert Message: '..alert_msg..'\r\nTime: '..date(timeformat,ts))
else
debug('no email address found')
end
-- Note, SMS alerts must be added to an account to receive SMS alerts
if phone_number ~= nil then
debug('sending sms alert to: '..tostring(phone_number))
sms(phone_number,'CLP Alert: '..alert_msg)
else
debug('no phone number found')
end
end
-- update last alert time
last_alert = now
end
-- Update last value
last_value = push_counter
push_button1.last = now --start waiting now
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment