This is a small project I did with Raspberry pi to be able to water plants with an email sent to do so!
Here is what you need:
1. Rapberry pi
2. Hobby Motor
3. L293D
4. Rubber tube and some bottle cap
Below is the connection diagram between raspberry pi and motor chip (L293D). Credit goes to Adafruit lesson on DC motor. I followed the same connections and used the occidentalis software provided by them.
For the software to extract gmail titles to look for “Waterr” and water the plant, I followed the below code (With due credits to https://pythonadventures.wordpress.com/tag/gmail/)
import urllib2
import untangle
#from motor1 import run_motor
import RPi.GPIO as io
io.setmode(io.BCM)
in1_pin = 4
in2_pin = 17
io.setup(in1_pin, io.OUT)
io.setup(in2_pin, io.OUT)
def set(property, value):
try:
f = open("/sys/class/rpi-pwm/pwm0/" + property, 'w')
f.write(value)
f.close()
except:
print("Error writing to: " + property + " value: " + value)
set("delayed", "0")
set("mode", "pwm")
set("frequency", "500")
set("active", "1")
def clockwise():
io.output(in1_pin, True)
io.output(in2_pin, False)
def counter_clockwise():
io.output(in1_pin, False)
io.output(in2_pin, True)
clockwise()
def run_motor():
while True:
clockwise()
set("duty", "99")
FEED_URL = 'https://mail.google.com/mail/feed/atom'
def get_unread_msgs(user, passwd):
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(
realm='New mail feed',
uri='https://mail.google.com',
user='{user}@gmail.com'.format(user=user),
passwd=passwd
)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
feed = urllib2.urlopen(FEED_URL)
return feed.read()
##########
if __name__ == "__main__":
import getpass
# user = raw_input('Username: ')
# passwd = getpass.getpass('Password: ')
# print get_unread_msgs(user, passwd)
#make sure you change the below to yours
USER = 'USERNAME'
PASSWORD = PASSWORD'
xml = get_unread_msgs(USER, PASSWORD)
o = untangle.parse(xml)
try:
for e in o.feed.entry:
title = e.title.cdata
# print title
if title == "Waterr":
print 'Watering the raspberry pi now'
#Call the motor function here to water.
run_motor()
except IndexError:
pass # no new mail
Send email with subject as “Waterr” to see your raspberry pi watering your plants.
If you want to use cron tab, please follow the blog below (one of the best blogs I know on python)
Calling a Python script from crontab
If you want to execute it every few minutes, it is better to use code such as below than crontab (which is better used for boot time activities).
while true:
<do something>
sleep <sometime>