views:

1321

answers:

5

hello

we are working on a project for school, we have 2 pir motion sensors running off an arduino micro controller, we able to view the output of the serial port in both ardunio IDE and python IDLE

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

anyone have any ideas on how to achieve this?

thanks

+1  A: 

I'm not sure exactly which part of this process you're having trouble with, but here's a sketch of a solution:

You can use the pyserial library to communicate with the Arduino from python when the Arduino is plugged into the computer via USB.

On the python side, your code would look like this:

serial = serial.Serial("/dev/tty.usbserial-A6007btF", 38400) # the serial name you can see in the Arduino GUI - you might just need to say "COM1" on Windows
result = serial.readline(); # blocks until you get something from the Arduino
if result == "motion":
    # send email here

On the Arduino side, you'd just do something like this:

void loop()
{
    if(30 seconds have passed with motion)
        Serial.println("motion");
}

Make sense?

Jesse Rusak
+1: I've used pyserial for microcontroller communication and it rocks socks.
cdleary
A: 

from what I can tell the board has standard rs232 port. you can make a listener python script that accepts a signal from the board when the event occurs then fire off a method that sends the email. If you want a nicely designed piece look into Twisted, which has packages for serial port comm and mail. Otherwise check:

http://docs.python.org/library/email

http://www.varesano.net/blog/fabio/serial+rs232+connections+python

Vasil
A: 

You need to use the Serial library in your Arduino sketch. See the Serial Communication section on this page:

http://arduino.cc/en/Reference/HomePage

There are examples for the Serial library in the Communication folder of the Arduino environment.

On your host machine, use the pySerial module, as suggested by jder.

You might also find this page in the Arduino Playground to be a useful starting point:

http://www.arduino.cc/playground/Interfacing/Python

David Boddie
A: 

From the question, I think you already know how to read from the serial port.

So I'd suggest something like this

import time,smtplib

beginTime = time.time() + 86400 # stay one day ahead for now
while True:
  if serial port has values : # ie. motion detected
    beginTime = time.time()
  if time.time() - beginTime > 30 :
    mailObj = smtplib.SMTP('smtp_server_here', smtp_server_port)
    mailObj.sendmail('from', 'to..', 'message')
    beginTime = time.time() + 86400 # reset time

I hope that helps

Nikhil
A: 

hello thanks for the replies, definitely got us looking at this from a different view point

at this point we can send an email from python, we can read the arduino serial port in python, just having an issue putting it all together

this is what our python code looks like, at the while 1: is were 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 = "[email protected]"
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("[email protected]",
   "Alarm Alert!",
   "Both Motion Sensor A & B have been active for over # seconds",
   "stor_fight.jpg")

any tips would be much appreciated

thanks again for the replies

1) Why have you registered two different user accounts with the same name? 2) Could you update the original question with the request for clarification? That's the common practice, since your questions are editable. This is supposed to be an answer section.
cdleary
not to sure how to edit the main question, i was going to put this in the comment section of the question but it said i need more rep! lol