PIR sensors + Arduino + Python + email alerts


Question

We are working on a project for school, and we have 2 PIR motion sensors running off an Arduino microcontroller. We are able to view the output of the serial port in both the Ardunio IDE and Python IDLE.

What we want to do next is, after about 30 seconds of motion being detected, sending out an email alert, seeing that we don't have Ethernet capability at this point, we figured the easiest way would be to grab the emailing through Python.

How to achieve this?

Update:

At this point we can send an email from Python, we can read the Arduino serial port in Python, but we just have an issue putting it all together.

This is what our Python code looks like, at the while 1: is where confusion happens:

import smtplib,serial

ser = serial.Serial(port=2, baudrate=9200)

from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os

gmail_user = "usr@gmail.com"
gmail_pwd = "pw"

def mail(to, subject, text, attach):
   msg = MIMEMultipart()

   msg['From'] = gmail_user
   msg['To'] = to
   msg['Subject'] = subject

   msg.attach(MIMEText(text))

   part = MIMEBase('application', 'octet-stream')
   part.set_payload(open(attach, 'rb').read())
   Encoders.encode_base64(part)
   part.add_header('Content-Disposition',
           'attachment; filename="%s"' % os.path.basename(attach))
   msg.attach(part)

   mailServer = smtplib.SMTP("smtp.gmail.com", 587)
   mailServer.ehlo()
   mailServer.starttls()
   mailServer.ehlo()
   mailServer.login(gmail_user, gmail_pwd)
   mailServer.sendmail(gmail_user, to, msg.as_string())
   # Should be mailServer.quit(), but that crashes...
   mailServer.close()

while 1: **// CONFUSION HAPPENS HERE //** <----------------------

   ser.readline()

   if ser.readline() = "motion" 

   do this mail sequence?

   mail("usr2@gmail.com",
   "Alarm Alert!",
   "Both Motion Sensor A & B have been active for over # seconds",
   "stor_fight.jpg")

Any tips would be much appreciated.

1
5
10/5/2012 6:21:44 AM

ser.readline()

   if ser.readline() = "motion" 

This code reads a line from the serial port twice and discards the first one.

You also need a double-equals for comparison, and the line will (I assume...) have carriage-return characters on the end of it.

You might want to try something like

if ser.readline().starts_with("motion")
1
8/1/2012 2:27:35 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon