Building a video-enabled nest box

Motion

The motion detector is used to turn on the IR LEDs. It also serves as an additional security measure. In the event that the camera gives out, you will still be able to tell whether there is movement in the nest box. If the photoelectric sensor breaks, then the camera can turn on when the motion detector detects movement. Circuit diagram 1 [2] shows the wiring for this.

Software

The software helps to collect all of the data. The PHP script status.php (see the "Apache" box) continuously writes the activities to a MySQL database. A second script, reporting.php (Listing 5), evaluates the data and uses it to create a website. You can call up this site as needed using the browser on any computer that is part of the local network.

Apache

A simple way to expand the web server is to load modules. You should install the server under Raspbian using the command

sudo apt-get install apache2

Then, you can immediately use the web browser to test whether everything works. A simple HTML page should appear under http://localhost. It usually makes sense to install the PHP module together with the server, and this is done with:

$ sudo apt-get install php5 php5-mysql

Using the command below, you can write a PHP file to the WWW directory of Apache for a simple test. You will then find a lot of information about the PHP installation under http://localhost/index.php.

$ sudo echo "<?php phpinfo();?>" >   /var/www/index.php

Now you can set up a new database named nika and assign corresponding administrative rights to a user of the same name. The database contains only the nika table, the structure of which is described in Table 2. The script createDB.sql (Listing 3) can do this work for you. You should execute the script with the command:

$ mysql -u root -p < createDB.sql

Listing 4, or status.php, shows the PHP routine that reads the ports of the photoelectric sensors every two seconds after the GPIO pins are initialized. Once a sensor is triggered, the value 1 will be written into the corresponding column of the database. Then, the routine resets the sensors. You will figure out as you go along whether the wait time that was defined with the usleep() command is suitable or whether you need to shorten it. The routine queries the status of the motion sensors to either turn on or turn off the IR LEDs.The script runs in an infinite loop. The loop is started with nohup php status.php & in the background, so that it continues to run even when the corresponding terminal is closed.You should put Listing 5, reporting.php, in the directory for data from the web server. The listing consists of simple SQL queries and delivers little layout. You invoke the script on the local network via http://RasPi-IP/reporting.php.

Listing 3

createDB.sql

create database nika;
use nika;
create table nika (ts timestamp, go_in int , go_out int, move int);
GRANT ALL ON nika.* TO 'nika'@'localhost' IDENTIFIED BY 'nika';
FLUSH PRIVILEGES;

Listing 4

status.php

<?php
exec('/usr/local/bin/gpio mode 0 out');
exec('/usr/local/bin/gpio mode 1 out');
exec('/usr/local/bin/gpio mode 2 in');
exec('/usr/local/bin/gpio mode 3 in');
exec('/usr/local/bin/gpio mode 4 in');
while(true) {
  exec('/usr/local/bin/gpio read 2 2>&1', $goin , $rc);
  exec('/usr/local/bin/gpio read 3 2>&1', $goout, $rc);
  exec('/usr/local/bin/gpio read 4 2>&1', $move , $rc);
  $goin=$goin[0];
  $goout=$goout[0];
  $move=$move[0];
  if (($goin>0) or ($goout>0)) {
    $db = mysql_connect("localhost","nika","nika") or die("DB Connect error");
    mysql_select_db("nika");
    $q = "insert into nika values ( now(),$goin,$goout,$move)";
    echo ("\n$q\n");
    mysql_query($q);
    exec('/usr/local/bin/gpio write 0 1');
    exec('/usr/local/bin/gpio write 0 0');
    mysql_close($db);
  }
  if ($move>0) {
    exec('/usr/local/bin/gpio write 1 1');
  }
  else {
    exec('/usr/local/bin/gpio write 1 0');
  }
echo("in:$goin out:$goout move:$move\n");
usleep(2000000);
}
?>

Table 2

Columns in nika Table

Name

Data Type

ts

Timestamp (TIMESTAMP)

go_in

Integer (INT)

go_out

Integer (INT)

move

Integer (INT)

Listing 5

reporting.php

<html>
<body>
  <H2>10 most recent flight movements</H2>
  <?php
  $db = mysql_connect("localhost","nika","nika") or die("DB Connect error");
  mysql_select_db("nika");
  $q = "select * from nika order by ts desc limit 10";
  $ds = mysql_query($q);
  echo "<table border=1>";
  echo "<tr><td>ts</td><td>go_in</td><td>go_out</td></tr>";
  while($r = mysql_fetch_object($ds)) {
    echo "<tr><td>".$r->ts."</td>";
    echo "<td>".$r->go_in."</td>";
    echo "<td>".$r->go_out."</td></tr>";
  }
  echo "</table>";
  ?>
  <H2>Number of birds in the box</H2>
  <?php
  $q = "select sum(go_in) as go_in from nika";
  $ds = mysql_query($q);
  $r = mysql_fetch_object($ds);
  $in = $r->go_in;
  $q = "select sum(go_out) as go_out from nika";
  $ds = mysql_query($q);
  $r = mysql_fetch_object($ds);
  $out = $r->go_out;
  $sum = $in-$out;
  echo ("$sum<br>");
  mysql_close($db);
  ?>
</body>
</html>

MySQL

MySQL is a widely used open source database that is ideal for small to mid-sized projects. It is also one of the fundamental software packages of Raspbian and can therefore easily be installed via the package manager. The software will ask for the root password for the database, and you should make a note of it. Aside from this one step, installation usually runs with no further interaction. To test whether installation has been successful, you should connect with the server via the command mysql -uroot -p. At the MySQL prompt, type show databases, so you can inspect the databases that are already available.

For the most part, you will only need a few modules for this setup. These include a web server (most often Apache), a PHP plugin, a MySQL database, and the WiringPi library. You may already have these components on your system. Directions for installing these programs are found in the "WiringPi," "MySQL," and "Apache" sidebars.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content