Sunday, December 23, 2012

Pogoplug Security Camera

Pogoplug Security Monitoring System

Continuing on from our Raspberry Pi set-up of a streaming internet webcam, I have modified my setup to use a Pogoplug (or Dockstar, which is basically the same hardware) as my security camera system. The Pogoplug V2 Grey has a 1.2Ghz Marvel Kirkwood ARM9 processor while the Raspberry Pi has a 700Mhz Broadcom BCM2835 ARM176 core with hardware floating point. Basically the processing power of the Pogoplug is much more than the Raspberry Pi although graphically the Pi is much more capable with it's GPU.

But since my main purpose is to make a headless webcam security system (not plugged in to a TV or monitor), the Pogoplug is more suited for this application.

Here's a sample of the final output of my webpage:


















The top header is just a bunch of scripts I used to show the current temp, visitor info, current Winnipeg time and the time in Asia.

The webcam views are from local ports, but if you want this to be viewable from the internet, you would have to define routing tables using your router to route traffic from the internet port to a particular web stream.

In this case, if your pogoplug has an IP address of 192.168.1.99 and the webcam stream are from port 80 and 8080 (for each webcam), then you would have to define an incoming port (from the net, example port 88) and route it to local port of 192.168.1.99:80. If you have a DD-WRT router, you basically would have to just go to your router's IP (in my case 192.168.1.1) and pick the NAT/QoS tab, and setup Port From 88 to route to 192.168.1.99 and port 80...  This would mean, any reference to http://<youripaddress>:88 would automatically route to the pogoplug's port 80.



If your Internet service provider randomly assigns an IP address each time you connect to the net, you would need to know your new Internet IP address, but if you use a DNS redirector, you would just need to remember your website. 
In this example, I am using a free DNS redirector from DYNDNS to use the site <myfreesite>.gotdns.com and it is updated daily with my router's internet address. You can replace this with whatever web address you already have or subscribe to a free DNS redirector like DYNDNS. You can then automate your update to Dyndns by logging in to their site with your username/password and it will record your IP address as the address to go to whenever you logon.

DD-WRT has a DDNS tab that will automatically logon and update the DYNDNS table to your current IP.

 


Host name is your dynamic DNS website name, example: http://mywebsite.dyndns.com.

Here's a sample code form webpage:

Index.html

<!DOCTYPE HTML SYSTEM>
<html><head>
<title>Roland's webcams</title>
<meta http-equiv=refresh content="3600"></head><body>

<hr>
<table border="0"><tr>
<td>
<a href=http://myfreesite.gotdns.com:88/?action=stream>
<img src=http://myfreesite.gotdns.com:88/?action=stream height=260 width=360></a>
</td>
<td>
<a href=http://myfreesite.gotdns.com::89/?action=stream>
<img src=http://myfreesite.gotdns.com:89/?action=stream height=260 width=360></$
</td>

<td>
<a href=http://myfreesite.gotdns.com:90/?action=stream>
<img src=http://myfreesite.gotdns.com:90/?action=stream height=260 width=360></$
</td>
</tr>
</table>
<BR>


The first webcam grabs the output of my old NSLU2 (the predecessor of the Pogoplug) which is basically a NAS device that I reverted to a webcam server, while the latter two are hosted in my Pogoplug and runs two instances (actually 4, the other two are webcam image captures stored as JPGs) of MJPG-Streamer. 2 web services that serves the video stream (for each webcam) and 2 file streams that saves an image per second for the 2 webcams. The NSLU2 is not powerful enough to do image conversion (JPG to AVI) so I haven't been saving the stream.

The Pogoplug setup generates a webstream for each webcam (accessible from the internet from any device) and also a   jpeg of the image per second into a defined directory (cam1 and cam2). Every 2 hours, it shuts down the mjpg-streamer (using "pkill mjpg-streamer"), renames the cam directories  to camX-YYYY.MM.DD.hh.mm (X for the webcam number) and restarts the webcams/file streamer. Once it restarts the capture, it will then convert the renamed directory's JPG files to an AVI and when done, it zips the JPG into a file under the processed directory (deleting the jpgs in the process). After the zip process, it will delete the now empty camX-YYYY.MM.DD.hh.mm directory.

The JPG to AVI conversion is handled by ffmpeg and usually takes 40 minutes per camera. Zipping the files into a zip file takes around 5 minutes, so the basic processing time for both cameras is approximately 90 minutes. During those 90 minutes, any additional processing may push the processing time to conflict with the scheduled re-run of housekeeping.sh (i.e. a second instance of housekeeping.sh may run when the 90 minute process gets pushed beyond the 120 minute cron process). Since I made the pogoplug into a SAMBA server (use pacman to install SAMBA), I am able to go into the Pogoplug's drive from a Windows/Mac PC and copy the files (or watch the stored AVI) to my local machine. This eats up processing power from the Pogoplug, and if there was a huge file transfer process, housekeeping.sh may not have enough time to do all it's work within 120 minutes.

The default conversion is to an AVI file. Using better compression (MP4 format), it will take the Pogoplug more than an hour, so I opted to just zip the JPGs and convert those from a different PC as needed.



Basically my setup consists of:

1. Pogoplug Grey V2 (same as the more common Pogoplug Pink V2)
2. USB external portable 500GB hard drive
3. 2 pcs of Logitech Quickcam 9000 Pro webcams
4. Dlink Powerline Network adapter (or use a USB WIFI adapter/network port) - I'm using this because the location is a hard to reach.


The Pogoplug is running Arch Linux (the same version I installed in my Raspberry Pi), and I added the packages of mjpg-streamer, ffmpeg, and zip/unzip. (to install a Arch Linux Package, use pacman)

A great step-by-step instruction on how to install Arch Linux in a Pogoplug can be found here:
http://archlinuxarm.org/platforms/armv5/pogoplug-v2-pinkgray

After converting the Pogoplug to Arch Linux, just follow the same steps as with my previous posts on the Raspberry Pi Webcam server, but with modifications on the housekeeping.sh

housekeeping.sh:

#!/bin/bash
pkill mjpg_streamer
STRNAME1=cam1-$(date +%Y.%m.%d.%H%M)
STRNAME2=cam2-$(date +%Y.%m.%d.%H%M)
STRPATH1=/media/share/$STRNAME1
STRPATH2=/media/share/$STRNAME2
mv /media/share/cam1 $STRPATH1
mv /media/share/cam2 $STRPATH2
mkdir /media/share/cam1
mkdir /media/share/cam2
/usr/bin/mjpg_streamer  -i "input_uvc.so -d /dev/video0 -f 10 -r 960x720"\
  -o "output_http.so -p 80 -w /media/share/www"\
  -o "output_file.so -f /media/share/cam1 -d 1000"\
  -b
/usr/bin/mjpg_streamer  -i "input_uvc.so -d /dev/video1 -f 10 -r 960x720"\
  -o "output_http.so -p 8080 -w /media/share/www"\
  -o "output_file.so -f /media/share/cam2 -d 1000"\
  -b
for file in $STRPATH1/*.jpg; do
    printf -vsequenceImage $STRPATH1/%05d.jpg "$((++i))"
    [[ -e $sequenceImage ]] || \
        mv "$file" "$sequenceImage"
done
i=0
for file in $STRPATH2/*.jpg; do
    printf -vsequenceImage $STRPATH2/%05d.jpg "$((++i))"
    [[ -e $sequenceImage ]] || \
        mv "$file" "$sequenceImage"
done

ffmpeg -f image2 -i $STRPATH1/%05d.jpg /media/share/processed/$STRNAME1.avi

ffmpeg -f image2 -i $STRPATH2/%05d.jpg /media/share/processed/$STRNAME2.avi
zip -m /media/share/processed/$STRNAME1.zip $STRPATH1/*.jpg
rmdir $STRPATH1
zip -m /media/share/processed/$STRNAME2.zip $STRPATH2/*.jpg
rmdir $STRPATH2

The basic process is, at first run, kill all instances of MJPG-STREAMER, rename the cam1 and cam2 directory to a unique folder name, and then create a blank cam1 and cam2 directory. Launch MJPG-Streamer and store a picture every second. This process runs very quick so at most we only lose 1 second of video in the switch process. After that, the old cam1 directory is processed to rename all the files into a sequential file name convention, and the same is done for the cam2 directory.

The process takes a bit (approximately 5 minutes) and my old python script does this around 5 seconds, so I have yet to refine this step to make it run within the bash script.

The next step is to generate the AVI files from the JPG images, and this process takes around 40 minutes (each directory) and will probably eat around 70% CPU. However, this still leaves a bit more CPU muscle for basic stuffs like file copy, mpeg streaming and web services.

After the AVI generation process, the JPG files are zipped to a ZIP files and moved to the PROCESSED directory. The blank cam1 directory is then removed for a cleaner directory structure.



EDIT 6Mar2017: Here's my new script, using mencoder

#!/bin/bash
pkill mjpg_streamer
STRNAME1=pogoplug-$(date +%Y.%m.%d.%H%M)
STRDIR=/media/hdd/share/processed/$(date +%Y.%m)
if [ ! -d "$STRDIR" ]; then
   mkdir $STRDIR
fi
STRPATH1=/media/hdd/share/$STRNAME1
mv /media/hdd/share/cam1 $STRPATH1
mkdir /media/hdd/share/cam1
/usr/local/bin/mjpg_streamer -i "/usr/local/lib/input_uvc.so -d /dev/video0 -f 10 -r 1600x896"\
  -o "/usr/local/lib/output_http.so -p 80 -w /media/hdd/share"\
  -o "/usr/local/lib/output_file.so -f /media/hdd/share/cam1 -d 1000"\
  -b
if [ -f STRNAME1.txt ]
then
  rm STRNAME1.txt
fi
ls $STRPATH1/*.jpg > STRNAME1.txt
mencoder -nosound -mf fps=15 -o "$STRDIR/$STRNAME1.avi" -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=8000 mf://@STRNAME1.txt
rm -r $STRPATH1


I've modified my original script to make it more faster and removed the secondary step of looping in files to rename it to a sequential number.. Since the files are sorted by date (ascending), I just generate the a text file with the names of the pictures, pass it to mencoder, which will generate the AVI file. The file name generated will have the current date and time so that I will know the sequence. I've removed the ZIP process as I do not need the old files, but you can put it back if you want (just copy the previous script)...

3 comments:

EJ Blom said...

Nice tutorial. I also own a NSLU2 (with debian) for webcam capturing. Lately I obtained a Pogoplug V2 (with the 1.2Ghz) and also the dual core classic edition (with less memory). Great value for money! I am looking into grabbing pictures from a webcam. Thanks for the tutorial.

Unknown said...

Great Ideas and information..It helps me a lot..


Best Portable Video Camera

Anonymous said...

In the world every day new gadgets are launching with some new future. WorldEyeCam