Aggiunto implementazione driver rainsensorqty railizzato da @androtto
This commit is contained in:
@@ -4,21 +4,28 @@
|
|||||||
#RAINSENSORQTY_MONPID="$TMP_PATH/rainsensorqty_monitor.pid"
|
#RAINSENSORQTY_MONPID="$TMP_PATH/rainsensorqty_monitor.pid"
|
||||||
#
|
#
|
||||||
|
|
||||||
function drv_rainsensorqty_check () {
|
|
||||||
|
|
||||||
if [[ -f "$RAINSENSORQTY_MONPID" && -z "$RAINSENSORQTY_MONPID" ]] ; then
|
function drv_rainsensorqty_writelog {
|
||||||
pid=$( < "$RAINSENSORQTY_MONPID" )
|
#2 variables - $1 function, $2 message
|
||||||
echo "drv_rainsensorqty_check - NORMAL: checking if $pid pid is running" >> "$LOG_OUTPUT_DRV_FILE"
|
echo -e "$1 - `date`\t\t$2" >> "$LOG_OUTPUT_DRV_FILE"
|
||||||
log_write "$$ pid monitor process - see $RAINSENSORQTY_MONPID"
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function drv_rainsensorqty_check () {
|
||||||
|
local f="drv_rainsensorqty_check"
|
||||||
|
|
||||||
|
if [[ -f "$RAINSENSORQTY_MONPID" ]] ; then
|
||||||
|
local pid=$( < "$RAINSENSORQTY_MONPID" )
|
||||||
|
drv_rainsensorqty_writelog $f "NORMAL: checking if $pid pid is running"
|
||||||
if ps -fp $pid >/dev/null ; then
|
if ps -fp $pid >/dev/null ; then
|
||||||
echo "drv_rainsensorqty_check - NORMAL: $pid pid is running" >> "$LOG_OUTPUT_DRV_FILE"
|
drv_rainsensorqty_writelog $f "NORMAL: $pid pid is running"
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
echo "drv_rainsensorqty_check - NORMAL: $pid pid is NOT running" >> "$LOG_OUTPUT_DRV_FILE"
|
drv_rainsensorqty_writelog $f "$pid pid monitor process NOT running - $RAINSENSORQTY_MONPID file contains $pid"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
echo "drv_rainsensorqty_check - ERROR: no raining monitor process file \$RAINSENSORQTY_MONPID" >> "$LOG_OUTPUT_DRV_FILE"
|
drv_rainsensorqty_writelog $f "ERROR: no raining monitor process file \$RAINSENSORQTY_MONPID"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,12 @@
|
|||||||
declare -g RAINSENSORQTY_LASTRAIN
|
declare -g RAINSENSORQTY_LASTRAIN
|
||||||
RAINSENSORQTY_LASTRAIN="$STATUS_DIR/rainsensorqty_lastrain"
|
RAINSENSORQTY_LASTRAIN="$STATUS_DIR/rainsensorqty_lastrain"
|
||||||
|
|
||||||
declare -g RAINSENSORQTY_LOG
|
#not used, to be checked
|
||||||
RAINSENSORQTY_LOG="$DIR_SCRIPT/log/rainsensorqty.log"
|
#declare -g RAINSENSORQTY_LOG
|
||||||
|
#RAINSENSORQTY_LOG="$DIR_SCRIPT/log/rainsensorqty.log"
|
||||||
|
|
||||||
|
declare -g RAINSENSORQTY_MONITORLOG
|
||||||
|
RAINSENSORQTY_MONITORLOG="$DIR_SCRIPT/log/rainsensorqty_monitor.log"
|
||||||
|
|
||||||
declare -g RAINSENSORQTY_PID
|
declare -g RAINSENSORQTY_PID
|
||||||
RAINSENSORQTY_MONPID="$TMP_PATH/rainsensorqty_monitor.pid"
|
RAINSENSORQTY_MONPID="$TMP_PATH/rainsensorqty_monitor.pid"
|
||||||
@@ -19,3 +23,8 @@ RAINSENSORQTY_PULSE=falling
|
|||||||
|
|
||||||
declare -g RAINSENSORQTY_WAIT
|
declare -g RAINSENSORQTY_WAIT
|
||||||
RAINSENSORQTY_WAIT=rising
|
RAINSENSORQTY_WAIT=rising
|
||||||
|
|
||||||
|
# mm of water for each pulse - default
|
||||||
|
declare -g RAINSENSORQTY_MMEACH
|
||||||
|
RAINSENSORQTY_MMEACH=0.303030303
|
||||||
|
|
||||||
|
|||||||
@@ -6,18 +6,23 @@
|
|||||||
#RAINSENSORQTY_SECSBETWEENRAINEVENT=10800 # =3h, significa che dopo 3 si resetta il numero di vaschette da riempire e solo dopo il riempimento del numero di vaschette si considera una nuova pioggia
|
#RAINSENSORQTY_SECSBETWEENRAINEVENT=10800 # =3h, significa che dopo 3 si resetta il numero di vaschette da riempire e solo dopo il riempimento del numero di vaschette si considera una nuova pioggia
|
||||||
|
|
||||||
function drv_rainsensorqty_init {
|
function drv_rainsensorqty_init {
|
||||||
|
local f="drv_rainsensorqty_init"
|
||||||
|
local monitor_sh="$RAINSENSORQTY_DIR/rainsensorqty_monitor.sh"
|
||||||
|
|
||||||
# format RAIN_GPIO="drv:rainsensorqty:25"
|
# format RAIN_GPIO="drv:rainsensorqty:25"
|
||||||
|
|
||||||
# esegue rainmonitor
|
drv_rainsensorqty_writelog $f "NORMAL - executing $monitor_sh"
|
||||||
if [[ -x "$monitor_sh" ]] ; then
|
|
||||||
echo "OK - run rainmonitor"
|
|
||||||
nohup "$monitor_sh" $gpio $RAINSENSORQTY_LOOPSFORSETRAINING $RAINSENSORQTY_SECSBETWEENRAINEVENT >> /tmp/debug_rainmonitor.log 2>&1 &
|
|
||||||
sleep 1
|
|
||||||
else
|
|
||||||
:
|
|
||||||
echo "KO - not run rainmonitor"
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
# esegue rainmonitor
|
||||||
|
if [ -x "$monitor_sh" ] ; then
|
||||||
|
nohup "$monitor_sh" >> $RAINSENSORQTY_MONITORLOG 2>&1 &
|
||||||
|
sleep 1
|
||||||
|
drv_rainsensorqty_writelog $f "NORMAL: $monitor_sh has pid $( < $RAINSENSORQTY_MONPID)"
|
||||||
|
else
|
||||||
|
drv_rainsensorqty_writelog $f "ERROR: cannot find \"\$monitor_sh \" "
|
||||||
|
fi
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set | $GREP -e ^GPIO -e ^LOG -e ^CUT -e ^JQ -e ^RAIN -e ^SCR -e ^TMP > "$RAINSENSORQTY_DIR/set_var"
|
||||||
|
|
||||||
|
|||||||
@@ -4,14 +4,13 @@
|
|||||||
# $1 identificativo gpio del sensore di pioggia
|
# $1 identificativo gpio del sensore di pioggia
|
||||||
#
|
#
|
||||||
function drv_rainsensorqty_rain_sensor_init {
|
function drv_rainsensorqty_rain_sensor_init {
|
||||||
|
drv_rainsensorqty_writelog "drv_rainsensorqty_rain_sensor_init" $1
|
||||||
echo "drv_rainsensorqty_rain_sensor_init $1" >> "$LOG_OUTPUT_DRV_FILE"
|
|
||||||
|
|
||||||
local drvt="$( echo $RAIN_GPIO | $CUT -f 1 -d: )"
|
local drvt="$( echo $RAIN_GPIO | $CUT -f 1 -d: )"
|
||||||
local drv="$( echo $RAIN_GPIO | $CUT -f 2 -d: )"
|
local drv="$( echo $RAIN_GPIO | $CUT -f 2 -d: )"
|
||||||
local gpio="$( echo $RAIN_GPIO | $CUT -f 3 -d: )"
|
local gpio_port="$( echo $RAIN_GPIO | $CUT -f 3 -d: )"
|
||||||
|
|
||||||
$GPIO -g mode $gpio in
|
$GPIO -g mode $gpio_port in
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,34 +20,34 @@ function drv_rainsensorqty_rain_sensor_init {
|
|||||||
# $1 identificativo gpio del sensore di pioggia
|
# $1 identificativo gpio del sensore di pioggia
|
||||||
#
|
#
|
||||||
function drv_rainsensorqty_rain_sensor_get {
|
function drv_rainsensorqty_rain_sensor_get {
|
||||||
|
local now=$(date +%s)
|
||||||
|
local interval=100 # 100 secondi > 1 minuto frequenza di verifica da parte di, come da seguente schedulazione
|
||||||
|
#* * * * * /home/pi/piGarden/piGarden.sh check_rain_sensor 2> /tmp/check_rain_sensor.err
|
||||||
|
local f="drv_rainsensorqty_check"
|
||||||
|
drv_rainsensorqty_writelog $f $1
|
||||||
|
|
||||||
echo "drv_rainsensorqty_rain_sensor_get $1" >> "$LOG_OUTPUT_DRV_FILE"
|
# verifica se lo script di monitorin e' attivo
|
||||||
|
if drv_rainsensorqty_check ; then
|
||||||
|
drv_rainsensorqty_writelog $f "NORMAL - drv_rainsensorqty_check ok, monitor process running"
|
||||||
|
if [ -f "$RAINSENSORQTY_LASTRAIN" ] ; then
|
||||||
|
local lastrain="$( < "$RAINSENSORQTY_LASTRAIN" )"
|
||||||
|
(( diff = now - lastrain ))
|
||||||
|
drv_rainsensorqty_writelog $f "NORMAL: last rain $( date --date="@$lastrain" ) "
|
||||||
|
drv_rainsensorqty_writelog $f "NORMAL: check rain $( date --date="@$now" ) "
|
||||||
|
if (( diff <= interval )) ; then
|
||||||
|
drv_rainsensorqty_writelog $f "RAIN : check rain - diff $diff < $interval - return $RAIN_GPIO_STATE"
|
||||||
|
return $RAIN_GPIO_STATE
|
||||||
|
else
|
||||||
|
drv_rainsensorqty_writelog $f "NO_RAIN : check rain - diff $diff < $interval - return 99"
|
||||||
|
return 99
|
||||||
|
fi
|
||||||
|
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
drv_rainsensorqty_writelog $f "ERROR - drv_rainsensorqty_check failed, no monitor process running"
|
||||||
|
exit 1
|
||||||
|
|
||||||
if [ drv_rainsensorqty_check ]; then
|
|
||||||
# Cosa deve fare ????
|
|
||||||
# gli facciamo rieseguire drv_rainsensorqty_init in modo che riavvii il monitor ???
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
local state_rain=""
|
|
||||||
|
|
||||||
# INSERISCI QUI DENTRO I CONTROLLI SUL FILE $STATUS_DIR/rainsensorqty_lastrain
|
|
||||||
# SE SI VERIFICANO LE CONDIZIONI PER CUI SI DEVE INTERROMPERE L'IRRIGAZIONE
|
|
||||||
# DEVI IMPOSTARE last_rain CON IL VALORE $RAIN_GPIO_STATE
|
|
||||||
|
|
||||||
#
|
|
||||||
#
|
|
||||||
#
|
|
||||||
#
|
|
||||||
#
|
|
||||||
#
|
|
||||||
#
|
|
||||||
#
|
|
||||||
#
|
|
||||||
#
|
|
||||||
|
|
||||||
echo $state_rain
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,36 +1,33 @@
|
|||||||
#!/bin/bash -x
|
#!/bin/bash
|
||||||
#
|
#
|
||||||
# Bash script to measure rainfall
|
# Bash monitor script to measure rainfall
|
||||||
# Author: androtto
|
# Author: androtto
|
||||||
# Url:
|
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
# Scrive un messaggio nel file di log
|
|
||||||
# $1 log da scrivere
|
|
||||||
# 3 parameter expected in order:
|
|
||||||
#$gpio
|
|
||||||
#$RAINSENSORQTY_LOOPSFORSETRAINING
|
|
||||||
#$RAINSENSORQTY_SECSBETWEENRAINEVENT
|
|
||||||
|
|
||||||
function log_write {
|
|
||||||
echo -e "`date`\t\t$1" >> $RAINSENSORQTY_LOG
|
|
||||||
}
|
|
||||||
|
|
||||||
###############
|
###############
|
||||||
# MAIN
|
# MAIN #
|
||||||
###############
|
###############
|
||||||
|
|
||||||
if [ ! $# = 3 ] ; then
|
|
||||||
echo "ERROR: 3 parameters expected"
|
|
||||||
fi
|
|
||||||
|
|
||||||
GPIO=$1
|
|
||||||
LOOPSFORSETRAINING=$2
|
|
||||||
SECSBETWEENRAINEVENT=$3
|
|
||||||
|
|
||||||
DIRNAME="$( dirname $0 )"
|
DIRNAME="$( dirname $0 )"
|
||||||
. /etc/piGarden.conf # test sulla presenza del file gia' fatti prima
|
f="$(basename $0)"
|
||||||
. "$DIRNAME/config.include.sh"
|
. $DIRNAME/common.include.sh
|
||||||
|
|
||||||
|
echo "$(date) ---------------- NEW RUN "
|
||||||
|
|
||||||
|
if [[ -f "$DIRNAME/set_var" ]] ; then
|
||||||
|
echo "NORMAL: file $DIRNAME/set_var found - getting variables"
|
||||||
|
. "$DIRNAME/set_var"
|
||||||
|
else
|
||||||
|
echo "ERROR: $DIRNAME/set_var not found"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
#drvt="$( echo $RAIN_GPIO | $CUT -f 1 -d: )"
|
||||||
|
#drv="$( echo $RAIN_GPIO | $CUT -f 2 -d: )"
|
||||||
|
gpio_port="$( echo $RAIN_GPIO | $CUT -f 3 -d: )"
|
||||||
|
|
||||||
#got from config file above:
|
#got from config file above:
|
||||||
#RAINSENSORQTY_LASTRAIN="$STATUS_DIR/rainsensorqty_lastrain"
|
#RAINSENSORQTY_LASTRAIN="$STATUS_DIR/rainsensorqty_lastrain"
|
||||||
@@ -41,43 +38,47 @@ DIRNAME="$( dirname $0 )"
|
|||||||
#RAINSENSORQTY_WAIT=rising
|
#RAINSENSORQTY_WAIT=rising
|
||||||
|
|
||||||
|
|
||||||
if [[ -f "$RAINSENSORQTY_MONPID" && -z "$RAINSENSORQTY_MONPID" ]] ; then
|
# no other monitor process running...
|
||||||
pid=$( < "$RAINSENSORQTY_MONPID" )
|
if [[ -f "$RAINSENSORQTY_MONPID" ]] ; then
|
||||||
|
pid="$( < "$RAINSENSORQTY_MONPID" )"
|
||||||
if ps -fp $pid >/dev/null ; then
|
if ps -fp $pid >/dev/null ; then
|
||||||
log_write "ERROR monitor process already running\n$( ps -fp $pid )"
|
drv_rainsensorqty_writelog $f "ERROR monitor process already running\n$( ps -fp $pid )"
|
||||||
exit 1
|
exit 1
|
||||||
else
|
fi
|
||||||
log_write "no rainmonitor process running"
|
fi
|
||||||
|
|
||||||
|
#drv_rainsensorqty_writelog $f "NORMAL - no rainmonitor process already running"
|
||||||
echo $$ > $RAINSENSORQTY_MONPID
|
echo $$ > $RAINSENSORQTY_MONPID
|
||||||
log_write "$$ pid monitor process - see $RAINSENSORQTY_MONPID"
|
drv_rainsensorqty_writelog $f "NORMAL - $$ pid monitor process started - see $RAINSENSORQTY_MONPID"
|
||||||
fi
|
#echo "NORMAL: no raining monitor process $( echo ; ps -ef | grep rain | grep monitor)"
|
||||||
fi
|
|
||||||
|
|
||||||
MMEACH=0.303030303
|
|
||||||
|
|
||||||
|
# init variables
|
||||||
|
MMEACH="$RAINSENSORQTY_MMEACH"
|
||||||
counter=0
|
counter=0
|
||||||
|
|
||||||
while true
|
while true
|
||||||
do
|
do
|
||||||
before=`date +%s`
|
before=`date +%s`
|
||||||
#gpio -g wfi $GPIO $RAINSENSORQTY_PULSE # falling 1->0
|
echo $GPIO -g wfi $gpio_port $RAINSENSORQTY_PULSE # falling 1->0
|
||||||
sleep $(</tmp/secs_to_wait) # for testing only
|
$GPIO -g wfi $gpio_port $RAINSENSORQTY_PULSE # falling 1->0
|
||||||
|
#sleep $(</tmp/secs_to_wait) # for testing only
|
||||||
now=`date +%s`
|
now=`date +%s`
|
||||||
(( elapsed = now - before ))
|
(( elapsed = now - before ))
|
||||||
if (( elapsed >= $SECSBETWEENRAINEVENT )) ; then
|
if (( $elapsed >= $RAINSENSORQTY_SECSBETWEENRAINEVENT )) ; then
|
||||||
counter=0
|
counter=0
|
||||||
log_write "sono passati $elapsed secondi ( > di $SECSBETWEENRAINEVENT ) dall'ultima precipitazione, reset counter"
|
drv_rainsensorqty_writelog $f "$elapsed seconds elapsed ( greater than $RAINSENSORQTY_SECSBETWEENRAINEVENT set ) since last rain, first $MMEACH mm rain"
|
||||||
echo "sono passati $elapsed secondi ( > di $SECSBETWEENRAINEVENT ) dall'ultima precipitazione, reset counter"
|
echo "$elapsed seconds elapsed ( greater than $RAINSENSORQTY_SECSBETWEENRAINEVENT set ) since last rain, first $MMEACH mm rain (first loop)"
|
||||||
fi
|
fi
|
||||||
counter=$(( counter+=1 ))
|
counter=$(( counter+=1 ))
|
||||||
MMWATER=$( $JQ -n "$counter*$MMEACH" )
|
MMWATER=$( $JQ -n "$counter*$MMEACH" )
|
||||||
log_write "counter $counter - $MMWATER mm acqua"
|
drv_rainsensorqty_writelog $f "now is $MMWATER mm rain"
|
||||||
echo "counter $counter - $MMWATER mm acqua"
|
echo "now is $MMWATER mm rain (loop $counter)"
|
||||||
if (( counter >= $LOOPSFORSETRAINING )) ; then
|
if (( $counter >= $RAINSENSORQTY_LOOPSFORSETRAINING )) ; then
|
||||||
log_write "raggiunta acqua per impedire irrigazione: $MMWATER mm"
|
drv_rainsensorqty_writelog $f "$MMWATER mm - irrigation to be stopped"
|
||||||
echo "raggiunta acqua per impedire irrigazione: $MMWATER mm"
|
echo "$MMWATER mm - irrigation to be stopped (loop $counter)"
|
||||||
date +%s > ${RAINSENSORQTY_LASTRAIN}
|
date +%s > ${RAINSENSORQTY_LASTRAIN}
|
||||||
date > ${RAINSENSORQTY_LASTRAIN}_date
|
date > ${RAINSENSORQTY_LASTRAIN}_date
|
||||||
fi
|
fi
|
||||||
#gpio -g wfi $GPIO $RAINSENSORQTY_WAIT # rising 0->1
|
echo $GPIO -g wfi $gpio_port $RAINSENSORQTY_WAIT # rising 0->1
|
||||||
|
$GPIO -g wfi $gpio_port $RAINSENSORQTY_WAIT # rising 0->1
|
||||||
done
|
done
|
||||||
|
|||||||
23
drv/rainsensorqty/test_rainsensorqty_CHECK.sh
Executable file
23
drv/rainsensorqty/test_rainsensorqty_CHECK.sh
Executable file
@@ -0,0 +1,23 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
DIR_SCRIPT=/home/pi/piGarden
|
||||||
|
NAME_SCRIPT="not_needed"
|
||||||
|
CONFIG_ETC="/etc/piGarden.conf"
|
||||||
|
TMP_PATH="/run/shm"
|
||||||
|
if [ ! -d "$TMP_PATH" ]; then
|
||||||
|
TMP_PATH="/tmp"
|
||||||
|
fi
|
||||||
|
|
||||||
|
LOG_OUTPUT_DRV_FILE="$DIR_SCRIPT/log/LOG_OUTPUT_DRV_FILE"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
. $CONFIG_ETC
|
||||||
|
|
||||||
|
. ./common.include.sh
|
||||||
|
. ./config.include.sh
|
||||||
|
. ./init.include.sh
|
||||||
|
. ./rainsensor.include.sh
|
||||||
|
|
||||||
|
drv_rainsensorqty_rain_sensor_get
|
||||||
|
echo "drv_rainsensorqty_rain_sensor_get $?"
|
||||||
|
|
||||||
23
drv/rainsensorqty/test_rainsensorqty_INIT.sh
Executable file
23
drv/rainsensorqty/test_rainsensorqty_INIT.sh
Executable file
@@ -0,0 +1,23 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
DIR_SCRIPT=/home/pi/piGarden
|
||||||
|
NAME_SCRIPT="not_needed"
|
||||||
|
CONFIG_ETC="/etc/piGarden.conf"
|
||||||
|
TMP_PATH="/run/shm"
|
||||||
|
if [ ! -d "$TMP_PATH" ]; then
|
||||||
|
TMP_PATH="/tmp"
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
. $CONFIG_ETC
|
||||||
|
|
||||||
|
. ./config.include.sh
|
||||||
|
. ./common.include.sh
|
||||||
|
. ./init.include.sh
|
||||||
|
. ./rainsensor.include.sh
|
||||||
|
|
||||||
|
drv_rainsensorqty_rain_sensor_init
|
||||||
|
|
||||||
|
drv_rainsensorqty_init
|
||||||
|
|
||||||
|
drv_rainsensorqty_rain_sensor_get
|
||||||
|
echo "drv_rainsensorqty_rain_sensor_get $?"
|
||||||
Reference in New Issue
Block a user