From 641ddadf779c6514962a9d78a914574a66e97dbb Mon Sep 17 00:00:00 2001 From: Ctibor Date: Mon, 5 Jun 2017 10:57:41 +0200 Subject: Introduce bash script used to take photos on RPi --- bin/nest_monitor.sh | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 bin/nest_monitor.sh (limited to 'bin/nest_monitor.sh') diff --git a/bin/nest_monitor.sh b/bin/nest_monitor.sh new file mode 100644 index 0000000..225f82b --- /dev/null +++ b/bin/nest_monitor.sh @@ -0,0 +1,120 @@ +#!/bin/bash + +# Name or IP address of remote server +remote="" +# Full path to the img directory on remote server +rdir="" +# User name used for connection on remote server +user="" +# Full path to temporary files directory +workdir="/tmp/nest" +# GPIO number used to drive IR LED +gpio=7 +# Size of captured images thumbnails +medium=440 +small=180 +# Interval between captures +interval=300 +# Local machine name +machine=$(hostname) + +init() { + timestamp=$(timestamp) + # Test connection + echo "Running nestmonitor on ${machine}, date: ${timestamp}" + status=$(/usr/bin/ssh -q -o BatchMode=yes -o ConnectTimeout=5 ${user}@${remote} echo ok 2>&1) + if [[ $status == ok ]] ; then + echo "${timestamp}: Connection to remote server is OK" + elif [[ $status == "Permission denied"* ]] ; then + echo "${timestamp}: User ${user} is not allowed to connect to ${remote}. Check your configuration. Exiting." + exit 2 + else + echo "${timestamp}: There is problem with your connection to ${remote}. Exiting." + exit 2 + fi + + # Init workdir + if ! [ -d ${workdir} ] ; then + mkdir -p ${workdir} + echo "${timestamp}: Creating working directory in ${workdir}" || exit 2 + else + echo "${timestamp}: Working directory ${workdir} already exists" + fi + + # Init GPIO for LED + echo "${timestamp}: Initializing GPIO for IR LED light" + /usr/bin/gpio mode ${gpio} output || exit + + # Check for camera connection + if [[ $(/usr/bin/vcgencmd get_camera | grep -q "detected=0") -eq 1 ]]; then + echo "${timestamp}: Camera not detected. Exiting!" + exit 2 + else + echo "${timestamp}: Camera found. All systems clear for operation!" + echo "============================================================" + echo "Starting to take images!" + echo "============================================================" + fi +} + +led_on() { + echo "${timestamp}: Turning on IR LED light" + /usr/bin/gpio write ${gpio} 1 +} + +led_off() { + echo "${timestamp}: Turning off IR LED light" + /usr/bin/gpio write ${gpio} 0 +} + +clean() { + timestamp=$(timestamp) + echo "====================================================================" + echo "Nest monitor failed!" + echo "====================================================================" + echo "${timestamp}: Cleaning ${workdir}" + rm -r ${workdir} + echo "${timestamp}: Disabling GPIO pin for IR LED light" + /usr/bin/gpio mode 7 input + exit 2 +} + +take() { + led_on + echo "${timestamp}: Taking a new shot!" + /usr/bin/raspistill --rotation 180 --hflip -awb off -awbg 1.0,1.0 -q 100 --width 1000 --height 750 --output ${workdir}/${1}-full.jpeg + led_off +} + +process() { + echo "${timestamp}: Processing image thumbnails" + /usr/bin/convert ${workdir}/${1}-full.jpeg -resize ${medium} ${workdir}/${1}-${medium}.jpeg || echo "Failed to create thumbnail!" + /usr/bin/convert ${workdir}/${1}-full.jpeg -resize ${small} ${workdir}/${1}-${small}.jpeg || echo "Failed to create thumbnail!" +} + +upload() { + echo "${timestamp}: Uploading images to ${remote}" + for i in "full" "${medium}" "${small}"; do + /usr/bin/scp -q ${workdir}/${1}-${i}.jpeg ${user}@${remote}:${rdir}/ + rm ${workdir}/${1}-${i}.jpeg + done + /usr/bin/ssh -q ${user}@${remote} "/bin/ln -sf ${rdir}/${timestamp}-full.jpeg ${rdir}/latest.jpeg" +} + +timestamp() { + /bin/date +%Y%m%d%H%M +} + + +run() { + init + timestamp=$(timestamp) + take "${timestamp}" + process "${timestamp}" + upload "${timestamp}" + echo "Shot complete. See you in ${interval} seconds!" +} + +run + +trap clean SIGINT SIGHUP SIGQUIT SIGTERM -- cgit v1.2.3