Raspberry Pi web-based PIR Sensor Motion Detection

Raspberry Pi PIRIntroduction

Passive Infrared sensors (PIR sensor) measure the heat radiated from objects and they are commonly used in motion detection alarm systems. They are called passive sensors as they only detect infrared light and not transmit.

The PIR sensor circuit consists of a Pyroelectric sensor and a circuit board that contains a Micro Power PIR Motion detector IC, BISS0001 and other components. The input from the sensor is sent to the BISS0001 IC, which processes the signal to make the output pin HIGH or LOW accordingly.

A very simple circuit is shown below consisting of only a PP3 battery, LED and a resistor. The LED will turn on when the PIR sensor detects motion.

PIR detection circuit

However, in this project, we wanted to monitor how often people walk through a specific area. So we decided to use a Raspberry Pi to read the PIR and log the time that a motion was detected. The connections for the circuit is shown below:
Raspberry Pi PIR motion detection

Web-based logging

We wanted to be able to remotely look at the log through a web interface. So we proceeded into installing a web server on the Pi. The instructions to install a webserver on the Pi can be found at: Raspberry Pi Web-server installation

After that, we installed sqlite3:

sudo apt-get install sqlite3
sudo apt-get install php5-sqlite

Created a database:

sqlite3 PIRlog.db

In the SQLite shell, these commands were entered to create a table called pir:

BEGIN;
CREATE TABLE pir (timestamp DATETIME);
COMMIT;

Use the .quit command to exit the SQLite shell.
Use these commands to set the database’s owner to www-data:

sudo chown www-data:www-data PIRlog.db 

Python code

Afterwards, we created the following python script:

import time
import RPi.GPIO as GPIO
import sqlite3
GPIO.setmode(GPIO.BCM)

GPIO.setup(23,GPIO.IN)
GPIO.setup(24,GPIO.OUT)

while True:

	if GPIO.input(23)==True:
		GPIO.output(24,True)
		time.sleep(5)
		GPIO.output(24,False)
		conn=sqlite3.connect('PIRlog.db')
    		curs=conn.cursor()
		curs.execute("INSERT INTO pir values(datetime('now'))")
    		conn.commit()
	 	conn.close()

When motion is detected by the PIR sensor, its output pin goes high and this is read by the Pi. An LED is then toggled on and an entry is inserted in the database. The above script is not well optimised and can be easily improve; in this post, we just wanted to show the simplicity of making cool projects.

Now, how can you see the database?
We then created a php webpage to replace our default index.php located at /var/www/html on the Pi.

PHP webpage code

This is our php:

<html>
 <head>
  <title>Behind The Sciences Access log</title>
 </head>
 <body>
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL|E_STRICT);
$database = new SQLite3 ('PIRlog.db');
    //phpinfo();
$sql = "SELECT timestamp FROM pir";
$result = $database -> query($sql);
if (!$result) die("Cannot execute query.");
while ($row = $result -> fetcharray())
{
echo $row['timestamp'];
echo "<br>";
}
$database -> close();
date_default_timezone_set('UTC');
echo "<p>Current date and time: " . date("r") . "</p>";
echo '<p style="text-align: center;">Behind The Sciences 2016</p>';
?>
</body>
</html>

The webpage simply opens the sqlite database, reads the contents and displays it. Going into our favourite browser and typing the ip address of the Pi, we are greeted with this:
Pi Web Interface

Thanks for reading this post. We welcome comments and suggestions, please comment down here or send us an email at contact@behindthesciences.com

Add a Comment

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

Show Buttons
Hide Buttons