Tuesday, December 29, 2020

HAMVOIP - email on a Raspberry Pi pin status change

 I wanted to have the clubhouse send me an email if the alarm system went off.  The plan is to have a contact closure bring GPIO 0 (Pin 11) logic low on alarm.

The GPIO example didn't include a way to then detect when the pin went high again, so I had to come up with a slight modification.

The first step is to set up email:

https://www.hamvoip.org/howto/Allstar_email_howto.pdf

Then I wrote some clever scripts to perform an endless round-robbin task of monitoring the GPIO pin in question.

The script /home/email_alarm.sh is started from /etc/rc.local


----------------
home/email_alarm.sh
----------------

#!/bin/bash
# Required button hold down seconds
# set mode to in and pullup pin 0 (Physical pin 11, GPIO17)
PIN="0"
gpio mode $PIN in
gpio mode $PIN up
bash home/alarm_wait_active.sh &
exit

-------------
home/alarm_wait_active.sh
-------------

#!/bin/bash
# Required button hold down seconds
HOLDTIME=5
#define pin to watch
PIN="0"
TIME1=1
while [ 1=1 ]
 # wait in interrupt for pin to go low
 gpio wfi $PIN falling
 do
 # Got the low now poll to see if it stays low for holdtime
 while [ `gpio read $PIN` -eq "0" ];
 do
 sleep 1
 let TIME1+=1
 if [ $TIME1 -gt $HOLDTIME ]; then
 # if greater than holdtime then exit past done
 break 3;
 fi
 done
 TIME1=1
 continue
done
# code to execute when holdtime is exceeded goes here
#echo -e "Subject: Pi alarm active" | sendmail -v email@gmail.com
echo "alarm active"
bash /home/alarm_wait_clear.sh &
exit


---------------
/home/alarm_wait_clear.sh
---------------

#!/bin/bash
# Required button hold down seconds
HOLDTIME=5
#define pin to watch
PIN="0"
TIME1=1
while [ 1=1 ]
 # wait in interrupt for pin to go high
 gpio wfi $PIN rising
 do
 # Got the low now poll to see if it stays high for holdtime
 while [ `gpio read $PIN` -eq "1" ];
 do
 sleep 1
 let TIME1+=1
 if [ $TIME1 -gt $HOLDTIME ]; then
 # if greater than holdtime then exit past done
 break 3;
 fi
 done
 TIME1=1
 continue
done
# code to execute when holdtime is exceeded goes here
#echo -s "Pi alarm cleared" | sendmail -v email@gmail.com
echo "alarm cleared"
bash ./alarm_wait_active.sh
exit