Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Project: Detect and alarm on switch
08-19-2014, 05:43 AM,
#1
Project: Detect and alarm on switch
Here is what I am planning to do:
- On a door I want to make a switch.
- When the door is open, I want the Raspberry to "send" an alarm to my iPhone

How would I want to do this:
- Raspberry with a switch to the gpio
- Check every minute (through cron) if the switch is open.
- If so, send an email
Reply
08-19-2014, 05:57 AM, (This post was last modified: 08-19-2014, 06:38 AM by digdas.)
#2
RE: Project: Detect and alarm on switch
Hardware:
- Raspberry PI
- Power supply
- Wifi module (The door is not near any internet cabling)
- Switch to detect the open-state
- SD-card (for the boot-partition)
- USB stick (for the OS)

Switch connection:
Between pins 20 and 18 (GND + GPIO 24)
I am using the internal pull-up resistor
Reply
08-19-2014, 06:05 AM,
#3
RE: Project: Detect and alarm on switch
Software details:
- Webpage to see the current state and the on/off moments
- Mail sending (Alarm triggering, is set in a config web-page)
Reply
08-19-2014, 06:17 AM, (This post was last modified: 08-19-2014, 06:38 AM by digdas.)
#4
RE: Project: Detect and alarm on switch
For software I am going to use wiringPI. This can be found at:
WiringPI
To install I gave the following commands:
Code:
$ sudo apt-get install git-core
$ git clone git://git.drogon.net/wiringPi
$ cd wiringPi
$ ./build
Test:
Code:
$ gpio -v
gpio version: 2.20
Copyright (c) 2012-2014 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:
  Type: Model B, Revision: 2, Memory: 512MB, Maker: Sony
$ gpio readall
+-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+
| BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
|     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
|   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5V      |     |     |
|   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |
|   4 |   7 | GPIO. 7 |   IN | 0 |  7 || 8  | 1 | ALT0 | TxD     | 15  | 14  |
|     |     |      0v |      |   |  9 || 10 | 1 | ALT0 | RxD     | 16  | 15  |
|  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
|  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
|  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
|     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
|  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
|   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
|  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 0 | IN   | CE0     | 10  | 8   |
|     |     |      0v |      |   | 25 || 26 | 0 | IN   | CE1     | 11  | 7   |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
|  28 |  17 | GPIO.17 |   IN | 0 | 51 || 52 | 0 | IN   | GPIO.18 | 18  | 29  |
|  30 |  19 | GPIO.19 |   IN | 0 | 53 || 54 | 0 | IN   | GPIO.20 | 20  | 31  |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
+-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+
$

Now follows the command to read input GPIO pin 18
Code:
$ gpio -1 mode 18 up
# -1 means: use P1 header pin numbering
# mode = set the mode
# 18 = pin 18
# up = enable the pull-up resistor
$ gpio -1 read 18
1
# -1 means: use P1 header pin numbering
# read = read the status of the pin
# 18= pin number 18
# Now with 18 and 20 connected:
$ gpio -1 read 18
0
Reply
08-20-2014, 05:01 AM, (This post was last modified: 08-20-2014, 05:10 AM by digdas.)
#5
RE: Project: Detect and alarm on switch
The following command-script is used to log the status:

(File: /home/pi/door/logStatus.sh)
Code:
#!/bin/sh

cd /home/pi/door

# Initialize pin

/usr/local/bin/gpio -1 mode 18 up

status=`/usr/local/bin/gpio -1 read 18`

# Check status.log file

if [ ! -f status.log ]
then
  echo "Date Time Status" > status.log
fi

laststatus=`tail -1 status.log | awk '{print $3}'`

# Append new status if different

if [ ! X$status = X$laststatus ]
then
  echo `date +'%Y-%m-%d %H:%M:%S' ` $status >> status.log
fi
For the gpio command I added the full path name, as in cron this might not be set.

Remember to make the file executable:
Code:
chmod +x logStatus.sh
The following is added to cron, to log the data:
Code:
* * * * * /home/pi/door/logStatus.sh
Reply
08-20-2014, 05:45 AM,
#6
RE: Project: Detect and alarm on switch
Now adding the php webserver.
First install the software:
Code:
sudo apt-get install php5 libapache2-mod-php5 php5-intl php5-mcrypt php5-curl php5-gd php5-sqlite
Test if the software is working. I created a file with the name /var/www/index.php:
Code:
<?php
phpinfo();
?>
and browse to http://192.168.0.44/index.php
It shows php-info.


Now I add a page which gives me back the status of the GPIO: info.php
Code:
<?php
system ( "gpio -1 read 18" );
?>

Then the file to show the info in a div:
Code:
<html>
  <body>
    <h1>Data</h1>
    <div id="status">Test</div>
  </body>

<script type="text/javascript" >
  var intervalId = window.setInterval(function() {
    var xhr= new XMLHttpRequest();
    xhr.open('GET', 'info.php', true);
    xhr.onreadystatechange= function() {
      if (this.readyState!==4) return;
      if (this.status!==200) return; // or whatever error handling you want
      document.getElementById('status').innerHTML= this.responseText;
     };
    xhr.send();
   }, 500);
</script>
</html>
To explain this:
The div to update is in the body, and has id="status".
To update this div:
document.getElementById('status').innerHTML= this.responseText;

To get the information for the response text is in the function created in window.setInterval

And to execute this on a regular basis (500 ms) it is called from within window.setInterval
Reply
08-25-2014, 12:56 AM, (This post was last modified: 08-25-2014, 12:57 AM by digdas.)
#7
RE: Project: Detect and alarm on switch
Now I added the code to send me an email when the door is open (status == 0)
Code:
#!/bin/sh

# Configuration
fromMail=xx@yy.zz
toMail=xx@yy.zz

cd /home/pi/door


# Initialize pin

/usr/local/bin/gpio -1 mode 18 up

status=`/usr/local/bin/gpio -1 read 18`

# Check status.log file

if [ ! -f status.log ]
then
  echo "Date Time Status" > status.log
fi

laststatus=`tail -1 status.log | awk '{print $3}'`

# Append new status if different

if [ ! X$status = X$laststatus ]
then
  echo `date +'%Y-%m-%d %H:%M:%S' ` $status >> status.log
fi

# Mail if status is not 2014-08-24 16:54:01 0

if [ ! X$status = X0 ]
then
  (
   echo "Warning: The door is open"
   echo ""
   echo "Raspberry @ `hostname`"
   date
  ) | mail -s "Door open!!!" -a from:$fromMail $toMail

fi
(Edit the from and to mail addresses to what you need them to be
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)