Python on the Pi – My trail camera

My old Pi – Too slow as a desktop PC

I’ve had this old Pi for quite a few years now. It’s so slow that it can’t be used for much other than playing around with it but it’s ideal for the purpose of being a trail camera. I fitted the PiCam module and also a WiFi adapter. This model, unlike the latest models, was pretty much a bare bones computer.

So I did a little Python programming to talk to the camera module and just recently included a facility to send me a mail whenever a photo was taken.

In this case too, I use an external infrared movement detector which has a normally closed contact. The contact is wired between ground and pin 17 on the GPIO port. Contact bounce has been handled within the Python program. A simple solution with exciting possibilities.

Here is my latest Python code.

from picamera import PiCamera
from pygame import *

screen = display.set_mode ((640, 128))
display.set_caption ('Eds trail camera INITILIZED  - Press q to Quit')

import smtplib
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, GPIO.PUD_UP)

camera = PiCamera()

stop = False
counter = 0

def my_callback(channel):
    snaptime = time.strftime('%A %d %b %Y %H:%M:%S')

    smtpUser = 'yourmailaddress@gmail.com'
    smtpPass = 'yourpassword'

    toAdd = 'yourdestinationaddress@gmail.com'
    fromAdd = smtpUser

    subject = snaptime
    header = 'To: ' + toAdd + '\n' + 'From: ' + fromAdd + '\n'+ 'Subject: '  + subject
    body = 'Eds trail camera was triggered through movement detection. '

    print header + '\n' + body

    s = smtplib.SMTP('smtp.gmail.com', 587)

    s.ehlo()
    s.starttls()
    s.ehlo()

    s.login(smtpUser,smtpPass)
    s.sendmail(fromAdd, toAdd, header + '\n\n' + body)

    global counter

    # DEBOUNCE Code

    if (counter >= 10):
        counter = 0

        camera.annotate_text_size = 30
        camera.annotate_text = snaptime

        time.sleep(0.1)	    # Allow 100 ms for IR LED's to switch on (At night)
        camera.capture('/home/pi/snapshot_' + snaptime + '.jpg')

        print ('Camera was tiggered on ' + snaptime)

camera.rotation = 180

#camera.start_preview()

GPIO.add_event_detect(17, GPIO.RISING, callback = my_callback)

while (stop == False) :

   time.sleep(0.1)
   counter = counter + 1    # Allow for trigger contact bounce

   for e in event.get():
       if e.type == KEYDOWN:
           if (e.key == K_q):
               print ('Trail camera stopped')
               stop = True
           else:
               print('Press q to Quit')

#camera.stop_preview()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s