Below sample allows to monitor the state of Digital input 1, and upon state change, send an Email to predefined email address.
Note that you will need a SMTP account to be able to send the mail.
You can subscribe to free SMTP test account under jangosmtp.com/default.asp
local sched = require “sched”
local os = require “os”
local devicetree = require ‘devicetree’
local smtp = require ‘socket.smtp’
local from_email = “AAF_script_LS@sierrasupport.com”
local email_rcpt = {
“”,
}
local in1 = ‘system.aleos.io.in1’
function when_dig_input_changes (din_data)
print("Digital input 1 state changed to: ", din_data[in1])
local mesgt = {
headers = {
to = "name surname <destination email address>",
--cc = '"name surname " < copy email address>',
subject = "Alert: Digital input 1 state changed to:" .. din_data[in1]
},
body = "this is the body of my email"
}
print (‘Sending email message’)
local r, e = smtp.send{
from = from_email,
rcpt = email_rcpt,
user = “xxxxx”,
password = “xxxxxx”,
server = “relay.jangosmtp.net”,
port = 25,
source = smtp.message(mesgt)
}
print®
print(e)
end
function main ()
print(“Starting Event_Reporting sample app”)
assert(devicetree.init())
-- subscirbe to IO1 digital input.
-- Function "when_dig_input_changes" will be executed upon every state change.
assert(devicetree.register(in1, when_dig_input_changes))
end
– execute in loop
sched.run(main)
sched.loop()