#!/bin/bash # # ---------------- # nico - May 2011 # ---------------- weather_sh=$HOME/bin/weather.sh twit_sh=$HOME/bin/twit.sh arduino_ip="http://192.168.1.6" rain_threshold="60" # 60% chance of precipitation msg_not="Did not turn sprinkler on because chance of rain is" #msg_not="Pas d'arrosage car la probabilite de pluie est de" msg_on="Turned on circuit" #msg_on="Mise en marche arrosage, circuit" msg_off="Turned everything off." #msg_off="Arret arrosage." turn_on= twit="yes" weather="yes" dbg= err=ok while test $# -gt 0 do arg=$1 case $arg in -on) shift; if [ $# -eq 0 ]; then err="$arg syntax"; break; fi turn_on=$1 ;; -twit) twit="yes" ;; +twit) twit="no" ;; -weather) weather="yes" ;; +weather) weather="no" ;; -help) err=help ;; *) err="unknown argument $arg" ;; esac shift unset arg if [ "$err" != "ok" ]; then break; fi done trap "rm -f $tmps; exit 0" 0 1 2 3 15 if [ "$err" != "ok" ]; then if [ "$err" != "help" ]; then echo Error: $err 1>&2 fi echo " " 1>&2 echo "Usage: "`basename $0` 1>&2 echo " " 1>&2 echo " [-on num_circuit : turn on circuit 'num_circuit'. [1-3]" 1>&2 echo " [-|+twit] : send twit. (default is $twit)" 1>&2 echo " [-|+weather] : check weather forecast. (default is $twit)" 1>&2 echo " " 1>&2 echo "Examples: " 1>&2 echo " " 1>&2 echo ""`basename $0`" # turn everything off" 1>&2 echo ""`basename $0`" -on 2 # turn on circuit #2" 1>&2 echo ""`basename $0`" -on 1 -twit -weather # turn on circuit #3" 1>&2 echo " " 1>&2 exit 1 fi # turn off everything echo "turn off" curl "${arduino_ip}/?cmd=3" > /dev/null 2> /dev/null sleep 1 curl "${arduino_ip}/?cmd=5" > /dev/null 2> /dev/null sleep 1 curl "${arduino_ip}/?cmd=7" > /dev/null 2> /dev/null sleep 1 echo "done" # turn on msg= if [ "$turn_on" != "" ]; then if [ "$weather" == "yes" ]; then for i in `$weather_sh | grep rain | sed -e 's;rain:\([0-9]*\)%;\1;'` do if [ "$dbg" != "" ]; then echo "rain:$i"; fi if [ "$i" -ge "$rain_threshold" ]; then msg="$msg_not ${i}%..." fi done fi if [ "$msg" == "" ]; then cmd=`echo $turn_on | tr [123] [246]` msg="$msg_on #$turn_on (cmd=$cmd)." curl "${arduino_ip}/?cmd=$cmd" > /dev/null 2> /dev/null fi else msg="$msg_off" fi now=`date '+%Y%m%d-%H:%M:%S'` #now=`date '+%H:%M:%S'` echo "$now $msg" if [ "$twit" == "yes" ]; then $twit_sh "$now $msg" fi exit 0