Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- #
- # Sending emails in combination
- # with Motion surveillance software
- #
- # (c) Dr. Yves J. Hilpisch
- # The Python Quants GmbH
- #
- import sys
- print(sys.version)
- import smtplib
- from smtplib import SMTP
- import email
- from datetime import datetime
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- import webiopi
- debuglevel = 0
- smtp = SMTP()
- smtp.set_debuglevel(debuglevel)
- # The Door Sensor is attached to GPIO18
- Door = 18
- # The LED Relay is attached to GPIO 3
- LED = 3
- # This is a variable with a default value that will not trigger anything
- Triggered = 3
- # Enable debug output
- webiopi.setDebug()
- # Retrieve GPIO lib
- GPIO = webiopi.GPIO
- # Determine the current state of the door, at script start up.
- IsDoorOpen = int(GPIO.digitalRead(Door))
- if IsDoorOpen == 1:
- TheDoor = "Open"
- if IsDoorOpen == 0:
- TheDoor = "Closed"
- # This macro sends an email when the door begins to open.
- @webiopi.macro
- def OpenMail():
- fromaddr = 'botnaribbb@gmail.com'
- toaddrs = '7033896656@tmotext.com' # can be list of strings
- subject = 'Garage Door is Opening'
- #
- # Email body
- #
- rightnow = datetime.now()
- 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')
- #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.
- #msg.attach(MIMEText(body, 'plain'))
- #
- # Email object
- #
- msg = MIMEText(body)
- msg['From'] = fromaddr
- msg['To'] = toaddrs
- msg['Subject'] = subject
- #
- # Connecting to SMTP server and
- # sending the email
- #
- smtp = smtplib.SMTP()
- smtp.set_debuglevel(1)
- smtp.connect('smtp.gmail.com', 587)
- smtp.login('botnaribbb@gmail.com', 'djbobo199')
- text = msg.as_string()
- smtp.sendmail(msg["From"], msg["To"].split(","), text)
- smtp.quit()
- # This macro send this email when the door has completely closed.
- @webiopi.macro
- def CloseMail():
- fromaddr = 'botnaribbb@gmail.com'
- toaddrs = '7033896656@tmotext.com' # can be list of strings
- subject = 'Garage Door is now Closed'
- #
- # Email body
- #
- rightnow = datetime.now()
- body = "{0}\n{1}\n\n\n{2}".format(rightnow.strftime("%I:%M%p"), rightnow.strftime("%A %b %d, %Y"), 'Have a nice day!')
- #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.
- #msg.attach(MIMEText(body, 'plain'))
- #
- # Email object
- #
- msg = MIMEText(body)
- msg['From'] = fromaddr
- msg['To'] = toaddrs
- msg['Subject'] = subject
- #
- # Connecting to SMTP server and
- # sending the email
- #
- smtp = smtplib.SMTP()
- smtp.set_debuglevel(1)
- smtp.connect('smtp.gmail.com', 587)
- smtp.login('botnaribbb@gmail.com', 'djbobo199')
- text = msg.as_string()
- smtp.sendmail(msg["From"], msg["To"].split(","), text)
- smtp.quit()
- # This macro only send an email if the door is already open when the script is started or the PI is rebooted
- @webiopi.macro
- def AlreadyMail():
- fromaddr = 'botnaribbb@gmail.com'
- toaddrs = '7033896656@tmotext.com' # can be list of strings
- subject = "GarageDoor Startup - Door is: {0}".format(TheDoor)
- #
- # Email body
- #
- rightnow = datetime.now()
- body = "{0}\n{1}\n\n\n{2}".format(rightnow.strftime("%I:%M%p"), rightnow.strftime("%A %b %d, %Y"), 'Have a nice day!')
- #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.
- #msg.attach(MIMEText(body, 'plain'))
- #
- # Email object
- #
- msg = MIMEText(body)
- msg['From'] = fromaddr
- msg['To'] = toaddrs
- msg['Subject'] = subject
- #
- # Connecting to SMTP server and
- # sending the email
- #
- smtp = smtplib.SMTP()
- smtp.set_debuglevel(1)
- smtp.connect('smtp.gmail.com', 587)
- smtp.login('botnaribbb@gmail.com', 'djbobo199')
- text = msg.as_string()
- smtp.sendmail(msg["From"], msg["To"].split(","), text)
- smtp.quit()
- @webiopi.macro
- def TurnOnLED():
- GPIO.setFunction(LED, GPIO.IN)
- @webiopi.macro
- def TurnOffLED():
- GPIO.setFunction(LED, GPIO.OUT)
- AlreadyMail()
- # Called by WebIOPi at script loading
- def setup():
- webiopi.debug("Script with macros - Setup")
- GPIO.setFunction(Door, GPIO.IN)
- # Sync state of GPIO18 and Triggered
- # Looped by WebIOPi
- def loop():
- global Triggered
- #Check door status and set LED
- if int(GPIO.digitalRead(Door)) == 1:
- GPIO.setFunction(LED, GPIO.OUT)
- if int(GPIO.digitalRead(Door)) == 0:
- GPIO.setFunction(LED, GPIO.IN)
- if Triggered > 0 and int(GPIO.digitalRead(Door)) == 1:
- if Triggered == 3:
- AlreadyMail
- Triggered = 0
- if Triggered == 1:
- OpenMail()
- Triggered = 0
- if Triggered == 0 and int(GPIO.digitalRead(Door)) == 0:
- CloseMail()
- Triggered = 1
- webiopi.sleep(1)
- # Called by WebIOPi at server shutdown
- def destroy():
- webiopi.debug("Script with macros - Destroy")
- # Reset GPIO functions
- GPIO.setFunction(Door, GPIO.IN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement