7 Commits
v0.2 ... v0.3.0

3 changed files with 265 additions and 24 deletions

View File

@@ -1,3 +1,16 @@
## 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
Fix bug: if it's reining, the solenoid valves were also closed even if they were pushed open in "force" mode
## 0.2.1 - 22/04/2017
Add installation instructions in README.md file
## 0.2 (Easter egg) - 17/04/2017
Implementation of socket server for communicate with piGardenWeb
Implementation of messages (error, warning, success) passed to piGardenWeb

View File

@@ -1,4 +1,4 @@
## piGarden
# piGarden
Bash script to manage an irrigation system built with a Raspberry Pi
@@ -9,3 +9,62 @@ Documentation of piGarden and build system irrigation with Raspberry Pi can be f
## License
This script is open-sourced software under GNU GENERAL PUBLIC LICENSE Version 2
## Installation
1) Installs the necessary packages on your terminal:
``` bash
sudo apt-get install git curl gzip grep sed ucspi-tcp
```
2) Compile and install Jq (commandline JSON processor):
``` bash
cd
sudo apt-get install flex -y
sudo apt-get install bison -y
sudo apt-get install gcc -y
sudo apt-get install make -y
sudo apt-get install autotools -y
sudo apt-get install dh-autoreconf -y
wget https://github.com/stedolan/jq/releases/download/jq-1.5/jq-1.5.tar.gz
tar xfvz jq-1.5.tar.gz
cd jq-1.5
autoreconf -i
./configure --disable-maintainer-mode
make
sudo make install
```
3) Compile and install gpio program from WiringPi package:
``` bash
cd
git clone git://git.drogon.net/wiringPi
cd wiringPi
git pull origin
./build
```
4) Download and install piGarden in your home
``` bash
cd
git clone https://github.com/lejubila/piGarden.git
```
## Configuration
Copy configuration file in /etc
```bash
cd
sudo cp piGarden/conf/piGarden.conf.example /etc/piGarden.conf
```
Customize the configuration file.
For more information see
[www.lejubila.net/2015/12/impianto-di-irrigazione-con-raspberry-pi-pigarden-lo-script-di-gestione-quinta-parte/](https://www.lejubila.net/2015/12/impianto-di-irrigazione-con-raspberry-pi-pigarden-lo-script-di-gestione-quinta-parte/)
and
[www.lejubila.net/2017/04/pigarden-0-2-easter-egg/](https://www.lejubila.net/2017/04/pigarden-0-2-easter-egg/)

View File

@@ -62,6 +62,9 @@ function reset_messages {
# $2 se specificata la string "force" apre l'elettrovalvola anche se c'é pioggia
#
function ev_open {
cron_del open_in $1 > /dev/null 2>&1
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`
@@ -89,10 +92,14 @@ function ev_open {
fi
fi
local state=1
if [ "$2" = "force" ]; then
state=2
fi
log_write "Solenoid '$1' open"
message_write "success" "Solenoid open"
supply_positive
#gpio_alias2number $1
ev_alias2number $1
EVNUM=$?
ev_number2gpio $EVNUM
@@ -100,9 +107,71 @@ function ev_open {
$GPIO -g write $g $RELE_GPIO_CLOSE
sleep 1
$GPIO -g write $g $RELE_GPIO_OPEN
ev_set_state $EVNUM 1
ev_set_state $EVNUM $state
}
#
# 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
# $1 alias elettrovalvola
@@ -120,6 +189,8 @@ function ev_close {
sleep 1
$GPIO -g write $g $RELE_GPIO_OPEN
ev_set_state $EVNUM 0
cron_del open_in_stop $1 > /dev/null 2>&1
}
#
@@ -207,7 +278,7 @@ function gpio_alias2number {
done
log_write "ERROR solenoid alias not found: $1"
message_write "error" "Solenoid alias not found"
message_write "warning" "Solenoid alias not found"
exit 1
}
@@ -226,7 +297,7 @@ function ev_alias2number {
done
log_write "ERROR solenoid alias not found: $1"
message_write "error" "Solenoid alias not found"
message_write "warning" "Solenoid alias not found"
exit 1
}
@@ -292,10 +363,10 @@ function ev_status {
#
function check_rain_online {
# 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
local weather=`cat /tmp/check_rain_online.json | $JQ -M ".current_observation.weather"`
local current_observation=`cat /tmp/check_rain_online.json | $JQ -M ".current_observation"`
local local_epoch=`cat /tmp/check_rain_online.json | $JQ -M -r ".current_observation.local_epoch"`
$CURL http://api.wunderground.com/api/$WUNDERGROUND_KEY/conditions/q/$WUNDERGROUND_LOCATION.json > $TMP_PATH/check_rain_online.json
local weather=`cat $TMP_PATH/check_rain_online.json | $JQ -M ".current_observation.weather"`
local current_observation=`cat $TMP_PATH/check_rain_online.json | $JQ -M ".current_observation"`
local local_epoch=`cat $TMP_PATH/check_rain_online.json | $JQ -M -r ".current_observation.local_epoch"`
#echo $weather
#weather="[Light/Heavy] Drizzle"
if [ "$weather" = "null" ]; then
@@ -393,7 +464,7 @@ function close_all {
ev_status $al
local state=$?
#echo "$al = $state"
if [[ "$state" = "1" || "$1" = "force" ]]; then
if [[ "$state" -gt "0" || "$1" = "force" ]]; then
ev_close $al
log_write "close_all - Close solenoid '$al' for rain"
fi
@@ -426,6 +497,7 @@ function json_status {
local last_warning=""
local last_success=""
local with_get_cron="0"
local with_get_cron_open_in="0"
local vret=""
for i in $1 $2 $3 $4 $5 $6
@@ -433,6 +505,9 @@ function json_status {
if [ $i = "get_cron" ]; then
with_get_cron="1"
fi
if [ $i = "get_cron_open_in" ]; then
with_get_cron_open_in="1"
fi
done
for i in $(seq $EV_TOTAL)
@@ -497,7 +572,33 @@ function json_status {
fi
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"
@@ -547,7 +648,7 @@ function cron_del {
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"
rm "$TMP_CRON_FILE"
@@ -562,6 +663,7 @@ function cron_del {
# $5 mese
# $6 giorno della settimana
# $7 argomento della tipologia
# $8 secondo argomento della tipologia
#
function cron_add {
@@ -572,6 +674,7 @@ function cron_add {
local CRON_MON=$5
local CRON_DOW=$6
local CRON_ARG=$7
local CRON_ARG2=$8
local CRON_COMMAND=""
local PATH_SCRIPT=`$READLINK -f "$DIR_SCRIPT/$NAME_SCRIPT"`
local TMP_CRON_FILE2="$TMP_CRON_FILE-2"
@@ -667,6 +770,14 @@ function cron_add {
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)
CRON_COMMAND="$PATH_SCRIPT close $CRON_ARG"
;;
@@ -870,6 +981,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
# $1 alias elettrovalvola
@@ -929,14 +1058,17 @@ function del_cron_close {
function show_usage {
echo -e "piGarden v. $VERSION.$SUB_VERSION.$RELEASE_VERSION"
echo -e ""
echo -e "Usage:"
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_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 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_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_sensor check rain from hardware sensor"
echo -e "\t$NAME_SCRIPT close_all_for_rain close all solenoid if it's raining"
@@ -958,6 +1090,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 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 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 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"
@@ -1028,16 +1161,21 @@ function socket_server_command {
json_error 0 "Alias solenoid not specified"
else
ev_open $arg2 $arg3 &> /dev/null
json_status
json_status "get_cron_open_in"
fi
;;
open_in)
ev_open_in $arg2 $arg3 $arg4 $arg5 &> /dev/null
json_status "get_cron_open_in"
;;
close)
if [ "empty$arg2" == "empty" ]; then
json_error 0 "Alias solenoid not specified"
else
ev_close $arg2 &> /dev/null
json_status
json_status "get_cron_open_in"
fi
;;
@@ -1075,7 +1213,7 @@ function socket_server_command {
if [[ ! -z $vret ]]; then
json_error 0 "Cron set failed"
log_write "Cron set failed: $vret"
log_write "Cron del failed: $vret"
else
message_write "success" "Cron set successfull"
json_status
@@ -1083,6 +1221,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)
local vret=""
@@ -1164,16 +1318,20 @@ function debug2 {
}
VERSION=0
SUB_VERSION=2
SUB_VERSION=3
RELEASE_VERSION=0
DIR_SCRIPT=`dirname $0`
NAME_SCRIPT=${0##*/}
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=$$
RUN_FROM_TCPSERVER=0
TMP_CRON_FILE="/tmp/pigarden.user.cron.$$"
TMP_CRON_FILE="$TMP_PATH/pigarden.user.cron.$$"
if [ -f $CONFIG_ETC ]; then
. $CONFIG_ETC
@@ -1194,10 +1352,15 @@ case "$1" in
open)
if [ "empty$2" == "empty" ]; then
echo -e "Alias solenoid not specified"
exit 1
fi
ev_open $2 $3
;;
open_in)
ev_open_in $2 $3 $4 $5
;;
close)
if [ "empty$2" == "empty" ]; then
echo -e "Alias solenoid not specified"
@@ -1315,6 +1478,10 @@ case "$1" in
del_cron_open $2
;;
del_cron_open_in)
del_cron_open_in $2
;;
get_cron_open)
get_cron_open $2
;;
@@ -1346,5 +1513,7 @@ case "$1" in
;;
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