Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Start by importing the libraries we want to use
  4.  
  5. import RPi.GPIO as GPIO # This is the GPIO library we need to use the GPIO pins on the Raspberry Pi
  6. import smtplib # This is the SMTP library we need to send the email notification
  7. import time # This is the time library, we need this so we can use the sleep function
  8.  
  9. # Define some variables to be used later on in our script
  10.  
  11. # You might not need the username and password variable, depends if you are using a provider or if you have your raspberry pi setup to send emails
  12. # If you have setup your raspberry pi to send emails, then you will probably want to use 'localhost' for your smtp_host
  13.  
  14. smtp_username = "xx@gmail.com" # This is the username used to login to your SMTP provider
  15. smtp_password = "xxxxx" # This is the password used to login to your SMTP provider
  16. smtp_host = "smtp.gmail.com" # This is the host of the SMTP provider
  17. smtp_port = 587 # This is the port that your SMTP provider uses
  18.  
  19. smtp_sender = "xx@gmail.com" # This is the FROM email address
  20. smtp_receivers = ['xx@gmail.com'] # This is the TO email address
  21.  
  22. # The next two variables use triple quotes, these allow us to preserve the line breaks in the string.
  23.  
  24. # This is the message that will be sent when NO moisture is detected
  25.  
  26. message_dead = """From: Sender Name <sender@email.com>
  27. To: Receiver Name <receiver@email.com>
  28. Subject: Moisture Sensor Notification
  29.  
  30. Warning, no moisture detected! Plant death imminent!!! :'(
  31. """
  32.  
  33. # This is the message that will be sent when moisture IS detected again
  34.  
  35. message_alive = """From: Sender Name <sender@email.com>
  36. To: Receiver Name <receiver@email.com>
  37. Subject: Moisture Sensor Notification
  38.  
  39. Panic over! Plant has water again :)
  40. """
  41.  
  42. # This is our sendEmail function
  43.  
  44. def sendEmail(smtp_message):
  45.     try:
  46.         smtpObj = smtplib.SMTP(smtp_host, smtp_port)
  47.         smtpObj.ehlo()
  48.         smtpObj.starttls()
  49.         smtpObj.ehlo
  50.         smtpObj.login(smtp_username, smtp_password) # If you don't need to login to your smtp provider, simply remove this line
  51.         smtpObj.sendmail(smtp_sender, smtp_receivers, smtp_message)        
  52.         print "Successfully sent email"
  53.     except smtplib.SMTPException:
  54.         print "Error: unable to send email"
  55.  
  56. # This is our callback function, this function will be called every time there is a change on the specified GPIO channel, in this example we are using 17
  57.  
  58. def callback(channel):  
  59.     if GPIO.input(channel):
  60.         print "LED off"
  61.         sendEmail(message_dead)
  62.     else:
  63.         print "LED on"
  64.         sendEmail(message_alive)
  65.  
  66. # Set our GPIO numbering to BCM
  67. GPIO.setmode(GPIO.BCM)
  68.  
  69. # Define the GPIO pin that we have our digital output from our sensor connected to
  70. channel = 4
  71. # Set the GPIO pin to an input
  72. GPIO.setup(channel, GPIO.IN)
  73.  
  74. # This line tells our script to keep an eye on our gpio pin and let us know when the pin goes HIGH or LOW
  75. GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)
  76. # This line asigns a function to the GPIO pin so that when the above line tells us there is a change on the pin, run this function
  77. GPIO.add_event_callback(channel, callback)
  78.  
  79. # This is an infinte loop to keep our script running
  80. while True:
  81.     # This line simply tells our script to wait 6 hours, this is so the script doesnt hog all of the CPU
  82.     time.sleep(21600)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement