Raspberry - moisture_monitor.py


import moisture_ascii_emoji
import RPi.GPIO as GPIO # This is the GPIO library we need to use the GPIO pins on the Raspberry Pi
import smtplib # This is the SMTP library we need to send the email notification
import time # This is the time library, we need this so we can use the sleep function

# Define some variables to be used later on in our script
# 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
# If you have setup your raspberry pi to send emails, then you will probably want to use 'localhost' for your smtp_host
smtp_username = "enter_username_here" # This is the username used to login to your SMTP provider
smtp_password = "enter_password_here" # This is the password used to login to your SMTP provider
smtp_host = "x.x.x" # This is the host of the SMTP provider
smtp_port = 25 # This is the port that your SMTP provider uses
smtp_sender = "[email protected]" # This is the FROM email address
smtp_receivers = ['[email protected]'] # This is the TO email address
# The next two variables use triple quotes, these allow us to preserve the line breaks in the string.
# This is the message that will be sent when NO moisture is detected
message_dead = """From: Treeminator <[email protected]>
To: X <[email protected]>
Subject: Plant Dehydration

It's me the IT Services Plant. I'm thirsty, please water me. Plant death imminent!!! :'(

This might be my last message.

Kind Regards,

IT Services Plant
Photosynthesis Specialist


# This is the message that will be sent when moisture IS detected again
message_alive = """From: Treeminator <[email protected]>
To: X <[email protected]>
Subject: Plant Dehydration
The panic is over! I have water again :)
# This is our sendEmail function
def sendEmail(smtp_message):
    localtime = time.asctime( time.localtime(time.time()) )
        smtpObj = smtplib.SMTP(smtp_host, smtp_port)
#        smtpObj.login(smtp_username, smtp_password) # If you don't need to login to your smtp provider, simply remove this line
        smtpObj.sendmail(smtp_sender, smtp_receivers, smtp_message)
        print "Successfully sent email", localtime
    except smtplib.SMTPException:
        print "Error: unable to send email", localtime
# Set our GPIO numbering to BCM
# Define the GPIO pin that we have our digital output from our sensor connected to
channel = 21
# Set the GPIO pin to an input
GPIO.setup(channel, GPIO.IN)
# This line tells our script to keep an eye on our gpio pin and let us know when the pin goes HIGH or LOW
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)
# This is an infinte loop to keep our script running
previousstate = 0
while True:
    localtime = time.asctime( time.localtime(time.time()) )
    if GPIO.input(channel) == 1:
        state = 1
        print "Water Level: Critical. Please water me, I'm thirsty.", localtime
        if previousstate != state:
            previousstate = state
        state = 0
        print "Water Levels: OK. The panic is over.", localtime
        if previousstate != state:
            previousstate = state
    # This line simply tells our script to wait 0.1 of a second, this is so the script doesnt hog all of the CPU