Sunday, July 28, 2013

Fully Automated Beehive Surveillance

I've finally managed to acquire and configure all of the different elements needed to enable continuous, around-the-clock video monitoring of the inside of my beehive.  The camera inside my hive is the same one described in my previous posts, but I no longer have to connect it directly to my laptop in order to use the camera.  Now, the camera is controlled by a Raspberry Pi housed in an aluminium box on the underside of the hive with videos periodically being uploaded to my Network Attached Storage (NAS) device via a wifi connection.  When ever I want to view the latest internal video of my hive, I simply connect to my server and view the video.  

Setting up this system took a lot of research and experimentation, so I'll probably require a series of posts to completely explain it.  For now, I will describe what steps I took to configure the Raspberry Pi.  


Initial configuration - 



To configure my Raspberry Pi, I took the following steps:

1) Create root (su) account:
$sudo passwd root
enter password for root

2) Create new user, transfer groups from pi to new user, and delete pi:
# adduser username
# groups pi
output with be something like: pi adm dialout cdrom ...
copy the groups from this output for use in the following
# usermod -a -G pi,adm,dialout,cdrom... username
in this case you are replacing the spaces in between the groups in the output with commas
# groups username
this is to confirm the new user now has the same groups as pi
# deluser -remove-home pi
this will remove pi's home directory as well as the user.

3) Once root account and username are created with strong passwords, connect to internet and run the following as root:
# apt-get update
# apt-get upgrade

4) Auto-login:
# nano /etc/inittab
Change it from:
1:2345:respawn:/sbin/getty --noclear 38400 tty1
to:
1:2345:respawn:/bin/login -f username tty2 </dev/tty1 >/dev/tty2 2>&1

5) Auto desktop launch:
# nano /etc/rc.local
add
su -l username -c startx
at the end above “exit 0”

6) To change default keyboard to us instead of uk:
# nano /etc/default/keyboard
change 'gb' to 'us'


Configuring motion -

For my project, I decided to use the program 'motion' to control the camera. As a program, motion has a wide variety of user options that can be set in the motion configuration files.  For my project, I mode the following changes

  1) threshold 0  - this disables the motion activated filming
  2) norm 1 - I forget what this does
  3) rotate 90 - rotates any videos or images captured with motion 90 degrees right
  4) ffmpg timelapse 20 - enables time-lapse movies and sets the time between frames to 20 seconds

Connecting to the NAS - 


To setup the NAS connection, I needed to first create a local mount point
# mkdir /home/username/nfsmount

After that all I need to do is enter the mount command
# mount -v -t nfs 192.168.2.102:/mnt/NASName -o vers=2,nolock /home/username/nfsmount

However, because I was eventually automating this entire process, I instead had to enable the NAS to be mounted by a regular user which required an additional entry into fstab.   
# nano /etc/fstab
add '192.168.2.102:/mnt/NASName /home/username/nfsmount nfs rw,nolock,noauto,user 0 0'

To mount and unmount NAS as a regular user:

$ mount /home/username/nfsmount
$ umount /home/username/nfsmount


Bringing it all together - 

For the final element of this project I needed to create a small script that would automatically start at boot and continue to run in the background. To do that I had to learn some new things about using shell scripts.  Here is the main shell script that I eventually settled upon:  


#!/bin/sh
sleeprun(){
    #sleep 86400 - one day
    for process in $(pidof motion)
    do
        kill $process
    done
    mv /home/
username/Videos/motion/*.mpg /home/username/Videos
    motion -c /etc/motion/motion.conf
    sleep 680
        }

load2NAS(){
    mount /home/
username/nfsmount
    sleep 15
    mount /home/
username/nfsmount
    mv /home/
username/Videos/*.mpg '/home/username/nfsmount/Shared Files/Videos'
    sleep 5
    umount /home/
username/nfsmount
    }

while :
do
    sleeprun
    load2NAS
done


The first function kills any motion programs currently running, moves any videos in the motion output folder to a different local directory, restarts the motion daemon, and, then, goes to sleep for however many seconds you want it to sleep.  The second function mounts the NAS to the Raspberry Pi, sleeps for 15 seconds, tries to mount the NAS again in case the first attempt failed, moves any videos in the secondary local directory to a folder on the NAS and unmounts the NAS.  Finally, at the end of the script I have an infinite loop set to run the two functions over and over.  


In order to have this script start up at boot, I had to add 'su
username -c /home/username/scripts/motion_script.sh &' to '/etc/rc.local' just above "exit 0" and 'su -l username -c startx'.  To give rc.local permission to execute motion_script.sh, I had to enter:
$ chmod +x /home/username/scripts/motion_script.sh

Finally, when all was done, I decided to disable the gui interface on boot by commenting out “su -l username -c startx” in rc.local. 

First videos - 

Earlier today I was able to install everything in the hive and I've already gotten the first video.  This videos (below) shows images from both before and during the installation and, consequently, isn't very good.  However, it is still the first video.  



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.