Hey guys,
We are finally at the stage where we can wrap up our Raspberry Pi motion detection project. If you recall in the the last part we had recorded our first “motion detection” video and left some lines unaltered in the motion.conf file for scripts, we will be using those lines in the motion.conf today.
We will be setting up two scripts:
- sms.py
- movie.py
Python-Text & Email
In general *.py files are python files and python is a leading programming and scripting language these days. I will be providing these files below and you just need to follow the process listed in this tutorial to run the files, but first we must do a few setup steps:
Google Voice:
- I recommend you register a new email address and configure google voice with it.
- Text your regular phone number using google voice
- Once you receive the text on your phone from the google voice number, text it back.
- Go to your gmail – email, and you will notice the text is also available in your email.
- If you notice the “from” field there will be a special email string created by google voice for the communication between your google voice number and physical phone, it will look something like this:
'1xxxxxxxxxx.1xxxxxxxxxx.WvS-mS5WHd@txt.voice.google.com'Just make a note of this, we will use it in our script.
Google Account Configuration: Turn access using less secure apps ‘ON’
- Go to google accounts – security section
- scroll down to find the following section:
This by default should be off, we need to turn this ‘on’ and allow access with less secure apps, because our sms.py and movie.py are python scripts (progams/apps) which essentially going to log into the gmail account in a less secure manner without using any form of encryption. Although, as I had recommended at the very beginning of this tutorial- “create a new gmail account” – so that we are comfortable with this “less secure” access.
Python Scripts:
Python-Text & Email
(I am definitely doing this to boost traffic to my website.)
Anyway once you have the scripts make sure you edit the following lines and give script your gmail username and password, also the special google voice string in the sms.py script. These scripts just function like a email client and send emails using using your account.
Lines to edit, only edit inside the quotes ‘______’ :
fromaddr = 'yourmail@gmail.com'
toaddrs = 'anymail@anything.com','1xxxxxxxxxx.1xxxxxxxxxx.WvS-mS5WHd@txt.voice.google.com' password = 'password'
Once this is done, save the files, we will transfer them on to our raspberry pi. This is a small clip of me testing the text feature:
Raspberry Pi and WinScp:
Start your Raspberry Pi
Open terminal and create a new folder called pyscripts at /home/pi/pyscripts
mkdir /home/pi/pyscripts
Open Winscp and transfer the sms.py and movie.py to this folder.
Now configure the motion.conf which is located at /usr/local/etc/motion/motion.conf
I have uploaded an updated version of motion.conf with the python files above, which you can refer.
Essentially the only lines that have changed in motion.conf are:
# Command to be executed when an event starts. on_event_start python3 /home/pi/pyscripts/sms.py # Command to be executed when a movie file is closed. on_movie_end python3 /home/pi/pyscripts/movie.py %f
Now you are all set to test the whole system out by simply starting motion by issuing the following command on terminal console:
cd /usr/local/bin sudo motion
Your motion software should now be working, it will record video when it detects motion, it will send you a text alert and also email you the video.
Thanks guys, this concludes our first project using Raspberry Pi, hopefully we can do some other fun projects in other categories soon. Do subscribe to mailing list to know about the latest projects that are posted here.
The full tutorial for the above is also available on youtube as below: