26.10.2017, 08:52
Hello,
I have a script that sends an email with attached foscam camera snaphots when my doorbell is pushed.
That works great for months, but after some time it crashed my lm4 as following:
- when my doorbell is pushed i receive an email with size 0 images. every doorbell push i receive that mail with size 0 image
- so i try to find the problem and test my code manual with a resident script (sleepinterval 60), the same code as my event-based script
-I receive every 60 seconds an email, thats correct. But when i stop/deactivate this script i also receive every 60 seconds the email, why?
-So i need to restart my lm4 to solve this issue.
Nit_email
Nit_camera
I have a script that sends an email with attached foscam camera snaphots when my doorbell is pushed.
That works great for months, but after some time it crashed my lm4 as following:
- when my doorbell is pushed i receive an email with size 0 images. every doorbell push i receive that mail with size 0 image
- so i try to find the problem and test my code manual with a resident script (sleepinterval 60), the same code as my event-based script
Code:
require('user.nit_email')
mail({'test@test.nl'}, 'Test', 'Test message.', 'std_camera_2')
-So i need to restart my lm4 to solve this issue.
Nit_email
Code:
local from_name = 'HomeLynk'
local settings = {
-- "from" field, only e-mail must be specified here
from = 'test@test.nl',
-- smtp username
--user = '',
-- smtp password
--password = '',
-- smtp server
server = '1.2.3.4',
-- smtp server port
port = 25,
-- enable ssl, required for gmail smtp
--secure = 'sslv23',
}
local escape = function(v)
return '<' .. tostring(v) .. '>'
end
-- send an e-mail,
function mail(to, subject, message, att)
-- make sure these settings are correct
local smtp = require('socket.smtp')
if att then
local mime = require('mime')
local ltn12 = require('ltn12')
-- generate attachements
bodyAtt = handleAtt(att, 'start')
else
bodyAtt = {"",""}
end
if type(to) ~= 'table' then
to = { to }
end
for index, email in ipairs(to) do
to[ index ] = escape(email)
end
settings.source = smtp.message({
headers = {
from = from_name..escape(settings.from),
to = table.concat(to, ', '),
subject = subject,
},
body = {
--preamble = "Hello Recipient, \r\n" .. "This is your e-mail with a picture attached.",
{
header = {
['Content-type'] = 'text/html; charset=utf-8',
},
body = mime.eol(0, message),
},
bodyAtt[1],bodyAtt[2], -- get attachements if exists (max 2 for now) <---todo:make it dynamic
--epilogue = "That's it folks! :)"
}
})
bodyAtt = nil
-- fixup from field
settings.from = escape(settings.from)
settings.rcpt = to
smtp.send(settings)
if att then
handleAtt(att, 'end')
end
end
-- std_camera: snapshot camera voordeur
-- std_camera_b: snapshot camera voordeur en achterdeur
function handleAtt(att, handle)
if handle == "start" then
if att == "std_camera" then
require('user.nit_camera')
dst = '/tmp/capture.jpg'
local url = string.format(url_cmdFoscam, cam_voordeur, cam_port, 'snapPicture2', '', cam_usr2, cam_pwd2)
local cmd = string.format('wget -O %q %q', dst, url)
os.execute(cmd)
local attachment = ltn12.source.chain( ltn12.source.file(io.open(dst, 'r')), ltn12.filter.chain(mime.encode('base64'), mime.wrap()) )
return
{
{
headers = {
['Content-type'] = 'image/jpeg; name="image.jpg"',
['Content-disposition'] = 'attachment; filename="image.jpg"',
['Content-transfer-encoding'] = 'BASE64',
},
body = attachment
}
}
elseif att == "std_camera_b" then
require('user.nit_camera')
dst1 = '/tmp/capture1.jpg'
dst2 = '/tmp/capture2.jpg'
local url1 = string.format(url_cmdFoscam, cam_voordeur, cam_port, 'snapPicture2', '', cam_usr2, cam_pwd2)
local url2 = string.format(url_cmdFoscam, cam_achterdeur, cam_port, 'snapPicture2', '', cam_usr2, cam_pwd2)
local cmd1 = string.format('wget -O %q %q', dst1, url1)
local cmd2 = string.format('wget -O %q %q', dst2, url2)
os.execute(cmd1)
os.execute(cmd2)
local attachment1 = ltn12.source.chain( ltn12.source.file(io.open(dst1, 'r')), ltn12.filter.chain(mime.encode('base64'), mime.wrap()) )
local attachment2 = ltn12.source.chain( ltn12.source.file(io.open(dst2, 'r')), ltn12.filter.chain(mime.encode('base64'), mime.wrap()) )
return
{
{
headers = {
['Content-type'] = 'image/jpeg; name="image1.jpg"',
['Content-disposition'] = 'attachment; filename="image1.jpg"',
['Content-transfer-encoding'] = 'BASE64',
},
body = attachment1
},
{
headers = {
['Content-type'] = 'image/jpeg; name="image2.jpg"',
['Content-disposition'] = 'attachment; filename="image2.jpg"',
['Content-transfer-encoding'] = 'BASE64',
},
body = attachment2
}
}
elseif att == "std_camera_2" then
require('user.nit_camera')
dst1 = '/tmp/capture1.jpg'
dst2 = '/tmp/capture2.jpg'
local url1 = string.format(url_cmdFoscam, cam_voordeur, cam_port, 'snapPicture2', '', cam_usr2, cam_pwd2)
local cmd1 = string.format('wget -O %q %q', dst1, url1)
local cmd2 = string.format('wget -O %q %q', dst2, url1)
os.execute(cmd1) -- direct snapshot
cmdFoscam(cam_voordeur, cam_port, 'ptzGotoPresetPoint', '&name=Deur') -- move to frontdoor
os.sleep(20) -- wait for 20 seconds
os.execute(cmd2) -- new snapshot
local attachment1 = ltn12.source.chain( ltn12.source.file(io.open(dst1, 'r')), ltn12.filter.chain(mime.encode('base64'), mime.wrap()) )
local attachment2 = ltn12.source.chain( ltn12.source.file(io.open(dst2, 'r')), ltn12.filter.chain(mime.encode('base64'), mime.wrap()) )
return
{
{
headers = {
['Content-type'] = 'image/jpeg; name="image1.jpg"',
['Content-disposition'] = 'attachment; filename="image1.jpg"',
['Content-transfer-encoding'] = 'BASE64',
},
body = attachment1
},
{
headers = {
['Content-type'] = 'image/jpeg; name="image2.jpg"',
['Content-disposition'] = 'attachment; filename="image2.jpg"',
['Content-transfer-encoding'] = 'BASE64',
},
body = attachment2
}
}
end
elseif handle == "end" then
if att == "std_camera" then
os.remove(dst)
elseif att == "std_camera_b" then
os.remove(dst1)
os.remove(dst2)
elseif att == "std_camera_2" then
os.remove(dst1)
os.remove(dst2)
end
end
end
Nit_camera
Code:
-- snapshot urls
cam_voordeur = '2.2.2.2'
cam_achterdeur = '2.2.2.3'
cam_port = 80
cam_usr = 'user'
cam_pwd = 'password'
cam_usr2 = 'user2'
cam_pwd2 = 'password2'
-- start command urls
url_cmdFoscam = 'http://%s:%s/cgi-bin/CGIProxy.fcgi?cmd=%s%s&usr=%s&pwd=%s'
-- Foscam camera command
function cmdFoscam(iCamera, iPort, iCmd, iParam)
local url, response
url = string.format(url_cmdFoscam, iCamera, iPort, iCmd, iParam, cam_usr, cam_pwd)
if (url) then
require('socket.http')
socket.http.TIMEOUT = 5
response = socket.http.request( url_encode(url) )
end
return response
end