Advertisement
Guest User

Untitled

a guest
Aug 18th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 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 = "enter_username_here" # This is the username used to login to your SMTP provider
  15. smtp_password = "enter_password_here" # This is the password used to login to your SMTP provider
  16. smtp_host = "enter_host_here" # This is the host of the SMTP provider
  17. smtp_port = 25 # This is the port that your SMTP provider uses
  18.  
  19. smtp_sender = "sender@email.com" # This is the FROM email address
  20. smtp_receivers = ['receiver@email.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.login(smtp_username, smtp_password) # If you don't need to login to your smtp provider, simply remove this line
  48.         smtpObj.sendmail(smtp_sender, smtp_receivers, smtp_message)        
  49.         print "Successfully sent email"
  50.     except SMTPException:
  51.         print "Error: unable to send email"
  52.  
  53. # 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
  54.  
  55. def callback(channel):  
  56.     if GPIO.input(channel):
  57.         print "LED off"
  58.         sendEmail(message_dead)
  59.     else:
  60.         print "LED on"
  61.         sendEmail(message_alive)
  62.  
  63. # Set our GPIO numbering to BCM
  64. GPIO.setmode(GPIO.BCM)
  65.  
  66. # Define the GPIO pin that we have our digital output from our sensor connected to
  67. channel = 17
  68. # Set the GPIO pin to an input
  69. GPIO.setup(channel, GPIO.IN)
  70.  
  71. # This line tells our script to keep an eye on our gpio pin and let us know when the pin goes HIGH or LOW
  72. GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)
  73. # 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
  74. GPIO.add_event_callback(channel, callback)
  75.  
  76. # This is an infinte loop to keep our script running
  77. while True:
  78.     # This line simply tells our script to wait 0.1 of a second, this is so the script doesnt hog all of the CPU
  79.     time.sleep(0.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement