SHARE
TWEET

Untitled

a guest May 23rd, 2015 130 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. #
  3. # Sending emails in combination
  4. # with Motion surveillance software
  5. #
  6. # (c) Dr. Yves J. Hilpisch
  7. # The Python Quants GmbH
  8. #
  9. import sys
  10. print(sys.version)
  11. import smtplib
  12. from smtplib import SMTP
  13. import email
  14. from datetime import datetime
  15. from email.mime.multipart import MIMEMultipart
  16. from email.mime.text import MIMEText
  17.  
  18. import webiopi
  19. debuglevel = 0
  20. smtp = SMTP()
  21. smtp.set_debuglevel(debuglevel)
  22. # The Door Sensor is attached to GPIO18
  23. Door = 18
  24. # The LED Relay is attached to GPIO 3
  25. LED = 3
  26. # This is a variable with a default value that will not trigger anything
  27. Triggered = 3
  28.  
  29. # Enable debug output
  30. webiopi.setDebug()
  31. # Retrieve GPIO lib
  32. GPIO = webiopi.GPIO
  33. # Determine the current state of the door, at script start up.
  34. IsDoorOpen = int(GPIO.digitalRead(Door))
  35. if IsDoorOpen == 1:
  36.     TheDoor = "Open"
  37. if IsDoorOpen == 0:
  38.     TheDoor = "Closed"
  39. # This macro sends an email when the door begins to open.
  40. @webiopi.macro
  41. def OpenMail():
  42.     fromaddr = 'botnaribbb@gmail.com'
  43.     toaddrs  = '7033896656@tmotext.com'  # can be list of strings
  44.     subject = 'Garage Door is Opening'
  45. #
  46. # Email body
  47. #
  48.     rightnow = datetime.now()
  49.     body = "{0}\n{1}\n\n\n{2}".format(rightnow.strftime("%I:%M%p"), rightnow.strftime("%A %b %d, %Y"), 'http://mycamera.com:81/PictureCatch.cgi?username=user&password=pass&channel=2')
  50.     #This next line is used if you want to attach a file or image to the email, since verizon will not convert a mime attached image to txt, I don't use it.
  51.     #msg.attach(MIMEText(body, 'plain'))
  52. #
  53. # Email object
  54. #
  55.     msg = MIMEText(body)
  56.     msg['From'] = fromaddr
  57.     msg['To'] = toaddrs
  58.     msg['Subject'] = subject
  59. #
  60. # Connecting to SMTP server and
  61. # sending the email
  62. #
  63.     smtp = smtplib.SMTP()
  64.     smtp.set_debuglevel(1)
  65.     smtp.connect('smtp.gmail.com', 587)
  66.     smtp.login('botnaribbb@gmail.com', 'djbobo199')
  67.     text = msg.as_string()
  68.     smtp.sendmail(msg["From"], msg["To"].split(","), text)
  69.     smtp.quit()
  70.  
  71. # This macro send this email when the door has completely closed.
  72. @webiopi.macro  
  73. def CloseMail():
  74.     fromaddr = 'botnaribbb@gmail.com'
  75.     toaddrs  = '7033896656@tmotext.com'  # can be list of strings
  76.     subject = 'Garage Door is now Closed'
  77.    
  78. #
  79. # Email body
  80. #
  81.     rightnow = datetime.now()
  82.     body = "{0}\n{1}\n\n\n{2}".format(rightnow.strftime("%I:%M%p"), rightnow.strftime("%A %b %d, %Y"), 'Have a nice day!')
  83.     #This next line is used if you want to attach a file or image to the email, since verizon will not convert a mime attached image to txt, I don't use it.
  84.     #msg.attach(MIMEText(body, 'plain'))
  85. #
  86. # Email object
  87. #
  88.     msg = MIMEText(body)
  89.     msg['From'] = fromaddr
  90.     msg['To'] = toaddrs
  91.     msg['Subject'] = subject
  92. #
  93. # Connecting to SMTP server and
  94. # sending the email
  95. #
  96.     smtp = smtplib.SMTP()
  97.     smtp.set_debuglevel(1)
  98.     smtp.connect('smtp.gmail.com', 587)
  99.     smtp.login('botnaribbb@gmail.com', 'djbobo199')
  100.     text = msg.as_string()
  101.     smtp.sendmail(msg["From"], msg["To"].split(","), text)
  102.     smtp.quit()
  103. # This macro only send an email if the door is already open when the script is started or the PI is rebooted
  104. @webiopi.macro
  105. def AlreadyMail():
  106.    fromaddr = 'botnaribbb@gmail.com'
  107.     toaddrs  = '7033896656@tmotext.com'  # can be list of strings
  108.     subject = "GarageDoor Startup - Door is: {0}".format(TheDoor)
  109. #
  110. # Email body
  111. #
  112.     rightnow = datetime.now()
  113.     body = "{0}\n{1}\n\n\n{2}".format(rightnow.strftime("%I:%M%p"), rightnow.strftime("%A %b %d, %Y"), 'Have a nice day!')
  114.     #This next line is used if you want to attach a file or image to the email, since verizon will not convert a mime attached image to txt, I don't use it.
  115.     #msg.attach(MIMEText(body, 'plain'))
  116. #
  117. # Email object
  118. #
  119.     msg = MIMEText(body)
  120.     msg['From'] = fromaddr
  121.     msg['To'] = toaddrs
  122.     msg['Subject'] = subject
  123. #
  124. # Connecting to SMTP server and
  125. # sending the email
  126. #
  127.     smtp = smtplib.SMTP()
  128.     smtp.set_debuglevel(1)
  129.     smtp.connect('smtp.gmail.com', 587)
  130.     smtp.login('botnaribbb@gmail.com', 'djbobo199')
  131.     text = msg.as_string()
  132.     smtp.sendmail(msg["From"], msg["To"].split(","), text)
  133.     smtp.quit()
  134.    
  135.    
  136.    
  137.    
  138.    
  139.    
  140.  
  141. @webiopi.macro
  142. def TurnOnLED():
  143.     GPIO.setFunction(LED, GPIO.IN)
  144.    
  145. @webiopi.macro
  146. def TurnOffLED():
  147.     GPIO.setFunction(LED, GPIO.OUT)
  148.  
  149. AlreadyMail()
  150.  
  151. # Called by WebIOPi at script loading
  152. def setup():
  153.     webiopi.debug("Script with macros - Setup")
  154.     GPIO.setFunction(Door, GPIO.IN)
  155. # Sync state of GPIO18 and Triggered
  156. # Looped by WebIOPi
  157. def loop():
  158.     global Triggered
  159.     #Check door status and set LED
  160.     if int(GPIO.digitalRead(Door)) == 1:
  161.         GPIO.setFunction(LED, GPIO.OUT)
  162.     if int(GPIO.digitalRead(Door)) == 0:
  163.         GPIO.setFunction(LED, GPIO.IN)
  164.     if Triggered > 0 and int(GPIO.digitalRead(Door)) == 1:
  165.         if Triggered == 3:
  166.             AlreadyMail
  167.             Triggered = 0
  168.         if Triggered == 1:
  169.             OpenMail()
  170.             Triggered = 0
  171.  
  172.     if Triggered == 0 and int(GPIO.digitalRead(Door)) == 0:
  173.         CloseMail()
  174.         Triggered = 1
  175.     webiopi.sleep(1)      
  176. # Called by WebIOPi at server shutdown
  177. def destroy():
  178.     webiopi.debug("Script with macros - Destroy")
  179.     # Reset GPIO functions
  180.     GPIO.setFunction(Door, GPIO.IN)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top