Geek IT Guide

My blog on linux, coding, web design, hardware hacking and everything else in between.


Using IP Security Cam as a motion sensor

Instead of adding this to my multi-part series on my SecurityPi System, I figured I would make a dedicated post just to outline this hack. With a security system there is alot of different sensors commonly used, from window/door sensors to PIR motion sensors.


However, in many cases you also might have some IP Camera’s as well, in my case I have a few Foscam FI8918W’s.   Now looking through the web interface I noticed it had an alarm feature that basically allows the camera to send you an email when it detects motion.  While that is useful, I felt it was a little overkill in my case.  Not to mention, if the internet is down, how can I fully utilize that feature in a security system?  So I scoured the internet, surely there was someone that thought to use an ip camera for a security purpose.

I did run across some people that were using the FTP feature on the camera to use it as a sensor.  In this scenario, they use a script on a central server to monitor a folder for new uploads.  While I think this is a great idea and would work, I wanted to do something that used a little less network I/O, so why not simulate a SMTP server on a port on my central server and make the ip camera think it is sending an email. This would use very little network I/O and would probably be a whole lot faster.

So to get started I needed to open the port on my server and have a script to read the data and return the correct smtp requests.

First things first, I needed to get that port open, in my case, since this is on my SecurityPi server it is a Raspberry Pi with Debian Wheezy.  The Following command handles opening the port:

sudo iptables -A INPUT -d SERVERIP -p tcp --dport PORTTOUSE -j ACCEPT

Next I logged into the camera and setup the following settings, other ip camera’s may be a little different, but as long as your settings match your server settings, then you should be ok.

First, I started with the Mail settings by editing the highlighted fields below:

ipcam mail settings

Next, I set up the alarm settings, this is the part that will vary, set the settings that work for you:

ipcam alarm settings

the settings above seemed to work for me best, but it will vary for everyone.

Finally, we need something to simulate the smtp server so the camera can think it is sending an email.

I needed to have the smtp responses available for the whole system to work, so I created the following file:


then, I ran the following to test all the settings up to this point:

nc -l -p 5000 < /path/to/smtpresponse

When the camera gets motion or you click the test button (on the camera’s web interface in mail settings). You will get a response like so:

camera smtp response

At this point I could see what data I would need so I begin throwing a script together.

I came up with the following:

# V1.0
# Chris Jones 12/21/2013
# Script used to convert email alerts from ipcamera into mochad.

# *In ipcameras settings set "sender" or "from" value to the value you will assign device in securitypi's web interface.

############ Settings ##################
# port to receive email alert
# port for Mochad (Default 1099)
#SMTP Response File

# monitor port for email requests
while true
        CAMERA=$( nc -l -p $CAMPORT < $SMTPFILE | awk 'NR==2{print $1}' RS="<" FS=">" )
        DATE=$( date +"%m/%d %H:%M:%S" )
        echo "$DATE Rx CAMSEC Addr: $CAMERA Func: Motion_alert_IPCAMERA" | nc localhost $MOCHADPORT

You can download it here.
As you can see a very simple script. I have the settings at the top to make it customizable, then an infinite loop at the bottom with the output piped to awk and then I extract the second line:

'NR==2{print $1'

After that, I then pull what is between “<" and ">“:

RS="<" FS=">"

This gets the “MAIL FROM” address and stores it as a variable “CAMERA”.
Next, I get date-time and format to match Mochad’s format, store it as “DATE”.
Finally, I echo the line with 2 variables so it matches Mochad’s x10 format.
*this line and the Mochad port can be commented out or excluded if your going to use this to trigger something else or some other purpose.

At this point you could run this any way you prefer, in my case I am going to handle it with init.d and have it run as a daemon in the background, this way I can control it like other services. A good guide to that is here, along with many other great guides.

That about sums up this post, please free to download the script and files and modify how you see fit for your needs. I would love to hear how it worked out for you and if you have any suggestions, please let me know in the comments below.


  1. SecurityPi Part 5 | Geek IT Guide says:

    June 5th, 2014 at 7:41 am (#)

    […] table, to keep track of the user and method of the changes (sensor, web, etc..). In a previous post I put together a quick hack that would allow me to use a ip security camera as a motion sensor, […]

Leave a Response