Example: Automatic IP camera snapshot on intercom call

Task

The example below shows how to snapshot IP camera when the intercom call is done and send this snapshot over email.

Resident script

Add the following resident script and run once.

Source code    
  1. --***********************************************************--
  2. --** Email image attachment created by Erwin van der Zwart **--
  3. --***********************************************************--
  4. --******************* Start of parameters *******************--
  5. --***********************************************************--
  6.  
  7. --Gmail (smtp) username !IMPORTANT!
  8. user = 'YOUR.ADDRESS@GMAIL.COM'
  9.  
  10. --Gmail (smtp) password !IMPORTANT!
  11. password = 'YOUR GMAIL PASSWORD'
  12.  
  13. --Sender for e-mail
  14. from = '<your.address@gmail.com>'
  15. alias_from = 'YOUR FULL NAME'
  16.  
  17. --Recipient for e-mail
  18. to = '<recipient@domain.com>'
  19. alias_to = 'recipient full name'
  20.  
  21. --Subject for e-mail
  22. subject = 'Snapshot from camera automaticly send by homeLYnk'
  23.  
  24. --Attachment and filetype (set filetype as 'gif', 'png', 'bmp', 'jpg' or 'jpeg' according image source)
  25. image_type = 'jpeg'
  26. image_name_attachment = 'snapshot'
  27. source_image_url = 'http://your.camara.ip/snapshot.jpeg'
  28. image_description = 'Snapshot from IP Camera'
  29.  
  30. --Message on bottom of email (will only be showed when client don't understand attachment)
  31. epilogue = 'End of message'
  32.  
  33. --***********************************************************--
  34. --******************** End of parameters ********************--
  35. --***********************************************************--
  36. --********** DON'T CHANGE ANYTHING UNDER THIS LINE **********--
  37. --***********************************************************--
  38.  
  39. --Get remote (from HTTP) image and put image file to HL (will be deleted when end of script)
  40. os.execute('wget -O /www/scada/vis/' .. image_name_attachment .. '.' .. image_type .. ' ' .. source_image_url .. '')
  41.  
  42. --Create filename and location
  43. local fileName = '/www/scada/vis/' .. image_name_attachment .. '.' .. image_type
  44.  
  45. --Create table to include mail settings
  46. local settings = {
  47. from = from,
  48. rcpt = to,
  49. user = user,
  50. password = password,
  51. server = 'smtp.gmail.com',
  52. port = 465,
  53. secure = 'sslv23',
  54. }
  55.  
  56. --Load required modules to send email with attachment
  57. local smtp = require("socket.smtp")
  58. local mime = require("mime")
  59. local ltn12 = require("ltn12")
  60.  
  61. --Create e-mail header
  62. settings.source = smtp.message{
  63. headers = {
  64. from = '' .. alias_from .. ' ' .. from .. '',
  65. to = '' .. alias_to .. ' ' .. to .. '',
  66. subject = subject
  67. },
  68.  
  69. --Load attachment inside body
  70. body = {
  71. preamble = "",
  72. [1] = {
  73. headers = {
  74. ["content-type"] = 'image/' .. image_type .. '; name="' .. image_name_attachment .. '.' .. image_type .. '"',
  75. ["content-disposition"] = 'attachment; filename="' .. image_name_attachment .. '.' .. image_type .. '"',
  76. ["content-description"] = '' .. image_description .. '',
  77. ["content-transfer-encoding"] = "BASE64"
  78. },
  79.  
  80. body = ltn12.source.chain(
  81. ltn12.source.file(io.open(fileName, "rb")),
  82. ltn12.filter.chain(
  83. mime.encode("base64"),
  84. mime.wrap()
  85. )
  86. )
  87. },
  88. epilogue = epilogue
  89. }
  90. }
  91.  
  92. --Send the email
  93. r, e = smtp.send(settings)
  94.  
  95. --Create alert when sending gives an error with error message
  96. if (e) then
  97. alert("Could not send email: ", e, "\n")
  98. end
  99.  
  100. --Delete downloaded image file from HL
  101. os.remove(fileName)