My 6th grade son recently had a group work science investigative project where parents are encouraged to help with their experimental setup. The group proposed a door security alarm using a laser tripwire. It is controlled by a mobile webpage on and off switch and when triggered will send an SMS message to whoever is subscribed to the device.

# The Final Product Demo

Watch the video to see the complete product:

# Components

## 1. The User Interface Switch Controller

The security door requires a simple switch to turn the device on and off. Normally, this kind of security device requires an RFID card reader to authenticate authorize persons. However, since this is just a 6th grader science project this will complicate the electronic component a lot.

The chosen solution is to provide a mobile-friendly web page with an HTML on and off button hosted in Apache web server inside the Raspberry Pi. The disadvantage of this approach is the smart phone needs to be connected to the Wifi network of the Pi to be able to turn it on or off.

Pressing the buttons will execute a corresponding shell script that in turn calls a Python program that generates 3 beeps to the buzzer and turns on the device.

The source code of the PHP program can divided into 2 parts namely (1) the PHP webpage part link here and (2) the Shell/Python part link here. Disclaimer: The source folders do not have a build system so you have to copy the PHP files to /var/www/html and the shell scripts/Python programs to /home/pi/lol/etc since the paths are currently hard-coded.

The key takeaway in enabling a webpage to control a Raspberry Pi low level programming is through executing a shell script from a PHP script using shell_exec.

<?php
shell_exec('sudo /home/pi/lol/etc/buzzer-start.sh');
?>


Just don't forget to add the web server user www-data as a sudoer and add it to /etc/sudoers e.g. at the last line put:

www-data ALL=(ALL) NOPASSWD: ALL


## 2. The Laser Tripwire

The laser tripwire component was derived from A Raspberry Pi Laser Tripwire.

### The source code for laser tripwire

def send_sms():
global safe_to_send_sms
if safe_to_send_sms:
safe_to_send_sms = False
smslib.send_sms('xxxxxxxxxxxx,xxxxxxxxxxxx','Intruder alert! Somebody opened the door!')

ldr = LightSensor(4)
buzzer = Buzzer(17)

safe_to_send_sms = True

while True:
sleep(0.1)
print ldr.value
if ldr.value < 0.15:
buzzer.on()
else:
safe_to_send_sms = True
buzzer.off()


In a nutshell, the code loops and read the light-dependent resistor's (LDR) value at GPIO 4 and turns on the buzzer at GPIO 17 when the value of the light is less than 0.15 and turns off buzzer if it's greater than or equal to 0.15. The code spawns a thread when sending an SMS message otherwise will send several SMS messages inside the loop when the device alarms!

# 3. The SMS Text Alert Notifier

The implementation of SMS notification is through HTTP protocol using an SMS gateway instead of using GSM modem. More specifically by sending an HTTP POST that contains the username, password, SMS message, etc. to the SMS Gateway. In our case, we used BulkSMS and it cost about 7.50 EUR for 200 credits.

The relevant Python code to send an SMS message:

def send_sms(mobile_numbers,msg):
query_args = {
'message':msg,
'want_report':'1',
'msisdn':mobile_numbers,
'sender':'Kowalski'
}

url = 'http://bulksms.vsms.net:5567/eapi/submission/send_sms/2/2.0'
data = urllib.urlencode(query_args)
request = urllib2.Request(url, data)
print 'Sent sms!'