Hardware Reference
In-Depth Information
Figure 5-11. The pressure mat in operation
A Script for Your Mat Status
Now that you know your pressure mat is working correctly and your Raspberry Pi can read its value correctly, it might
be a good idea to write a simple script to poll the GPIO pin to warn you if someone steps on the mat. Take a look at
Listing 5-1 to see a simple bash script that I wrote to poll the GPIO and print a message to your terminal if someone
steps on the mat.
Listing 5-1. A Simple Bash Script to Poll the Mat
#!/bin/bash
# Description : A simple bash script to read the pressure mat status
# Author : Brendan Horan
# Start a loop that can't end
while true; do
# Get the value of the GPIO Pin
MATSTAT='cat /sys/class/gpio/gpio4/value'
if [ $MATSTAT == 1 ]
then
# Sleep for 2 seconds if status=1
sleep 2
else
# Echo out text if status=0
echo "Ouch! Don't step on me!!"
fi
done
To use this simple script, create a file in /usr/local/bin and call it mat.sh . Then put the contents of Listing 5-1
into the file and ensure that it's set to be executable. Run the mat.sh script and step on the mat. You will now see your
console fill up with lines printing “Ouch! Don't step on me!!”
This exact same circuit and code could be used on devices like tilt switches and magnetic door switches too.
Now that you have the pressure sensor set up, it's a good idea to use another method to detect if your area has been
breached. This is where the PIR will do its job.
 
Search WWH ::




Custom Search