Wireless IP Camera – Part 4: build your own IP camera software

Here’s the code in motion-detect.c:

Let’s start with detect_motion(). This function is the entry point for the program. It does some initialization to prepare GPIO and sets a timer to fire a timer signal (SIGALRM) at a certain interval. We’ll use the timer to keep track of time between checks of the PIR sensor on the GPIO. We also register the destructor function with the terminate signal to handle clean-up. After that the program goes into an endless loop to allow the timer and the GPIO checks to do their things. The last lines of the program are never executed because the destructor function calls exit() which ends the program.

Each time the timer signal arrives the messagehandler() function is fired. In this function the PIR sensor is read on the GPIO pin using checkGPIO(). CheckGPIO() simply reads a character from the system file that represents GPIO communication. If a signal is detected it will return ‘1’. If there is movement then the Payload() function is called to act upon the movement.

Let’s consider the Payload() function:

The Payload() function basically

  1. Grabs images with the webcams using Store_Images, which stores the images on disc
  2. It sends a notification e-mail to the defined notification address (see #defines)
  3. It sends an e-mail with image attachments to a second address (I use photobucked to mail these images to an off-site place).

After these steps the Payload() function checks if the PIR sensor is still sensing movement. We want to wait until movement has passed before grabbing more images. Otherwise the camera is recording a lot of pictures with the same content. When the checkGPIO() function returns ‘0’ movement has passed and the PIR sensor is ready to detect movement again. So the function only continues when it reads a ‘0’ from the sensor.

Let’s take a closer look to the Store_Images() function. The signature of the function is:

As you can see the function returns a struct of the type filename_store (see motion-detect.h). The struct contains three fields:

  1. The nr of filenames grabbed from the camera
  2. A list of names of files grabbed from the camera
  3. The size of memory used to store each filename (nr_of_chunks)

We do not know in advance how much memory to reserve for the list of filenames since we do not know in advance how many images we’ll grab from the camera . We’ll need a way to dynamically allocate memory for this filename list. We’ll use this struct for our bookkeeping and we’ll use and Obstack to manage the memory.

The Obstack is an addition in linux to the standard C library to handle dynamic memory management. It consists of a few macros that call malloc() and free() in a convenient and safe way. The way Obstack works is that you “start” a stack, grow it in size as needed, and “close” the stack. Upon closing you’ll receive a pointer to the memory region that contains the data.

In the Capture_Images() function you can see how it’s done:

I rely on the system() function to call the webcam software fswebcam to grab the actual images.

Now that we have all this code in place it would be nice if we could make a daemon out of this program so it can be run as a background process on the bifferboard and controlled as a server using start/stop commands from the command line.

On the next page I’ll explain how to daemonize the program…

  • StumbleUpon
  • Facebook
  • Twitter
  • Google Bookmarks
  • email
  • RSS
This entry was posted in Maker Recipes, Security. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">