from email import Encoders
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import
from email.Utils import formatdate
email_from = “firstname.lastname@example.org”
password = “googlepassword”
msg = MIMEMultipart()
msg[“From”] = email_from
msg[“To”] = to_addr
msg[“Subject”] = subject
msg[“Date”] = formatdate(localtime=True)
part = MIMEBase(‘application’, “
server = smtplib.SMTP(“smtp.
print(“notification email sent to ““+
print(“Error sending email!”)
Now, open up the Python interpreter and try this on
for size. Import our new library (from emailnotify import
*), then pass a command; all of the arguments are strings.
Within moments, you should have an email in your
mailbox. This new library is easily incorporated into our
main function. Check the article link for
including the email function.
Adding Simple Message System
(SMS) Capability for Cell Phones
We can add additional capability to the system by
taking advantage of a simple trick to send SMS messages.
Depending on your carrier, you can send yourself or
anyone an SMS using email. This is simply a “normal”
email address — no special access is required. If it doesn’t
work, check with your wireless provider. Refer to Figure 5
and substitute the destination mobile phone number as
Hopefully, you are feeling somewhat satisfied with
your new powers. I really enjoyed getting to this point, but
I’d also been tinkering with a few other libraries and
realized there is a lot farther we can go with this project!
Adding a Webcam Picture
For this next exercise, we will import another library
and dig out an old USB webcam from our pile of junk.
The installation of this library is much more involved. I
recommend looking up “how to install simplecv” or go to
www.simplecv.org. I found the basic instructions below. If
these don’t work, go back to the website.
sudo apt-get install ipython python-opencv
python-scipy python-numpy python-pygame
sudo pip install
Note: This will take several hours to install on the Pi.
Perform the same post-install check to make sure
everything is operational:
from SimpleCV import Camera
NOTE: If this doesn’t work, try another webcam (there
can be some hardware-specific issues here). If you are still
unsuccessful, don’t worry — a fully supported Raspberry Pi
camera should be released by May 2013. I’ve had good
luck with both cameras I found: the Logitech V-UAR38
and Microsoft LifeCam VX-3000 (both circa 2006).
We will now build on our previous program. Say, for
example, you really want to see what is happening at your
house from a remote camera before hopping in the car
and rushing home. Python can take a picture of any
room/situation and attach it to an email.
Create a new file called
takepic.py. Here is the code:
from SimpleCV import Camera
myCamera = Camera()
frame = myCamera.getImage()
print(“picture capture successful!”)
May 2013 39