<$BlogRSDUrl$>

Thursday, January 31, 2013

Raspberri Pi and Motion Detection 

Taking inspiration from this Adafruit tutorial, I decided to play with a $10 motion detector I picked up at RadioShack and some python. My goal:  Don't play a sound when motion is detected. Instead, send my cell phone a text message.  This could be expanded to a variety of sensors.

Sending email can be a bit tricky from a home internet connection, as many internet service providers require that you use their outbound SMTP smarthost instead of sending email directly from your pi to the remote email address.  You will need to use the proper settings for your situation.  Also, if your cell phone number has an associated email to SMS service, check your cell phone contract to make sure you are not paying for incoming SMS messages.

Here is my first attempt, using wires and connectors salvaged from old computers, and some alligator clips to hold connections together.  The motion detector has a power pin, a ground pin, and a signal pin.  I connected the power pin on the motion detector to pin 2 (5v) on the pi, the ground to pin 6 (ground) on the pi, and the signal pin to pin 10 (GPIO 15).  Nothing started smoking, so all was well.






Here is the python script I used:
#!/usr/bin/env python
# This will write the date and time to a file when motion is detected.
# When motion is detected, it sleeps for a long time so we won't
# generate too much email. 

import time
from time import sleep
import smtplib

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

motion_sensor_pin = 15

#Set up inputs
GPIO.setup(motion_sensor_pin, GPIO.IN)

localtime = time.asctime( time.localtime(time.time()) )

while True:
    motion = GPIO.input(motion_sensor_pin)

    if motion:
        localtime = time.asctime( time.localtime(time.time()) )
        print(localtime)
        with open ('motion.log', 'a') as f:
                 f.write (localtime)
                 f.write ('\n')
        sender = 'my@email.com'
        receivers = ['receiver@there.com']
        message = """From: Me Here <my@email.com>
        To: You <receiver@there.com>
        Subject: Intruder Alert

        Motion detected:
        """ + localtime
        try:
           smtpObj = smtplib.SMTP('your.isp.smart.host')
           smtpObj.sendmail(sender, receivers, message)        
           print "Successfully sent email"
        except SMTPException:
           print "Error: unable to send email"
        # If motion was detected, sleep for a while.   
        sleep(600)

    sleep(0.01)

I then cleaned up the wiring and adhered a small breadboard to the top of the Raspberry Pi case:

Thursday, January 17, 2013

Playing with Arduino - Esplora 

http://arduino.cc/en/Main/ArduinoBoardEsplora

Connecting the Arduino to a computer.

On the Arduino:
#include <Esplora.h>

void setup() {
  // Initialize serial:
  Serial.begin(9600);
}

void loop() {
  // read the sensor:
  if (Serial.available() > 0) {
    int inByte = Serial.read();
    int fahrenheit = Esplora.readTemperature(DEGREES_F);
    int celsius = Esplora.readTemperature(DEGREES_C);
    // do something depending on the character received. 
    switch (inByte) {
    case 'f':   
      Serial.print("Temperature is: ");
      Serial.print(fahrenheit);
      Serial.println(" degrees Fahrenheit.");
      break;
    case 'c':   
      Serial.print("Temperature is: ");
      Serial.print(celsius);
      Serial.println(" degrees Celsius.");
      break;
      }
    }
  }
Then write a python script (with the help of pyserial), and set the correct serial port.
#!/usr/bin/python
import serial

ser = serial.Serial('/dev/ttyACM0', 19200, timeout=1)
ser.write('f')
line = ser.readline()
print line
ser.close
When you call the python script (and have permission to that serial port), you get the temperature.

This page is powered by Blogger. Isn't yours?