Author Archives: lolouk44

Bash is dead, long live Python

I’ve started a major update of my main script.
Until now I was using bash to control the GPIO of the Raspberry Pi, but it was just too slow and was using too much CPU resources.
So today I’ve transcripted everything from Bash to Python using the functions from https://code.google.com/p/raspberry-gpio-python/

Well I say everything, I’m only missing the piezo buzzer controller as the functions above do not yet include PWM which is needed to drive the buzzer.
I’ll see if there is a way round until this function becomes available.

Watch this space…




Mobile View

I’ve added today a plugin to the site to allow quicker / better viewing when using mobile browsers.
For those of you who use WordPress and are interested in knowing, the plugin I use is WordPress Mobile Pack







Control Panel Update

A short but needed update to the RPi Control Panel has been made so that I can now (remotely) control the Web Cam motion detection by simply clicking on the Camera icon

I’ve also update the scripts so that I can (remotely) control the buzzer

I’ve updated both scripts in RPi Web Control

The next thing I’ll “play” with will be a Digital Temperature And Humidity Sensor so I can measure temperature, humidity and compute dew / frost points. This will be based on this tutorial




Home made PIR sensor for Raspberry Pi

I was looking to add another PIR to my collection of Raspberry Pi gadgets.
I have an old Glade Sense & Spray air freshener and thought: “there must be a way I can reuse this to use the built in PIR.
And so I tinkered.

And it couldn’t be easier, you only need:

  1. 1 x “old” Glade Sense & Spray air freshener
  2. Some wires for the connection to the RPi1 x “old” Glade Sense & Spray air freshener
  3. A soldering iron to melt the lead on the printed board and solder your own wire

Best of all, you won’t need batteries to run it anymore, since the RPi provides +3.3V (and the original set up was using 2 x 1.5V AA batteries)
Second bonus, you get a visual notification of the movement via the built in LED.

Instructions:

  1. Open the box, remove the old spray cartridge and batteries
  2. Unscrew the back plate
  3. Remove the shafts and the motor
  4. Unclip the board
  5. The connection to solder your wire to is shown below
Inside view of the Glade Sense and Spray

Inside view of the Glade Sense and Spray

Note the red arrows showing the clips, the blue arrow showing the pin to connect to and the – and + signs to power the PIR

 

From then on, simply solder a wire to the connection shown. Be careful not to create a short circuit with the other pins.

Once done, you can connect the wire soldered on the blue arrow pin to one of the Raspberry Pi GPIO pins.
The pin will receive a status of 1 when movement is detected, 0 otherwise.

See picture of final “product” on my wall:

Glade PIR on the wall

Glade PIR on the wall

Important Note: Once movement is detected, the status received at the Pi will remain on 1 until the pre set time out (~20 min) has elapsed. It is unfortunately not possible to amend this time out. I’m guessing one of the 2 capacitors affects the time out, so removing it and replacing it with a smaller one could reduce the time out, but I’m not sure. I’m just guessing. Feel free to comment if you have managed to achieve a shorter time out.

Edit (15/02/2013): Now that it’s all up and running, I realised that although I called it a PIR (and I’m using it as one) and even though this is what it was designed for, the sensor actually measures the change in light conditions. Some of the drawbacks are that it won’t work at night and that a sudden change in lightning conditions, even without movement will trigger it. If you want a real PIR, I suggest this one which works really well with the Pi: Tandy Archer PIR Module Sensor




Using a Raspberry Pi as a CCTV

I’m using a Raspberry Pi to control a webcam running a CCTV with motion. The hardware setup is the following:

  • Raspberry Pi Model B
  • “Old PC” running motion
  • Logitech C270 HD connected to “Old PC”
  • An “old” Nexus S with IP Webcam used as a network camera
  • LEDs for visual info on whether motion detection is active or not

The software setup is the following:

  • Raspian “Wheezy”
  • Tightvnc for headless control
  • motion
  • Home made bash scripts to communicate with the Pi GPIO ports and monitor the door lock, control the LEDs and motion

Files are automatically uploaded to my Dropbox account so that I can check the video in the event of activity detected, but also should the worst happen, I have access to the videos showing who stole my Pi ! So that the motion detection only works when needed, I’ve connected my RPi to the lock in my door: When the door is locked, motion detection is active. Otherwise the detection bit is “dormant”.

11 Sept 2015: I’ve moved house and decided to go away from the “poor” quality of the webcam and smartphone (no night vision, when it rains on the window it blurs the pictures and triggers “false” motion due to the falling water drops) and plan on buying “real” CCTV cameras in the near future







RPi Web Control

I have made progress and based on an instructables guide (see my previous article) I completed the design of my Web RPi control panel.
See below screenshots.
RPi_Web_Control_1

RPi_Web_Control_2

And here is the code for those who want:

GPIOServer.sh:

# Script created by Laurent Mahe from Daniel Curzon's original script (http://www.instructables.com/member/drcurzon)
# Initial version created December 2012

###################################
#####  EDIT THESE BEFORE USE  #####
###################################
mysqlusername="****"
mysqlpassword="****"
mysqlhostname="192.168.0.24"

#######################################################################
# setup:
#	Program the GPIO correctly and initialise the different parameters
#######################################################################

Setup ()
{
	printf "\033c"
	echo "Setup in progress..."
	Door_Lock=8
	MyLED=0
	MyLED_Status=0
	AlarmStatus=0
	MyLight=3
	MyPIR=4
	MyPIR2=5
	MyBuzzer=0

	#Invoke GPIO
	#echo "0" > /sys/class/gpio/export
	#echo "17" > /sys/class/gpio/export
	#echo "18" > /sys/class/gpio/export
	#echo "22" > /sys/class/gpio/export
	#echo "23" > /sys/class/gpio/export

	echo "in" > /sys/class/gpio/gpio0/direction
	echo "out" > /sys/class/gpio/gpio17/direction
	gpio -g mode 18 in
	echo "in" > /sys/class/gpio/gpio22/direction
	echo "in" > /sys/class/gpio/gpio23/direction
	echo "in" > /sys/class/gpio/gpio24/direction

	curl http://192.168.0.24:8080/1/detection/pause > /dev/null 2>&1
	curl http://192.168.0.24:8080/2/detection/start > /dev/null 2>&1
    $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber=91";)
    $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber=92";)
	if [ `gpio read $Door_Lock` = 1 ]; then
	  echo "Lock Status = [1] Unlocked" 
	else
	  echo "Lock Status = [0] Locked"
	  gpio write $MyLED 1
      $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='0'";)
      $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='17'";)
	  echo -e `date +%F_%T` "Activating motion detection...\n"
	  curl http://192.168.0.24:8080/1/detection/start > /dev/null 2>&1
      $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber=91";)
      $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber=92";)
	  waitDoor_Unlock
	fi
	Update_MySQL
}

#######################################################################
# waitDoor_Lock:
#	Wait for the door to be locked. Because we are using pin 17 (which
#	has a pull up resistor) we wait for the signal to go low indicating
# 	the door has been locked, then wait 3 min before activating motion.
#######################################################################

waitDoor_Lock ()
{
  echo -n "Waiting for door to be locked... "
  while [ `gpio read $Door_Lock` = 1 ]; do
    Update_MySQL
  done
  echo -e "\n"`date +%F_%T` "Countdown 3 min..."
  gpio write $MyLED 1
  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='0'";)
  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='17'";)
  sleep 180
  echo -e `date +%F_%T` "Activating motion detection...\n"
  curl http://192.168.0.24:8080/1/detection/start > /dev/null 2>&1
  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber=91";)
  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber=92";)
}

#######################################################################
# waitDoor_Unlock:
#	Wait for the door to be unlocked. Because we are using pin 17 (which
#	has a pull up resistor) we wait for the signal to go high indicating
# 	the door has been unlocked.
#######################################################################

waitDoor_Unlock ()
{
  echo -n "Waiting for door to be unlocked... "
  while [ `gpio read $Door_Lock` = 0 ]; do
    gpio write $MyLED $MyLED_Status
    if [ $MyLED_Status = 0 ]; then
      MyLED_Status=1
    else
      MyLED_Status=0
    fi
    Update_MySQL
  done
  echo -e "\n"`date +%F_%T` "Pausing motion detection...\n"
  curl http://192.168.0.24:8080/1/detection/pause > /dev/null 2>&1
  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber=91";)
  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber=92";)
  gpio write $MyLED 0
}

#######################################################################
# SetBuzzer:
#	Plays buzzer
#	Use: SetBuzzer x y where x is Freq and y is length
#######################################################################

SetBuzzer ()
{
  local note="$1" time="$2"
  if test "$note" -eq 0 ; then
    gpio -g mode 18 in
  else
    local period="$(perl -e"printf'%.0f',600000/440/2**(($note-69)/12)")"
	gpio -g mode 18 pwm
    gpio pwmr "$((period))"
    gpio -g pwm 18 "$((period/2))"
    gpio pwm-ms
  fi
  sleep "$time"
}

#######################################################################
# Update_MySQL:
#	Checks PIR + Photo-Resistor:
#	if no movement after 23:00 and light is on -> Alarm
#	if door is unlocked and no light after 23:00 -> Alarm
#	Finally update MySQL table for Web Control Panel
#######################################################################

Update_MySQL ()
{
	  #checks time of day. If past 23:00, sets alarm if (no light AND door unlocked) OR (light AND no movememnt)
	  MyTime=`date +%H`
#	  if (( 1 )); then
	  if (( (${MyTime:1:1} > 2 && ${MyTime:0:1} == 2) || (${MyTime:1:1} < 4 && ${MyTime:0:1} == 0))); then
		if [ `gpio read $MyPIR` = 0 ] && [ `gpio read $MyPIR2` = 0 ] && [ `gpio read $MyLight` = 1 ]; then
		  echo -e `date +%F_%T` "\n"" Late, lights on and no movement --> [ALARM]"
		  AlarmStatus=1
		  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='18'";)
		  SetBuzzer 90 0.5
		else
		  AlarmStatus=0
		  gpio -g mode 18 in
		  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber='18'";)
		fi
		if [ `gpio read $Door_Lock` = 1 ] && [ `gpio read $MyLight` = 0 ]; then
		  echo -e `date +%F_%T` "\n"" Late, lights off and Door not locked --> [ALARM]"
		  AlarmStatus=1
		  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='18'";)
		  SetBuzzer 90 0.5
		else
		  AlarmStatus=0
		  gpio -g mode 18 in
		  $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber='18'";)
		fi
	  fi
	  if ((AlarmStatus == 1)); then
			AlarmStatus=0
			gpio -g mode 18 in
			$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber='18'";)
	  fi

	#Get Door Lock Status
	if ((`cat /sys/class/gpio/gpio0/value` == 0)); then
		#set Door status in SQL to 1 (locked)
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='0'";)
	else
		#set Door status in SQL to 0 (unlocked)
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber='0'";)
	fi

	#Get Photo Resistor Status
	if ((`cat /sys/class/gpio/gpio22/value` == 1)); then
		#set status in SQL to 1
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='22'";)
	else
		#set status in SQL to 0
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber='22'";)
	fi

	#Get PIR1 Status
	if ((`cat /sys/class/gpio/gpio23/value` == 1)); then
		#set status in SQL to 1
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='23'";)
	else
		#set status in SQL to 0
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber='23'";)
	fi 

	#Get PIR2 Status
	if ((`cat /sys/class/gpio/gpio24/value` == 1)); then
		#set status in SQL to 1
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='24'";)
	else
		#set status in SQL to 0
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber='24'";)
	fi 

	#Get Door LED
	if ((`cat /sys/class/gpio/gpio17/value` == 0)); then
		#set Door status in SQL to 1 (locked)
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber='17'";)
	else
		#set Door status in SQL to 0 (unlocked)
		$(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber='17'";)
	fi

	#Set Buzzer
	if [ $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='18'";) == "1" ]; then
		if (($MyBuzzer == 0)); then
			SetBuzzer 90 0.5
			MyBuzzer=1
		fi
	else
		gpio -g mode 18 in
		MyBuzzer=0
	fi

	CAM1=`curl -s http://192.168.0.24:8080/1/detection/status`
	MyCAM1=${CAM1:137:6}
	if [ $MyCAM1 == "ACTIVE" ]; then
		#update SQL as active
	    $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber=91";)
	else
		#either in pause or not available
	    $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber=91";)
	fi

	CAM2=`curl -s http://192.168.0.24:8080/2/detection/status`
	MyCAM2=${CAM2:137:6}
	if [ $MyCAM2 == "ACTIVE" ]; then
		#update SQL as active
	    $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber=92";)
	else
		#either in pause or not available
	    $(mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber=92";)
	fi
}

#Start Loop
while :
do
  Setup
  waitDoor_Lock
  waitDoor_Unlock
done

 

control.php:

<HTML>
<HEAD>
<title>RPi Control Panel</title>
<link rel="stylesheet" type="text/css" href="StyleSheet.css" />
<HEAD>
<STYLE type="text/css">
	BODY
	{
	background-attachment: fixed;
	background-image:  url(RPi_Bkgrd.jpg);
	background-repeat: no-repeat;
	background-position: center center;
	}
</STYLE>
<script src="jquery-1.8.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready (function () {
	var updater = setTimeout (function () {
	$("div#myDiv").load("index.php","action=refresh");
	}, 500);
});
</script>
</HEAD>
<BODY>

<?php
session_start();
//////////////////////////////
// EDIT THESE TWO VARIABLES //
//////////////////////////////
$MySQLUsername = "****";
$MySQLPassword = "****";

/////////////////////////////////
// DO NOT EDIT BELOW THIS LINE //
/////////////////////////////////
$MySQLHost = "127.0.0.1";
$MySQLDB = "gpio";

/* gets the data from a URL */
function get_data($url) {
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

If (($MySQLUsername == "USERNAME HERE") || ($MySQLPassword == "PASSWORD HERE")){
	print 'ERROR - Please set up the script first';
	exit();
}

$dbConnection = mysql_connect($MySQLHost, $MySQLUsername, $MySQLPassword);
mysql_select_db($MySQLDB, $dbConnection);

$tbl = '';
$tbl .= "<table name='GPIO' border='1' cellpadding='5'>";
$tbl .= "<tr><th>GPIO #</th><th>GPIO Description</th><th>Status</th></tr>";

//Door Lock
$strSQL1 = mysql_query ("SELECT pinNumber, pinStatus FROM pinStatus WHERE pinNumber=0");
$strSQL2 = mysql_query ("SELECT pinNumber, pinDescription FROM pinDescription WHERE pinNumber=0");
$pinRow = mysql_fetch_assoc($strSQL1);
$descRow = mysql_fetch_assoc($strSQL2);
$pinNumber = $pinRow['pinNumber'];
$pinStatus = $pinRow['pinStatus'];
$pinDescription = $descRow['pinDescription'];
If ($pinStatus == "0"){
	$image = "Lock-Off.png";
} else {
	$image = "Lock-On.png";
}
$tbl .= "<tr>";
$tbl .= "<td align='center'>" . $pinNumber . "</td><td>" . $pinDescription . "</td><td><img src=" . $image . " width='50' border='0'></td>";
$tbl .= "</tr>";

//Door LED
$strSQL1 = mysql_query ("SELECT pinNumber, pinStatus FROM pinStatus WHERE pinNumber=17");
$strSQL2 = mysql_query ("SELECT pinNumber, pinDescription FROM pinDescription WHERE pinNumber=17");
$pinRow = mysql_fetch_assoc($strSQL1);
$descRow = mysql_fetch_assoc($strSQL2);
$pinNumber = $pinRow['pinNumber'];
$pinStatus = $pinRow['pinStatus'];
$pinDescription = $descRow['pinDescription'];
If ($pinStatus == "0"){
	$image = "LED-Off.png";
} else {
	$image = "LED-On.png";
}
$tbl .= "<tr>";
$tbl .= "<td align='center'>" . $pinNumber . "</td><td>" . $pinDescription . "</td><td><img src=" . $image . " width='50' border='0'></td>";
$tbl .= "</tr>";

//Buzzer
$strSQL1 = mysql_query ("SELECT pinNumber, pinStatus FROM pinStatus WHERE pinNumber=18");
$strSQL2 = mysql_query ("SELECT pinNumber, pinDescription FROM pinDescription WHERE pinNumber=18");
$pinRow = mysql_fetch_assoc($strSQL1);
$descRow = mysql_fetch_assoc($strSQL2);
$pinNumber = $pinRow['pinNumber'];
$pinStatus = $pinRow['pinStatus'];
$pinDescription = $descRow['pinDescription'];
If ($pinStatus == "0"){
	$action = "Buzzer-on";
	$image = "Buzzer-Off.png";
} else {
	$action = "Buzzer-off";
	$image = "Buzzer-On.png";
}
$tbl .= "<tr>";
$tbl .= "<td align='center'>" . $pinNumber . "</td><td>" . $pinDescription . "</td><td><a href='index.php?action=" . $action . "'><img src=" . $image . " width='50' border='0'></a></td>";
$tbl .= "</tr>";

//Photo Resistor
$strSQL1 = mysql_query ("SELECT pinNumber, pinStatus FROM pinStatus WHERE pinNumber=22");
$strSQL2 = mysql_query ("SELECT pinNumber, pinDescription FROM pinDescription WHERE pinNumber=22");
$pinRow = mysql_fetch_assoc($strSQL1);
$descRow = mysql_fetch_assoc($strSQL2);
$pinNumber = $pinRow['pinNumber'];
$pinStatus = $pinRow['pinStatus'];
$pinDescription = $descRow['pinDescription'];
If ($pinStatus == "0"){
	$image = "Photo-Resistor-Off.png";
} else {
	$image = "Photo-Resistor-On.png";
}
$tbl .= "<tr>";
$tbl .= "<td align='center'>" . $pinNumber . "</td><td>" . $pinDescription . "</td><td><img src=" . $image . " width='50' border='0'></td>";
$tbl .= "</tr>";

//PIR1
$strSQL1 = mysql_query ("SELECT pinNumber, pinStatus FROM pinStatus WHERE pinNumber=23");
$strSQL2 = mysql_query ("SELECT pinNumber, pinDescription FROM pinDescription WHERE pinNumber=23");
$pinRow = mysql_fetch_assoc($strSQL1);
$descRow = mysql_fetch_assoc($strSQL2);
$pinNumber = $pinRow['pinNumber'];
$pinStatus = $pinRow['pinStatus'];
$pinDescription = $descRow['pinDescription'];
If ($pinStatus == "0"){
	$image = "PIR-Off.png";
} else {
	$image = "PIR-On.png";
}
$tbl .= "<tr>";
$tbl .= "<td align='center'>" . $pinNumber . "</td><td>" . $pinDescription . "</td><td><img src=" . $image . " width='50' border='0'></td>";
$tbl .= "</tr>";

//PIR1
$strSQL1 = mysql_query ("SELECT pinNumber, pinStatus FROM pinStatus WHERE pinNumber=24");
$strSQL2 = mysql_query ("SELECT pinNumber, pinDescription FROM pinDescription WHERE pinNumber=24");
$pinRow = mysql_fetch_assoc($strSQL1);
$descRow = mysql_fetch_assoc($strSQL2);
$pinNumber = $pinRow['pinNumber'];
$pinStatus = $pinRow['pinStatus'];
$pinDescription = $descRow['pinDescription'];
If ($pinStatus == "0"){
	$image = "PIR-Off.png";
} else {
	$image = "PIR-On.png";
}
$tbl .= "<tr>";
$tbl .= "<td align='center'>" . $pinNumber . "</td><td>" . $pinDescription . "</td><td><img src=" . $image . " width='50' border='0'></td>";
$tbl .= "</tr>";

//Garden Cam
$strSQL1 = mysql_query ("SELECT pinNumber, pinStatus FROM pinStatus WHERE pinNumber=91");
$strSQL2 = mysql_query ("SELECT pinNumber, pinDescription FROM pinDescription WHERE pinNumber=91");
$pinRow = mysql_fetch_assoc($strSQL1);
$descRow = mysql_fetch_assoc($strSQL2);
$pinNumber = $pinRow['pinNumber'];
$pinStatus = $pinRow['pinStatus'];
$pinDescription = $descRow['pinDescription'];
If ($pinStatus == "0"){
	$action = "Back-on";
	$image = "CCTV-Off.png";
} else {
	$action = "Back-off";
	$image = "CCTV-On.png";
}
$tbl .= "<tr>";
$tbl .= "<td align='center'>" . $pinNumber . "</td><td>" . $pinDescription . "</td><td><a href='index.php?action=" . $action . "'><img src=" . $image . " width='50' border='0'></a></td>";
$tbl .= "</tr>";

//Front Cam
$strSQL1 = mysql_query ("SELECT pinNumber, pinStatus FROM pinStatus WHERE pinNumber=92");
$strSQL2 = mysql_query ("SELECT pinNumber, pinDescription FROM pinDescription WHERE pinNumber=92");
$pinRow = mysql_fetch_assoc($strSQL1);
$descRow = mysql_fetch_assoc($strSQL2);
$pinNumber = $pinRow['pinNumber'];
$pinStatus = $pinRow['pinStatus'];
$pinDescription = $descRow['pinDescription'];
If ($pinStatus == "0"){
	$action = "Front-on";
	$image = "CCTV-Off.png";
} else {
	$action = "Front-off";
	$image = "CCTV-On.png";
}
$tbl .= "<tr>";
$tbl .= "<td align='center'>" . $pinNumber . "</td><td>" . $pinDescription . "</td><td><a href='index.php?action=" . $action . "'><img src=" . $image . " width='50' border='0'></a></td>";
$tbl .= "</tr>";

$tbl .= "</table>";

If (isset($_POST['action'])){
	If ($_POST['action'] == "setPassword"){
		$password1 = $_POST['password1'];
		$password2 = $_POST['password2'];
		If ($password1 != $password2){
			header('Location: index.php');
		}
		$password = mysql_real_escape_string($_POST['password1']);
		If (strlen($password) > 28){
			mysql_close();
			header('location: index.php');
		}
		$resetQuery = "SELECT username, salt FROM users WHERE username = 'admin';";
		$resetResult = mysql_query($resetQuery);
		If (mysql_num_rows($resetResult) < 1){
			mysql_close();
			header('location: index.php');
		}
		$resetData = mysql_fetch_array($resetResult, MYSQL_ASSOC);
		$resetHash = hash('sha256', $salt . hash('sha256', $password));
		$hash = hash('sha256', $password);
		function createSalt(){
			$string = md5(uniqid(rand(), true));
			return substr($string, 0, 8);
		}
		$salt = createSalt();
		$hash = hash('sha256', $salt . $hash);
		mysql_query("UPDATE users SET salt='$salt' WHERE username='admin'");
		mysql_query("UPDATE users SET password='$hash' WHERE username='admin'");
		mysql_close();
		header('location: index.php');
	}
}
If ((isset($_POST['username'])) && (isset($_POST['password']))){
	$username = mysql_real_escape_string($_POST['username']);
	$password = mysql_real_escape_string($_POST['password']);
	$loginQuery = "SELECT UserID, password, salt FROM users WHERE username = '$username';";
	$loginResult = mysql_query($loginQuery);
	If (mysql_num_rows($loginResult) < 1){
		mysql_close();
		header('location: index.php?error=incorrectLogin');
	}
	$loginData = mysql_fetch_array($loginResult, MYSQL_ASSOC);
	$loginHash = hash('sha256', $loginData['salt'] . hash('sha256', $password));
	If ($loginHash != $loginData['password']){
		mysql_close();
		header('location: index.php?error=incorrectLogin');
	} else {
		session_regenerate_id();
		$_SESSION['username'] = "admin";
		$_SESSION['userID'] = "1";
		mysql_close();
		header('location: index.php');
	}
}
If ((!isset($_SESSION['username'])) || (!isset($_SESSION['userID']))){
	print '
		<div id="main">
		<table border="0" width="90%">
		<tr><td valign="middle"><img src="RPi_logo.png"></td><td valign="middle"><center><font color="#CC0066" size="7" face="Georgia, serif">RPi Control Panel</font></center></td></tr>
		</table>
		<font face="verdana">
		<center>
	';
	print '
	<table border="0" align="center">
	<form name="login" action="index.php" method="post">
	<tr>
	<td>Username: </td><td><input type="text" name="username"></td>
	</tr>
	<tr>
	<td>Password: </td><td><input type="password" name="password"></td>
	</tr>
	<tr>
	<td colspan="2" align="center"><input type="submit" value="Log In"></td>
	</tr>
	</form>
	</table>
	</div>
	</body>
	</html>
	';
	die();
}
If (isset($_GET['action'])){
	If ($_GET['action'] == "logout"){
		$_SESSION = array();
		session_destroy();
		header('Location: index.php');
	} else If ($_GET['action'] == "setPassword"){
		print '
		<div id="main">
			<table border="0" width="90%">
			<tr><td valign="middle"><img src="RPi_logo.png"></td><td valign="middle"><center><font color="#CC0066" size="7" face="Georgia, serif">RPi Control Panel</font></center></td></tr>
			</table>
			<font face="verdana">
			<center>
		';
		print '
		<form name="changePassword" action="index.php" method="post">
		<input type="hidden" name="action" value="setPassword">
		<p>Enter New Password: <input type="password" name="password1">  Confirm: <input type="password" name="password2"><input type="submit" value="submit"></p>
		</form>
		';
	} else {
		$action = $_GET['action'];
		$pin = mysql_real_escape_string($_GET['pin']);
		if ($action == "turnOn"){
			$setting = "1";
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='$pin';");
			mysql_close();
			header('Location: index.php');
		} else If ($action == "turnOff"){
			$setting = "0";
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='$pin';");
			mysql_close();
			header('Location: index.php');
		} else If ($action == "Back-on"){
			exec ("curl http://192.168.0.24:8080/1/detection/start > /dev/null 2>&1", &$output);
			header('Location: index.php');
		} else If ($action == "Back-off"){
			exec ("curl http://192.168.0.24:8080/1/detection/pause > /dev/null 2>&1", &$output);
			header('Location: index.php');
		} else If ($action == "Front-on"){
			exec ("curl http://192.168.0.24:8080/2/detection/start > /dev/null 2>&1", &$output);
			header('Location: index.php');
		} else If ($action == "Front-off"){
			exec ("curl http://192.168.0.24:8080/2/detection/pause > /dev/null 2>&1", &$output);
			header('Location: index.php');
		} else If ($action == "Buzzer-on"){
			exec ('$(mysql -B --host=' . $MySQLHost  . ' --disable-column-names --user=' . $MySQLUsername . ' --password=' . $MySQLPassword . ' gpio -e "UPDATE pinStatus SET pinStatus=1 WHERE pinNumber=18";)', &$output);
			header('Location: index.php');
		} else If ($action == "Buzzer-off"){
			exec ('$(mysql -B --host=' . $MySQLHost  . ' --disable-column-names --user=' . $MySQLUsername . ' --password=' . $MySQLPassword . ' gpio -e "UPDATE pinStatus SET pinStatus=0 WHERE pinNumber=18";)', &$output);
			header('Location: index.php');
		} else IF ($action =="edit"){
			$pin = mysql_real_escape_string($_GET['pin']);
			$query = mysql_query("SELECT pinDescription FROM pinDescription WHERE pinNumber='$pin';");
			$descRow = mysql_fetch_assoc($query);
			$description = $descRow['pinDescription'];
			print '
				<div id="main">
				<table border="0" width="90%">
				<tr><td valign="middle"><img src="RPi_logo.png"></td><td valign="middle"><center><font color="#CC0066" size="7" face="Georgia, serif">RPi Control Panel</font></center></td></tr>
				</table>
				<font face="verdana">
				<center>
			';
			print '
			<table border="0">
			<form name="edit" action="index.php" method="get">
			<input type="hidden" name="action" value="update">
			<input type="hidden" name="pin" value="' . $pin . '">
			<tr>
			<td><p>Description: </p></td><td><input type="text" name="description" value="' . $description . '"></td><td><input type="submit" value="Confirm"></td>
			</tr>
			</form>
			</table>
			</div>
			</body></html>
			';
			mysql_close();
		} else IF ($action =="update"){
			$pin = mysql_real_escape_string($_GET['pin']);
			$description = mysql_real_escape_string($_GET['description']);
			mysql_query("UPDATE pinDescription SET pinDescription='$description' WHERE pinNumber='$pin';");
			header('Location: index.php');
		}
		else IF ($action =="refresh")
		{
			echo $tbl;
			die ();
		}
		else {
			header('Location: index.php');
		}

	}
} else {
	mysql_close();
	print '
		<div id="main">
		<table border="0" width="90%">
		<tr><td valign="middle"><img src="RPi_logo.png"></td><td valign="middle"><center><font color="#CC0066" size="7" face="Georgia, serif">RPi Control Panel</font></center></td></tr>
		</table>
		<font face="verdana">
		<center>
		<br><br><div id="myDiv">
	';
	echo $tbl;	
	print '</div><br><br>
	<p><a href="index.php?action=setPassword">Change Password</a></p>
	<a href="index.php?action=logout">Log out</a>
	</center>
	</font></div>
	</body></html>
	';
}	
?>

 




Theme Update

Having updated WordPress to version 3.5, I also updated the theme to “Twenty Twelve”.

It looks slightly slicker but I had to reduce the header pictures a bit to accommodate…




Control your Raspberry Pi via a Web Interface

Until now, I had to VNC on my Raspi, then open a terminal window, then run a script.
If I wanted to make adjustments, I’d have to stop the script, edit it, then run it again.

Thanks to drcurzon via www.instructables.com, I started to work on a Web Control Panel for my Raspi to contol LED’s, Light and Movement Alerts.

Have a look at the great instructions to reproduce: www.instructables.com/id/Web-Control-of-Raspberry-Pi-GPIO/

Web Control of Raspberry Pi GPIO