#!/bin/bash
GPIO=26
GPIO_DIR=/sys/class/gpio
MOTION_PID=/dev/shm/motion.pid
BTMAC=[わしの携帯電話のbluetoothのMACアドレス]
initialize(){
# GPIO26を有効化。プルアップ抵抗ON
echo $GPIO >${GPIO_DIR}/export
sleep 0.1
echo in >${GPIO_DIR}/gpio${GPIO}/direction
sleep 0.1
echo high >${GPIO_DIR}/gpio${GPIO}/direction
}
shutdown_handler(){
# GPIOクリーンアップ
echo $GPIO >$GPIO_DIR/unexport
motion_stop
exit 0
}
mail_send(){
cat << _EOD_|/usr/sbin/sendmail -t -f yuji@noizumi.org
From: yuji@noizumi.org
To: yuji@noizumi.org
Subject: Car Security alert $1
Car Security alarm was $1 at `date +"%Y/%m/%d %H:%M:%S"`
_EOD_
}
sms_send(){
DATEMES=`date +"%Y/%m/%d %H:%M:%S"`
curl -X POST https://api.twilio.com/2010-04-01/Accounts/[アカウント]/Messages.json \
--data-urlencode "Body=Car Security alarm was $1 at $DATEMES" \
--data-urlencode "From=[twilioの携帯電話番号]" \
--data-urlencode "To=[わしの携帯電話番号]" \
-u [アカウント]:[パスワード] -o /home/noizumi/logs/twilio.log
}
button_pushed(){
motion_start
mail_send activated
sms_send activated
# 車載警報装置発報中はループ
while [ "`cat ${GPIO_DIR}/gpio${GPIO}/value`" = "0" ]; do
sleep 1
done
mail_send stopped
sms_send stopped
}
motion_start(){
if [ ! -e ${MOTION_PID} ]; then
motion -n &
echo $! >${MOTION_PID}
fi
}
motion_stop(){
if [ -e ${MOTION_PID} ]; then
kill -TERM `cat ${MOTION_PID}`
rm -f ${MOTION_PID} || true
fi
}
bluetooth_connect(){
# bluetooth 接続状態確認
if [ "`hcitool con|grep -v Connection|grep ${BTMAC}`" != "" ]; then
# 接続してたら、動体検知止める
motion_stop
if [ "`ip a|grep bnep`" = "" ]; then
# bluetoothテザリング
bt-pan client ${BTMAC}
fi
else
# 接続してなかったら、動体検知スタート
motion_start
cat << _EOD_|bluetoothctl >/dev/null 2>&1
power on
connect ${BTMAC}
quit
_EOD_
fi
}
initialize
trap shutdown_handler 1 2 3 15
# pulseaudo 起動確認(bluetooth 接続用)
if [ "`ps ux|grep -e \"${USER}.*pulseaudio\"|grep -v grep`" = "" ]; then
pulseaudio --start
sudo systemctl restart bluetooth.service
fi
while :; do
# 車載警報装置発報時
if [ "`cat ${GPIO_DIR}/gpio${GPIO}/value`" = "0" ]; then
button_pushed
fi
bluetooth_connect
sleep 1
done