5 Commits

3 changed files with 232 additions and 48 deletions

View File

@@ -1,3 +1,15 @@
## 0.3.1 - 13/05/2017
Add experimental support for monostable solenoid valve:
- define in your config file the variable EV_MONOSTABLE and assign value 1
- if the solenoid valves close instead of opening and vice versa, reverse the values of the RELE_GPIO_CLOSE and RELE_GPIO_OPEN variables in your configuration file
## 0.3.0 - 07/05/2017
Add command "open_in" for scheduling on the fly the opens/close a solenoid
Add command "del_cron_open_in" for delete scheduling the fly the opens/close a solenoid
Add api in socket server for command open_in and delete_cron_open_in
Fix minor bug on command "open"
Changed the path of some temporary files to prevent sd card faults
## 0.2.2 - 25/04/2017 ## 0.2.2 - 25/04/2017
Fix bug: if it's reining, the solenoid valves were also closed even if they were pushed open in "force" mode Fix bug: if it's reining, the solenoid valves were also closed even if they were pushed open in "force" mode

View File

@@ -44,6 +44,10 @@ SED="/bin/sed"
# Percorso readlink # Percorso readlink
READLINK="/bin/readlink" READLINK="/bin/readlink"
# Se impostato con il valore 1, indica che il sistema gestisce elettrovalvole monostabili,
# se impostato a 0 il sistema gestirà elettrovalvole bisstabili
EV_MONOSTABLE=0
# Id gpio usati per simulare il doppio deviatore con cui eseguire l'alimentazione alle elettrovalvole # Id gpio usati per simulare il doppio deviatore con cui eseguire l'alimentazione alle elettrovalvole
SUPPLY_GPIO_1=2 SUPPLY_GPIO_1=2
SUPPLY_GPIO_2=3 SUPPLY_GPIO_2=3

View File

@@ -12,11 +12,13 @@ function initialize {
log_write "Run initialize" log_write "Run initialize"
# Imposta l'alimentazione con voltaggio negativo e setta i gpio in scrittura # Imposta l'alimentazione con voltaggio negativo e setta i gpio in scrittura per le elettrovalvole bistabili
if [ "$EV_MONOSTABLE" != "1" ]; then
$GPIO -g write $SUPPLY_GPIO_1 0 $GPIO -g write $SUPPLY_GPIO_1 0
$GPIO -g write $SUPPLY_GPIO_2 0 $GPIO -g write $SUPPLY_GPIO_2 0
$GPIO -g mode $SUPPLY_GPIO_1 out $GPIO -g mode $SUPPLY_GPIO_1 out
$GPIO -g mode $SUPPLY_GPIO_2 out $GPIO -g mode $SUPPLY_GPIO_2 out
fi
# Elimina tutti gli stati delle elettrovalvole preesistenti # Elimina tutti gli stati delle elettrovalvole preesistenti
rm -f "$STATUS_DIR"/* rm -f "$STATUS_DIR"/*
@@ -62,6 +64,9 @@ function reset_messages {
# $2 se specificata la string "force" apre l'elettrovalvola anche se c'é pioggia # $2 se specificata la string "force" apre l'elettrovalvola anche se c'é pioggia
# #
function ev_open { function ev_open {
cron_del open_in $1 > /dev/null 2>&1
if [ ! "$2" = "force" ]; then if [ ! "$2" = "force" ]; then
if [[ "$NOT_IRRIGATE_IF_RAIN_ONLINE" -gt 0 && -f $STATUS_DIR/last_rain_online ]]; then if [[ "$NOT_IRRIGATE_IF_RAIN_ONLINE" -gt 0 && -f $STATUS_DIR/last_rain_online ]]; then
local last_rain=`cat $STATUS_DIR/last_rain_online` local last_rain=`cat $STATUS_DIR/last_rain_online`
@@ -94,36 +99,117 @@ function ev_open {
state=2 state=2
fi fi
log_write "Solenoid '$1' open" # Dall'alias dell'elettrovalvola recupero il numero e dal numero recupero gpio da usare
message_write "success" "Solenoid open"
supply_positive
ev_alias2number $1 ev_alias2number $1
EVNUM=$? EVNUM=$?
ev_number2gpio $EVNUM ev_number2gpio $EVNUM
g=$? g=$?
# Gestisce l'apertura dell'elettrovalvola in base alla tipologia (monostabile / bistabile)
if [ "$EV_MONOSTABLE" == "1" ]; then
$GPIO -g write $g $RELE_GPIO_CLOSE
else
supply_positive
$GPIO -g write $g $RELE_GPIO_CLOSE $GPIO -g write $g $RELE_GPIO_CLOSE
sleep 1 sleep 1
$GPIO -g write $g $RELE_GPIO_OPEN $GPIO -g write $g $RELE_GPIO_OPEN
fi
ev_set_state $EVNUM $state ev_set_state $EVNUM $state
log_write "Solenoid '$1' open"
message_write "success" "Solenoid open"
} }
#
# Commuta un elettrovalvola nello stato aperto
# $1 minute_start
# $2 minute_stop
# $3 alias elettrovalvola
# $4 se specificata la string "force" apre l'elettrovalvola anche se c'é pioggia
#
function ev_open_in {
local minute_start=$1
local minute_stop=$2
local alias=$3
local force=$4
re='^[0-9]+$'
if ! [[ $minute_start =~ $re ]] ; then
echo -e "Time start of irrigation is wrong or not specified"
message_write "warning" "Time start of irrigation is wrong or not specified"
return 1
fi
if ! [[ $minute_stop =~ $re ]] ; then
echo -e "Time stop of irrigation is wrong or not specified"
message_write "warning" "Time stop of irrigation is wrong or not specified"
return 1
fi
if [ $minute_stop -lt "1" ] ; then
echo -e "Time stop of irrigation is wrong"
message_write "warning" "Time stop of irrigation is wrong"
return 1
fi
if [ "empty$alias" == "empty" ]; then
echo -e "Alias solenoid not specified"
message_write "warning" "Alias solenoid not specified"
return 1
fi
gpio_alias2number $alias > /dev/null 2>&1
minute_start=$(($minute_start + 1))
minute_stop=$(($minute_start + $minute_stop))
local cron_start=`date -d "today + $minute_start minutes" +"%M %H %d %m %u"`
cron_del open_in $alias > /dev/null 2>&1
cron_del open_in_stop $alias > /dev/null 2>&1
if [ "$minute_start" -eq "1" ]; then
ev_open $alias $force
else
cron_add open_in $cron_start "$alias" "$force"
fi
local cron_stop=`date -d "today + $minute_stop minutes" +"%M %H %d %m %u"`
cron_add open_in_stop $cron_stop "$alias"
message_write "success" "Scheduled start successfully performed"
#echo $cron_start
#echo $cron_stop
}
# #
# Commuta un elettrovalvola nello stato chiuso # Commuta un elettrovalvola nello stato chiuso
# $1 alias elettrovalvola # $1 alias elettrovalvola
# #
function ev_close { function ev_close {
log_write "Solenoid '$1' close"
message_write "success" "Solenoid close" # Dall'alias dell'elettrovalvola recupero il numero e dal numero recupero gpio da usare
supply_negative
#$GPIO_alias2number $1
ev_alias2number $1 ev_alias2number $1
EVNUM=$? EVNUM=$?
ev_number2gpio $EVNUM ev_number2gpio $EVNUM
g=$? g=$?
# Gestisce l'apertura dell'elettrovalvola in base alla tipologia (monostabile / bistabile)
if [ "$EV_MONOSTABLE" == "1" ]; then
$GPIO -g write $g $RELE_GPIO_OPEN
else
supply_negative
$GPIO -g write $g $RELE_GPIO_CLOSE $GPIO -g write $g $RELE_GPIO_CLOSE
sleep 1 sleep 1
$GPIO -g write $g $RELE_GPIO_OPEN $GPIO -g write $g $RELE_GPIO_OPEN
fi
ev_set_state $EVNUM 0 ev_set_state $EVNUM 0
log_write "Solenoid '$1' close"
message_write "success" "Solenoid close"
cron_del open_in_stop $1 > /dev/null 2>&1
} }
# #
@@ -211,7 +297,7 @@ function gpio_alias2number {
done done
log_write "ERROR solenoid alias not found: $1" log_write "ERROR solenoid alias not found: $1"
message_write "error" "Solenoid alias not found" message_write "warning" "Solenoid alias not found"
exit 1 exit 1
} }
@@ -230,7 +316,7 @@ function ev_alias2number {
done done
log_write "ERROR solenoid alias not found: $1" log_write "ERROR solenoid alias not found: $1"
message_write "error" "Solenoid alias not found" message_write "warning" "Solenoid alias not found"
exit 1 exit 1
} }
@@ -296,10 +382,10 @@ function ev_status {
# #
function check_rain_online { function check_rain_online {
# http://www.wunderground.com/weather/api/d/docs?d=resources/phrase-glossary&MR=1 # http://www.wunderground.com/weather/api/d/docs?d=resources/phrase-glossary&MR=1
$CURL http://api.wunderground.com/api/$WUNDERGROUND_KEY/conditions/q/$WUNDERGROUND_LOCATION.json > /tmp/check_rain_online.json $CURL http://api.wunderground.com/api/$WUNDERGROUND_KEY/conditions/q/$WUNDERGROUND_LOCATION.json > $TMP_PATH/check_rain_online.json
local weather=`cat /tmp/check_rain_online.json | $JQ -M ".current_observation.weather"` local weather=`cat $TMP_PATH/check_rain_online.json | $JQ -M ".current_observation.weather"`
local current_observation=`cat /tmp/check_rain_online.json | $JQ -M ".current_observation"` local current_observation=`cat $TMP_PATH/check_rain_online.json | $JQ -M ".current_observation"`
local local_epoch=`cat /tmp/check_rain_online.json | $JQ -M -r ".current_observation.local_epoch"` local local_epoch=`cat $TMP_PATH/check_rain_online.json | $JQ -M -r ".current_observation.local_epoch"`
#echo $weather #echo $weather
#weather="[Light/Heavy] Drizzle" #weather="[Light/Heavy] Drizzle"
if [ "$weather" = "null" ]; then if [ "$weather" = "null" ]; then
@@ -430,6 +516,7 @@ function json_status {
local last_warning="" local last_warning=""
local last_success="" local last_success=""
local with_get_cron="0" local with_get_cron="0"
local with_get_cron_open_in="0"
local vret="" local vret=""
for i in $1 $2 $3 $4 $5 $6 for i in $1 $2 $3 $4 $5 $6
@@ -437,6 +524,9 @@ function json_status {
if [ $i = "get_cron" ]; then if [ $i = "get_cron" ]; then
with_get_cron="1" with_get_cron="1"
fi fi
if [ $i = "get_cron_open_in" ]; then
with_get_cron_open_in="1"
fi
done done
for i in $(seq $EV_TOTAL) for i in $(seq $EV_TOTAL)
@@ -501,7 +591,33 @@ function json_status {
fi fi
local json_cron="\"cron\":{$json_get_cron}" local json_cron="\"cron\":{$json_get_cron}"
json="{$json_version,$json,$json_last_weather_online,$json_error,$json_last_info,$json_last_warning,$json_last_success,$json_last_rain_online,$json_last_rain_sensor,$json_cron}" local json_get_cron_open_in=""
if [ $with_get_cron_open_in = "1" ]; then
local values_open_in=""
local values_open_in_stop=""
for i in $(seq $EV_TOTAL)
do
local a=EV"$i"_ALIAS
local av=${!a}
local crn="$(cron_get "open_in" $av)"
crn=`echo "$crn" | sed ':a;N;$!ba;s/\n/%%/g'`
values_open_in="\"$av\": \"$crn\", $values_open_in"
local crn="$(cron_get "open_in_stop" $av)"
crn=`echo "$crn" | sed ':a;N;$!ba;s/\n/%%/g'`
values_open_in_stop="\"$av\": \"$crn\", $values_open_in_stop"
done
if [[ ! -z $values_open_in ]]; then
values_open_in="${values_open_in::-2}"
fi
if [[ ! -z $values_open_in_stop ]]; then
values_open_in_stop="${values_open_in_stop::-2}"
fi
json_get_cron_open_in="\"open_in\": {$values_open_in},\"open_in_stop\": {$values_open_in_stop}"
fi
local json_cron_open_in="\"cron_open_in\":{$json_get_cron_open_in}"
json="{$json_version,$json,$json_last_weather_online,$json_error,$json_last_info,$json_last_warning,$json_last_success,$json_last_rain_online,$json_last_rain_sensor,$json_cron,$json_cron_open_in}"
echo "$json" echo "$json"
@@ -551,7 +667,7 @@ function cron_del {
fi fi
$SED "$START,${END}d" "$TMP_CRON_FILE" | $CRONTAB - $SED "$START,${END}d" "$TMP_CRON_FILE" | $SED '$!N; /^\(.*\)\n\1$/!P; D' | $CRONTAB -
#$CRONTAB "$TMP_CRON_FILE" #$CRONTAB "$TMP_CRON_FILE"
rm "$TMP_CRON_FILE" rm "$TMP_CRON_FILE"
@@ -566,6 +682,7 @@ function cron_del {
# $5 mese # $5 mese
# $6 giorno della settimana # $6 giorno della settimana
# $7 argomento della tipologia # $7 argomento della tipologia
# $8 secondo argomento della tipologia
# #
function cron_add { function cron_add {
@@ -576,6 +693,7 @@ function cron_add {
local CRON_MON=$5 local CRON_MON=$5
local CRON_DOW=$6 local CRON_DOW=$6
local CRON_ARG=$7 local CRON_ARG=$7
local CRON_ARG2=$8
local CRON_COMMAND="" local CRON_COMMAND=""
local PATH_SCRIPT=`$READLINK -f "$DIR_SCRIPT/$NAME_SCRIPT"` local PATH_SCRIPT=`$READLINK -f "$DIR_SCRIPT/$NAME_SCRIPT"`
local TMP_CRON_FILE2="$TMP_CRON_FILE-2" local TMP_CRON_FILE2="$TMP_CRON_FILE-2"
@@ -671,6 +789,14 @@ function cron_add {
CRON_COMMAND="$PATH_SCRIPT open $CRON_ARG" CRON_COMMAND="$PATH_SCRIPT open $CRON_ARG"
;; ;;
open_in)
CRON_COMMAND="$PATH_SCRIPT open $CRON_ARG $CRON_ARG2"
;;
open_in_stop)
CRON_COMMAND="$PATH_SCRIPT close $CRON_ARG"
;;
close) close)
CRON_COMMAND="$PATH_SCRIPT close $CRON_ARG" CRON_COMMAND="$PATH_SCRIPT close $CRON_ARG"
;; ;;
@@ -874,6 +1000,24 @@ function get_cron_open {
} }
#
# Cancella tutte le schedulazioni cron per aprire/chiudere una elettrovalvola in modo ritardato
# $1 alias elettrovalvola
#
function del_cron_open_in {
local exists=`alias_exists $1`
if [ "check $exists" = "check FALSE" ]; then
log_write "Alias $1 not found"
echo "Alias $1 not found"
return 1
fi
cron_del "open_in" $1
cron_del "open_in_stop" $1
}
# #
# Legge tutte le schedulazioni cron per chiudere una elettrovalvola # Legge tutte le schedulazioni cron per chiudere una elettrovalvola
# $1 alias elettrovalvola # $1 alias elettrovalvola
@@ -933,15 +1077,17 @@ function del_cron_close {
function show_usage { function show_usage {
echo -e "piGarden v. $VERSION.$SUB_VERSION.$RELEASE_VERSION"
echo -e ""
echo -e "Usage:" echo -e "Usage:"
echo -e "\t$NAME_SCRIPT init initialize supply and solenoid in closed state" echo -e "\t$NAME_SCRIPT init initialize supply and solenoid in closed state"
echo -e "\t$NAME_SCRIPT open alias [force] open a solenoid" echo -e "\t$NAME_SCRIPT open alias [force] open a solenoid"
#echo -e "\t$NAME_SCRIPT open_for time alias [force] open a solenoid for specified time (in minute)" echo -e "\t$NAME_SCRIPT open_in minute_start minute_stop alias [force] open a solenoid in minute_start for minute_stop"
echo -e "\t$NAME_SCRIPT close alias close a solenoid" echo -e "\t$NAME_SCRIPT close alias close a solenoid"
echo -e "\t$NAME_SCRIPT list_alias view list of aliases solenoid" echo -e "\t$NAME_SCRIPT list_alias view list of aliases solenoid"
echo -e "\t$NAME_SCRIPT ev_status alias show status solenoid" echo -e "\t$NAME_SCRIPT ev_status alias show status solenoid"
echo -e "\t$NAME_SCRIPT ev_status_all show status solenoids" echo -e "\t$NAME_SCRIPT ev_status_all show status solenoids"
echo -e "\t$NAME_SCRIPT json_status [get_cron] show status in json format" echo -e "\t$NAME_SCRIPT json_status [get_cron|get_cron_open_in] show status in json format"
echo -e "\t$NAME_SCRIPT check_rain_online check rain from http://api.wunderground.com/" echo -e "\t$NAME_SCRIPT check_rain_online check rain from http://api.wunderground.com/"
echo -e "\t$NAME_SCRIPT check_rain_sensor check rain from hardware sensor" echo -e "\t$NAME_SCRIPT check_rain_sensor check rain from hardware sensor"
echo -e "\t$NAME_SCRIPT close_all_for_rain close all solenoid if it's raining" echo -e "\t$NAME_SCRIPT close_all_for_rain close all solenoid if it's raining"
@@ -963,6 +1109,7 @@ function show_usage {
echo -e "\t$NAME_SCRIPT add_cron_open alias m h dom mon dow add crontab for open a solenoid" echo -e "\t$NAME_SCRIPT add_cron_open alias m h dom mon dow add crontab for open a solenoid"
echo -e "\t$NAME_SCRIPT del_cron_open alias remove all crontab for open a solenoid" echo -e "\t$NAME_SCRIPT del_cron_open alias remove all crontab for open a solenoid"
echo -e "\t$NAME_SCRIPT get_cron_open alias get all crontab for open a solenoid" echo -e "\t$NAME_SCRIPT get_cron_open alias get all crontab for open a solenoid"
echo -e "\t$NAME_SCRIPT del_cron_open_in alias remove all crontab for open_in a solenoid"
echo -e "\t$NAME_SCRIPT add_cron_close alias m h dom mon dow add crontab for close a solenoid" echo -e "\t$NAME_SCRIPT add_cron_close alias m h dom mon dow add crontab for close a solenoid"
echo -e "\t$NAME_SCRIPT del_cron_close alias remove all crontab for close a solenoid" echo -e "\t$NAME_SCRIPT del_cron_close alias remove all crontab for close a solenoid"
echo -e "\t$NAME_SCRIPT get_cron_close alias get all crontab for close a solenoid" echo -e "\t$NAME_SCRIPT get_cron_close alias get all crontab for close a solenoid"
@@ -1033,16 +1180,21 @@ function socket_server_command {
json_error 0 "Alias solenoid not specified" json_error 0 "Alias solenoid not specified"
else else
ev_open $arg2 $arg3 &> /dev/null ev_open $arg2 $arg3 &> /dev/null
json_status json_status "get_cron_open_in"
fi fi
;; ;;
open_in)
ev_open_in $arg2 $arg3 $arg4 $arg5 &> /dev/null
json_status "get_cron_open_in"
;;
close) close)
if [ "empty$arg2" == "empty" ]; then if [ "empty$arg2" == "empty" ]; then
json_error 0 "Alias solenoid not specified" json_error 0 "Alias solenoid not specified"
else else
ev_close $arg2 &> /dev/null ev_close $arg2 &> /dev/null
json_status json_status "get_cron_open_in"
fi fi
;; ;;
@@ -1080,7 +1232,7 @@ function socket_server_command {
if [[ ! -z $vret ]]; then if [[ ! -z $vret ]]; then
json_error 0 "Cron set failed" json_error 0 "Cron set failed"
log_write "Cron set failed: $vret" log_write "Cron del failed: $vret"
else else
message_write "success" "Cron set successfull" message_write "success" "Cron set successfull"
json_status json_status
@@ -1088,6 +1240,22 @@ function socket_server_command {
;; ;;
del_cron_open_in)
local vret=""
vret=`del_cron_open_in $arg2`
if [[ ! -z $vret ]]; then
json_error 0 "Cron del failed"
log_write "Cron del failed: $vret"
else
message_write "success" "Scheduled start successfully deleted"
json_status "get_cron_open_in"
fi
;;
del_cron_close) del_cron_close)
local vret="" local vret=""
@@ -1169,16 +1337,20 @@ function debug2 {
} }
VERSION=0 VERSION=0
SUB_VERSION=2 SUB_VERSION=3
RELEASE_VERSION=0 RELEASE_VERSION=0
DIR_SCRIPT=`dirname $0` DIR_SCRIPT=`dirname $0`
NAME_SCRIPT=${0##*/} NAME_SCRIPT=${0##*/}
CONFIG_ETC="/etc/piGarden.conf" CONFIG_ETC="/etc/piGarden.conf"
TCPSERVER_PID_FILE="/tmp/piGardenTcpServer.pid" TMP_PATH="/run/shm"
if [ ! -d "$TMP_PATH" ]; then
TMP_PATH="/tmp"
fi
TCPSERVER_PID_FILE="$TMP_PATH/piGardenTcpServer.pid"
TCPSERVER_PID_SCRIPT=$$ TCPSERVER_PID_SCRIPT=$$
RUN_FROM_TCPSERVER=0 RUN_FROM_TCPSERVER=0
TMP_CRON_FILE="/tmp/pigarden.user.cron.$$" TMP_CRON_FILE="$TMP_PATH/pigarden.user.cron.$$"
if [ -f $CONFIG_ETC ]; then if [ -f $CONFIG_ETC ]; then
. $CONFIG_ETC . $CONFIG_ETC
@@ -1204,18 +1376,8 @@ case "$1" in
ev_open $2 $3 ev_open $2 $3
;; ;;
open_for) open_in)
re='^[0-9]+$' ev_open_in $2 $3 $4 $5
if ! [[ $2 =~ $re ]] ; then
echo -e "Time of irrigation is wrong or not specified"
exit 1
fi
if [ "empty$3" == "empty" ]; then
echo -e "Alias solenoid not specified"
exit 1
fi
ev_open $3 $4
;; ;;
close) close)
@@ -1335,6 +1497,10 @@ case "$1" in
del_cron_open $2 del_cron_open $2
;; ;;
del_cron_open_in)
del_cron_open_in $2
;;
get_cron_open) get_cron_open)
get_cron_open $2 get_cron_open $2
;; ;;
@@ -1366,5 +1532,7 @@ case "$1" in
;; ;;
esac esac
# Elimina eventuali file temporane utilizzati per la gestione dei cron
rm "$TMP_CRON_FILE" 2> /dev/null
rm "$TMP_CRON_FILE-2" 2> /dev/null