5 Commits

6 changed files with 390 additions and 31 deletions

View File

@@ -1,3 +1,10 @@
# 0.6.4 - 03/10/2021
- Add support for sensor: moisture, temperature, fertility, illuminance
- Add command line: sensor_status, sensor_status_all, sensor_status_set
- Add api command: sensor_status_set
- Add event ev_not_open_for_moisture, sensor_set_state_before, sensor_set_state_after
- Added zone humidity management: when a zone has reached the humidity defined in EVx_SENSOR_MOISTURE it does not start irrigation or interrupts it if it is already active. In case of rain it does not stop irrigation if the soil humidity has not reached the configured value
# 0.6.3 - 10/08/2021 # 0.6.3 - 10/08/2021
- Add command last_rain_sensor_timestamp, last_rain_online_timestamp, reset_last_rain_sensor_timestamp, reset_last_rain_online_timestamp - Add command last_rain_sensor_timestamp, last_rain_online_timestamp, reset_last_rain_sensor_timestamp, reset_last_rain_online_timestamp
- Add socket server api for reset_last_rain_sensor_timestamp, reset_last_rain_online_timestamp - Add socket server api for reset_last_rain_sensor_timestamp, reset_last_rain_online_timestamp

View File

@@ -107,6 +107,9 @@ EV1_ALIAS="1" #
EV1_GPIO=17 # Physical 11 - wPi 0 EV1_GPIO=17 # Physical 11 - wPi 0
#EV1_NORAIN=1 # Non interrompe l'irrigazione di questa zona in caso di pioggia #EV1_NORAIN=1 # Non interrompe l'irrigazione di questa zona in caso di pioggia
#EV1_MONOSTABLE=1 # L'elettrovalvola è monostabile #EV1_MONOSTABLE=1 # L'elettrovalvola è monostabile
#EV1_SENSOR_ALIAS=Mi_Flora # Nome del sensore (definito in SENSORx_ALIAS) per stabilire l'umidità del terreno
#EV1_SENSOR_MOISTURE=50 # Percentule di umidità ottimale
#EV1_SENSOR_MOISTURE_AUTOCLOSE=1 # Chiude automaticamente l'elettrovalvola quando supera l'umidità impostata in EVx_MONOSTABLE
EV2_ALIAS="2" # EV2_ALIAS="2" #
EV2_GPIO=27 # Physical 13 - wPi 2 EV2_GPIO=27 # Physical 13 - wPi 2
@@ -124,6 +127,16 @@ EV6_ALIAS="6" #
EV6_GPIO=24 # Physical 18 - wPi 5 EV6_GPIO=24 # Physical 18 - wPi 5
# Numero di sensori
SENSOR_TOTAL=1
# Definizione sensori
SENSOR1_ALIAS=Mi_Flora
# Definisci il servizio online da utilizzare per il controllo delle condizioni meteo, puoi scegliere openweathermap oppure wunderground. # Definisci il servizio online da utilizzare per il controllo delle condizioni meteo, puoi scegliere openweathermap oppure wunderground.
# Se non vuoi configurare nessun servizio imposta il vale "none" # Se non vuoi configurare nessun servizio imposta il vale "none"
WEATHER_SERVICE="openweathermap" WEATHER_SERVICE="openweathermap"

View File

@@ -75,11 +75,32 @@ function check_rain_sensor {
} }
# #
# Chiude tutte le elettrovalvole se sta piovendo # Chiude tutte le elettrovalvole se sta piovendo oppure chiude quelle che hanno raggiunto l'mudità massima impostata
# Eseguie il controllo in tempo reale sul sensore hardware e sui dati dell'ultima chiamata eseguita online # Eseguie il controllo in tempo reale sul sensore hardware e sui dati dell'ultima chiamata eseguita online
# #
function close_all_for_rain { function close_all_for_rain {
#
# Chiude le elettrovalvole che hanno raggiunto l'umidità del terreno impostata in EVx_SENSOR_MOISTURE
#
for i in $(seq $EV_TOTAL)
do
local a=EV"$i"_ALIAS
local al=${!a}
ev_status $al
local state=$?
local moisture=$(ev_check_moisture_autoclose $i)
if [ "$state" = "1" ] && [ "$moisture" -gt 0 ]; then
ev_close $al
log_write "irrigate" "warning" "close_all_for_rain - Close solenoid '$al' because maximum soil moisture has been reached"
fi
done
#
# Chiude le elettrovalvole in caso di pioggia
#
local close_all=0 local close_all=0
local now=`date +%s` local now=`date +%s`
@@ -102,6 +123,12 @@ function close_all_for_rain {
fi fi
if [ "$close_all" = "1" ]; then if [ "$close_all" = "1" ]; then
#
# PIOVE
# Valuta se la sciare aperte le elettrovalvole in caso l'umidità del sensore non sia stata raggiunta
#
for i in $(seq $EV_TOTAL) for i in $(seq $EV_TOTAL)
do do
local a=EV"$i"_ALIAS local a=EV"$i"_ALIAS
@@ -110,14 +137,17 @@ function close_all_for_rain {
local evnorain=${!a} local evnorain=${!a}
ev_status $al ev_status $al
local state=$? local state=$?
local moisture=$(ev_check_moisture $i)
#echo "$al = $state" #echo "$al = $state"
if [ "$state" = "1" ] && [ "$evnorain" != "1" ]; then if [ "$state" = "1" ] && [ "$evnorain" != "1" ] && [ "$moisture" -ne 0 ]; then
ev_close $al ev_close $al
log_write "irrigate" "warning" "close_all_for_rain - Close solenoid '$al' for rain" log_write "irrigate" "warning" "close_all_for_rain - Close solenoid '$al' for rain"
fi fi
done done
fi fi
} }
# #

249
include/sensor.include.sh Normal file
View File

@@ -0,0 +1,249 @@
#
# Imposta lo stato di una elettrovalvola
# $1 numero del sensore
# $2 tipologia dello stato
# $3 stato da scrivere
#
function sensor_set_state {
trigger_event "sensor_set_state_before" $1 $2 $3
echo "$3" > "$STATUS_DIR/sensor$1_$2"
trigger_event "sensor_set_state_after" $1 $2 $3
}
#
# Legge lo stato di un sensore
# $1 numero del sensore
# $2 tipologia dello stato
#
function sensor_get_state {
if [ ! -f "$STATUS_DIR/sensor$1_$2" ]; then
sensor_set_state $1 $2 ""
fi
local state=$(cat "$STATUS_DIR/sensor$1_$2" 2> /dev/null)
echo $state
}
#
# Recupera il numero di un sensore in base all'alias
# $1 alias del sensore
#
function sensor_alias2number {
for i in $(seq $EV_TOTAL)
do
a=SENSOR"$i"_ALIAS
av=${!a}
if [ "$av" == "$1" ]; then
return $i
fi
done
log_write "general" "error" "ERROR sensor alias not found: $1"
message_write "warning" "Sensor alias not found"
mqtt_status
exit 1
}
#
# Verifica se un alias di un sensore esiste
# $1 alias dell'elettrovalvola
#
function sensor_alias_exists {
local vret='FALSE'
for i in $(seq $EV_TOTAL)
do
a=SENSOR"$i"_ALIAS
av=${!a}
if [ "$av" == "$1" ]; then
vret='TRUE'
fi
done
echo $vret
}
#
# Mostra lo stato di tutte le elettrovalvole
#
function sensor_status_all {
for i in $(seq $SENSOR_TOTAL)
do
a=SENSOR"$i"_ALIAS
av=${!a}
for t in $SENSOR_STATE_TYPE
do
local state=$(sensor_get_state $i $t)
echo -e "$av: $t $state"
done
done
}
#
# Mostra lo stato di un sensore
# $1 alias sensore
# $2 tipologia dello stato
#
function sensor_status {
sensor_alias2number $1
i=$?
if [ -z "$2" ]; then
for t in $SENSOR_STATE_TYPE
do
local state=$(sensor_get_state $i $t)
echo -e "$av: $t $state"
done
else
local state=$(sensor_get_state $i $2)
echo -e "$state"
fi
}
#
# Imposta lo stato di un sensore per alias
# $1 alias sensore
# $2 tipologia dello stato
# $3 stato da imopostare
#
function sensor_status_set {
sensor_alias2number $1
i=$?
sensor_set_state $i $2 $3
mqtt_status
}
#
# Stampa la lista degli alias dei sensori
#
function list_alias_sensor {
for i in $(seq $SENSOR_TOTAL)
do
local a=SENSOR"$i"_ALIAS
local al=${!a}
echo $al
done
}
#
# Stampa lo stato di tutti i sensori in formato json
#
function json_sensor_status_all {
local js=""
local js_item=""
local js_type=""
for i in $(seq $SENSOR_TOTAL)
do
a=SENSOR"$i"_ALIAS
av=${!a}
js_type=""
for t in $SENSOR_STATE_TYPE
do
local state=$(sensor_get_state $i $t)
js_type="$js_type \"$t\": \"$state\", "
done
js_type="${js_type::-2}"
js_item="$js_item \"$av\":{$js_type}, ";
done
if [[ ! -z $js_item ]]; then
js_item="${js_item::-2}"
fi
js="\"sensor\": {$js_item}"
echo $js
}
#
# Controlla se la zona comandata da un elettrovalvola ha raggiunto l'umidità necessaria per non fare partire l'irrigazione,
# faerla chiudere in caso di pioggia.
# Se è stata superata l'umidità indicata in EVx_SENSOR_MOISTURE ritorna l'umidità attuale del sensore relativo all'elettrovalvola
# in caso contrario ritorna 0, se no è impostato il parametro EV_xSENSOR_ALIAS o EVxSENSOR?MOISTURE ritorna il valore -1
#
# $1 numero elettrovalvola da controllare
#
function ev_check_moisture {
local s=EV"$1"_SENSOR_ALIAS
local sa=${!s}
if [[ -z $sa ]]; then
echo -1
return
fi
local moisture=$(sensor_status $sa moisture)
local s=EV"$1"_SENSOR_MOISTURE
local max_moisture=${!s}
if [ -z $max_moisture ]; then
echo -1
return
fi
if [ $moisture -gt $max_moisture ]; then
log_write "sensor" "info" "ev_check_moisture_autoclose: humidity of the \"$sa\" sensor reached: $moisture%"
echo $moisture
return $moisture
fi
echo 0
return 0
}
#
# Controlla se la zona comandata da un elettrovalvola ha raggiunto l'umidità necessaria per essere chiusa in automatico
# Se è stata superata l'umidità indicata in EVx_SENSOR_MOISTURE ritorna l'umidità attuale del sensore relativo all'elettrovalvola
# in caso contrario ritorna 0, se no è impostato il parametro EV_xSENSOR_ALIAS, EVxSENSOR_MOISTURE o
# EVxSENSOR_MOISTURE_AUTOCLOSE ritorna il valore -1
#
# $1 numero elettrovalvola da controllare
#
function ev_check_moisture_autoclose {
local s=EV"$1"_SENSOR_ALIAS
local sa=${!s}
if [[ -z $sa ]]; then
echo -1
return
fi
local moisture=$(sensor_status $sa moisture)
local s=EV"$1"_SENSOR_MOISTURE
local max_moisture=${!s}
if [ -z $max_moisture ]; then
echo -1
return
fi
local s=EV"$1"_SENSOR_MOISTURE_AUTOCLOSE
local autoclose=${!s}
if [ -z $autoclose ]; then
echo -1
return
fi
if [ $autoclose -ne 1 ]; then
echo -1
return
fi
if [ $moisture -gt $max_moisture ]; then
log_write "sensor" "info" "ev_check_moisture_autoclose: humidity of the \"$sa\" sensor reached: $moisture%"
echo $moisture
return $moisture
fi
echo 0
return 0
}

View File

@@ -252,6 +252,14 @@ function socket_server_command {
json_status json_status
;; ;;
sensor_status_set)
if [ "empty$arg2" == "empty" ]; then
json_error 0 "Alias sensor not specified"
else
sensor_status_set $arg2 $arg3 $arg4 &> /dev/null
json_status
fi
;;
*) *)
json_error 0 "invalid command" json_error 0 "invalid command"

View File

@@ -88,35 +88,48 @@ function ev_open {
local EV_IS_MONOSTABLE_VAR=EV"$EVNUM"_MONOSTABLE local EV_IS_MONOSTABLE_VAR=EV"$EVNUM"_MONOSTABLE
local EV_IS_MONOSTABLE=${!EV_IS_MONOSTABLE_VAR} local EV_IS_MONOSTABLE=${!EV_IS_MONOSTABLE_VAR}
if [ ! "$2" = "force" ] && [ "$EVNORAIN" != "1" ]; then if [ ! "$2" = "force" ]; 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 moisture=$(ev_check_moisture $EVNUM)
local now=`date +%s` if [ $moisture -gt 0 ]; then
local dif=0 message_write "warning" "solenoid not open because maximum soil moisture has been reached"
let "dif = now - last_rain" trigger_event "ev_not_open_for_moisture" "$1"
if [ $dif -lt $NOT_IRRIGATE_IF_RAIN_ONLINE ]; then log_write "irrigate" "warning" "Solenoid '$1' not open because maximum soil moisture has been reached"
message_write "warning" "Solenoid not open for rain" return
trigger_event "ev_not_open_for_rain_online" "$1" fi
trigger_event "ev_not_open_for_rain" "$1"
log_write "irrigate" "warning" "Solenoid '$1' not open for rain (online check)" if [ "$EVNORAIN" != "1" ]; then
return
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 now=`date +%s`
local dif=0
let "dif = now - last_rain"
if [ $dif -lt $NOT_IRRIGATE_IF_RAIN_ONLINE ] && [ $moisture -ne 0 ]; then
message_write "warning" "Solenoid not open for rain"
trigger_event "ev_not_open_for_rain_online" "$1"
trigger_event "ev_not_open_for_rain" "$1"
log_write "irrigate" "warning" "Solenoid '$1' not open for rain (online check)"
return
fi
fi
check_rain_sensor
if [[ "$NOT_IRRIGATE_IF_RAIN_SENSOR" -gt 0 && -f $STATUS_DIR/last_rain_sensor && $moisture -ne 0 ]]; then
local last_rain=`cat $STATUS_DIR/last_rain_sensor`
local now=`date +%s`
local dif=0
let "dif = now - last_rain"
if [ $dif -lt $NOT_IRRIGATE_IF_RAIN_SENSOR ]; then
message_write "warning" "Solenoid not open for rain"
trigger_event "ev_not_open_for_rain_sensor" "$1"
trigger_event "ev_not_open_for_rain" "$1"
log_write "irrigate" "warning" "Solenoid '$1' not open for rain (sensor check)"
return
fi
fi fi
fi fi
check_rain_sensor
if [[ "$NOT_IRRIGATE_IF_RAIN_SENSOR" -gt 0 && -f $STATUS_DIR/last_rain_sensor ]]; then
local last_rain=`cat $STATUS_DIR/last_rain_sensor`
local now=`date +%s`
local dif=0
let "dif = now - last_rain"
if [ $dif -lt $NOT_IRRIGATE_IF_RAIN_SENSOR ]; then
message_write "warning" "Solenoid not open for rain"
trigger_event "ev_not_open_for_rain_sensor" "$1"
trigger_event "ev_not_open_for_rain" "$1"
log_write "irrigate" "warning" "Solenoid '$1' not open for rain (sensor check)"
return
fi
fi
fi fi
local state=1 local state=1
@@ -684,8 +697,9 @@ function json_status {
fi fi
local json_cron_open_in="\"cron_open_in\":{$json_get_cron_open_in}" local json_cron_open_in="\"cron_open_in\":{$json_get_cron_open_in}"
local json_timestamp="\"timestamp\": $(date +%s)" local json_timestamp="\"timestamp\": $(date +%s)"
local json_sensor="$(json_sensor_status_all)"
json="{$json_version,$json_timestamp,$json_event,$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 $json_schedule}" json="{$json_version,$json_timestamp,$json_event,$json,$json_sensor,$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 $json_schedule}"
echo "$json" echo "$json"
@@ -727,14 +741,25 @@ function show_usage {
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 "\n"
echo -e "\t$NAME_SCRIPT list_alias_sensor view list of aliases sensor"
echo -e "\t$NAME_SCRIPT sensor_status alias [type] show status sensor (type: $SENSOR_STATE_TYPE)"
echo -e "\t$NAME_SCRIPT sensor_status_set alias type value set status of sensor (type: $SENSOR_STATE_TYPE)"
echo -e "\t$NAME_SCRIPT sensor_status_all show status of all sensors"
echo -e "\n"
echo -e "\t$NAME_SCRIPT last_rain_sensor_timestamp show timestamp of last rain sensor" echo -e "\t$NAME_SCRIPT last_rain_sensor_timestamp show timestamp of last rain sensor"
echo -e "\t$NAME_SCRIPT last_rain_online_timestamp show timestamp of last rain online" echo -e "\t$NAME_SCRIPT last_rain_online_timestamp show timestamp of last rain online"
echo -e "\t$NAME_SCRIPT reset_last_rain_sensor_timestamp show timestamp of last rain sensor" echo -e "\t$NAME_SCRIPT reset_last_rain_sensor_timestamp show timestamp of last rain sensor"
echo -e "\t$NAME_SCRIPT reset_last_rain_online_timestamp show timestamp of last rain online" echo -e "\t$NAME_SCRIPT reset_last_rain_online_timestamp show timestamp of last rain online"
echo -e "\n"
echo -e "\t$NAME_SCRIPT json_status [get_cron|get_cron_open_in|get_schedule] show status in json format" echo -e "\t$NAME_SCRIPT json_status [get_cron|get_cron_open_in|get_schedule] show status in json format"
echo -e "\t$NAME_SCRIPT mqtt_status send status in json format to mqtt broker" echo -e "\t$NAME_SCRIPT mqtt_status send status in json format to mqtt broker"
echo -e "\t$NAME_SCRIPT check_rain_online check rain from http://api.wunderground.com/" echo -e "\n"
echo -e "\t$NAME_SCRIPT check_rain_online check rain from online api service"
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 "\n"
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"
echo -e "\t$NAME_SCRIPT close_all [force] close all solenoid" echo -e "\t$NAME_SCRIPT close_all [force] close all solenoid"
echo -e "\n" echo -e "\n"
@@ -1008,7 +1033,7 @@ function debug2 {
VERSION=0 VERSION=0
SUB_VERSION=6 SUB_VERSION=6
RELEASE_VERSION=3 RELEASE_VERSION=4
DIR_SCRIPT=`dirname $0` DIR_SCRIPT=`dirname $0`
NAME_SCRIPT=${0##*/} NAME_SCRIPT=${0##*/}
@@ -1034,6 +1059,7 @@ fi
. "$DIR_SCRIPT/include/cron.include.sh" . "$DIR_SCRIPT/include/cron.include.sh"
. "$DIR_SCRIPT/include/socket.include.sh" . "$DIR_SCRIPT/include/socket.include.sh"
. "$DIR_SCRIPT/include/rain.include.sh" . "$DIR_SCRIPT/include/rain.include.sh"
. "$DIR_SCRIPT/include/sensor.include.sh"
. "$DIR_SCRIPT/include/events.include.sh" . "$DIR_SCRIPT/include/events.include.sh"
MESSAGE_INFO="" MESSAGE_INFO=""
@@ -1043,8 +1069,14 @@ MESSAGE_SUCCESS=""
CURRENT_EVENT="" CURRENT_EVENT=""
CURRENT_EVENT_ALIAS="" CURRENT_EVENT_ALIAS=""
SENSOR_STATE_TYPE="moisture temperature fertility illuminance"
PARENT_PID=0 PARENT_PID=0
if [ -z $SENSOR_TOTAL ]; then
SENSOR_TOTAL=0
fi
if [ -z $LOG_OUTPUT_DRV_FILE ]; then if [ -z $LOG_OUTPUT_DRV_FILE ]; then
LOG_OUTPUT_DRV_FILE="/dev/null" LOG_OUTPUT_DRV_FILE="/dev/null"
fi fi
@@ -1115,6 +1147,26 @@ case "$1" in
ev_status_all ev_status_all
;; ;;
list_alias_sensor)
list_alias_sensor
;;
sensor_status)
sensor_status $2 $3
;;
sensor_status_all)
sensor_status_all
;;
sensor_status_set)
sensor_status_set $2 $3 $4
;;
json_sensor_status_all)
json_sensor_status_all
;;
json_status) json_status)
json_status $2 $3 $4 $5 $6 json_status $2 $3 $4 $5 $6
;; ;;