RaspaberryPI – motion detector – catch the thief;)

Out of the need of the moment, an application will be created to detect cats stealing ham in the kitchen. To make it more challenging I will use my RaspaberryPI computer with bought camera in second-hand shop;)

The aim is to catch, which of my cats is stealing ham from the kitchen and then vomits;) The revenge will be sweet – visit at vet;)

Program will be simple – Python script using well-known Motion library:

https://motion-project.github.io/motion_config.html

First of all install Motion on RaspaberryPI:
sudo apt install motion

then edit its config file

sudo vi /etc/motion/motion.conf

edit these lines:

  • Specify a target_dir in the configuration file.
  • Enable the web stream in the configuration file
  • Enable the web control in the configuration file
  • Enable motion detector box via [when motion will be detected – it will be overlayed with redbox]:
  • locate_motion_mode=redbox

Now start this deamon:

sudo service motion start

To check status use:

sudo service motion status

To stop this deamon use:

sudo service motion stop

Now, when motion capture deamon is running you can check live streaming using this url in webbrowser on Raspaberry PI:

http://localhost:8081

You can also retreive the live streaming using differnt computer in your local network:

– first retrieve IP address of RaspaberryPI

ipconfig -a

and then use this IP address on remote computer and port:8081

Now it is time to write proper script, which could look like this and could be added to crontab to run every 30 mins;)

Whenever motion is detected, take last snapshot file and send email using Gmail and remove all unused files.

author:magdaka

CatCacher ver. 1.0

import os

import subprocess

import smtplib

import socket

from email.mime.text import MIMEText

import datetime

to=”username@gmail.com”

gmail_user=”username@gmail.com”

gmail_password=”passwd”

print „Connect to SMTP server”

smtpserver=smtplib.SMTP(‚smtp.gmail.com’,587)

smtpserver.ehlo()

smtpserver.starttls()

smtpserver.ehlo()

smtpserver.login(gmail_user,gmail_password)

today=datetime.date.today()

print „Retrieve last motion detector”

listofImgs=os.listdir(‚/tmp/motion’)

listofImgs.sort()

print listofImgs[len(listofImgs)-1]

body= ” Cat was caught :%s- check attachment:” % today

body=body +”%s” % listofImgs[len(listofImgs)-1]

msg=MIMEText(body)

msg[‚Subject’]=’Alarm Raspberry PI w dniu %s’ % today

msg[‚From’]=gmail_user

msg[‚To’]=to

smtpserver.sendmail(gmail_user,[to],msg.as_string())

smtpserver.quit()

print body

print „Email was sent”

print „Delete files”

for file in listofImgs:

print „Removing file:”+file

os.remove(‚/tmp/motion/’+file)

My cat was caught red-handed using this program, so on Monday it is time to go to vet!

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *