Skip to content

Instantly share code, notes, and snippets.

@mikkun
Last active Sep 14, 2020
Embed
What would you like to do?
「第37回シェル芸勉強会 大阪サテライト LT」( https://speakerdeck.com/mikkun/shell-script-for-raspberry-pi )で紹介したシェルスクリプト
#!/bin/bash --
# Name : rpi-pir-alarm
# Purpose : Motion Detection and Email Notification Using Raspberry Pi
#
# Author : KUSANAGI Mitsuhisa <mikkun@mbg.nifty.com>
# License : MIT License
# Usage : ./rpi-pir-alarm
#=============================================================================
# Initialization
#=============================================================================
#-----------------------------------------------------------------------------
# Define variable(s)
#-----------------------------------------------------------------------------
set -u
shopt -s expand_aliases
if [ "$(uname)" != 'Linux' ] ; then
alias awk='gawk'
alias date='gdate'
alias fold='gfold'
alias sed='gsed'
alias tac='tail -r'
alias tr='gtr'
fi
PATH='/usr/bin:/bin'
export PATH
GPIO=17
INTERVAL=0.5
WAIT=30
IMG_DIR='/tmp'
MAX_IMGS=50
FROM='userx@y.foo.org'
TO='userc@d.bar.org'
#-----------------------------------------------------------------------------
# Define function(s)
#-----------------------------------------------------------------------------
remove_gpio() {
echo "Removing GPIO #${GPIO}..." >&2
echo $GPIO > '/sys/class/gpio/unexport'
sleep $INTERVAL
echo "Stopped ${0##*/}." >&2
exit 0
}
#-----------------------------------------------------------------------------
# Setup GPIO
#-----------------------------------------------------------------------------
set -e
if [ -d "/sys/class/gpio/gpio${GPIO}" ] ; then
echo "Resetting GPIO #${GPIO}..." >&2
echo $GPIO > '/sys/class/gpio/unexport'
sleep $INTERVAL
fi
echo "Exporting GPIO #${GPIO}..." >&2
echo $GPIO > '/sys/class/gpio/export'
sleep $INTERVAL
echo "Setting the direction of GPIO #${GPIO}..." >&2
echo 'in' > "/sys/class/gpio/gpio${GPIO}/direction"
sleep $INTERVAL
trap remove_gpio SIGINT SIGTERM
#=============================================================================
# Main
#=============================================================================
set +e
echo "Started ${0##*/}." >&2
prev_value=0
curr_value=0
while : ; do
curr_value=$(cat "/sys/class/gpio/gpio${GPIO}/value" || echo 0)
if [ "$curr_value" -gt $prev_value ] ; then
date=$(date '+%s')
file="${IMG_DIR}/${0##*/}_$(date -d "@${date}" '+%Y%m%d%H%M%S').jpg"
raspistill -w 640 -h 480 -o "$file" -ev 7
cat << END_MAIL | /usr/lib/sendmail -t
From: $FROM
To: $TO
Subject: ${0##*/}: [$(date -d "@${date}" '+%F %T')]
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="${date}_$$"
Content-Transfer-Encoding: 7bit
--${date}_$$
Content-Type: $(file -bi "$file" | sed 's/; charset=binary//'); name="${file##*/}"
Content-Disposition: attachment; filename="${file##*/}"
Content-Transfer-Encoding: base64
$(base64 "$file")
--${date}_$$--
END_MAIL
if [ "$MAX_IMGS" -gt 0 ] ; then
find "$IMG_DIR" -maxdepth 1 -name "${0##*/}_*" |
sort -r |
sed "1,${MAX_IMGS}d" |
tr '\n' '\0' |
xargs -0 rm -f
fi
sleep $WAIT
fi
prev_value=$curr_value
sleep $INTERVAL
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment