OpenSprinkler Pi/Shell

From Rayshobby Wiki
Jump to navigationJump to search

Shell program to control valves from cron

This is a very simple program to shift a specified value into the valve control bits on the OpenSprinkler Pi. It should run under little shells like dash and ash but I've only tested it with /bin/bash, the GNU Borne-Again Shell that's the default user shell on Raspbian.

It's designed to be run by cron. As root, use your favorite text editor to edit root's crontab:

sudo crontab -e

and add lines such as:

12 1 * * *  /usr/local/sbin/osppi -v2  1 1 1 1  1 1 1 1  # On at 1:12 am daily
42 1 * * *  /usr/local/sbin/osppi -v2  0 0 0 0  0 0 0 0  # Off at 1:42 am daily
31 15 * * * /usr/local/sbin/osppi -v2  0 0 1 0  0 0 0 0  # station 6 on at 3:31 pm daily
31 16 * * * /usr/local/sbin/osppi -v2  0 0 0 0  0 0 0 0  # station 6 off an hour later

Of course you'll have to copy the file to /usr/local/sbin and make it executable. Here, "you" already downloaded it to your desktop with the browser:

sudo cp ~you/Desktop/osppi /usr/local/sbin
sudo chmod 755 /usr/local/sbin/osppi

I'm sure the Python is good but I wanted something with no dependencies besides default Raspbian.

#/bin/bash
:
#   osppi  sprinklers on and off
#     for the OpenSprinkler-Pi board and R-Pi
:
gpiodir=/sys/class/gpio
gpios="4 17 22 27"
DAT=gpio27     # default, R-Pi v2.
:
:
usage="
$0 [-v1|-v2] ch8 ch7 ch6 ch5 ch4 ch3 ch2 ch1
:
where chN is 1 (on) or 0 (off) for each station.
Use -v1 or -v2 for R-Pi version 1 or 2."
die(){
       echo $*
       echo "$usage"
       exit 1
}
:
#  sanity check the input arguments
[ -z "$1" ] && die command parameters all missing!
:
case $1 in
"-v1") DAT=gpio21; shift;;
"-v2") DAT=gpio27; shift;;
*)     :;;
esac
for arg in $*
do
       case "$arg" in
       "0"|"1")        : ok;;
       *)              die "$arg" isn\'t a 1 or a 0;;
       esac
done
:
#  Functions first, then the program.
:
#  Set the control pins up.
reset_ospi ()
{
       for pin in $gpios
       do
               #  if needed...
               if [ ! -e $gpiodir/gpio$pin ]; then
               #       ... make the pin appear in /sys/class/gpio
                       echo $pin > $gpiodir/export
               fi
       done 
       for pin in $gpios
       do
               #  Drive the pin to ground
               echo out > $gpiodir/gpio$pin/direction
               echo 0   > $gpiodir/gpio$pin/value
       done
}
:
#  Receive a new bit into the shift register.
sclock ()
{
       echo 0 > $gpiodir/gpio4/value
       echo 1 > $gpiodir/gpio4/value
       echo 0 > $gpiodir/gpio4/value
}
# clock the shift register contents into the output buffer.
bclock (){
       echo 0 > $gpiodir/gpio22/value
       echo 1 > $gpiodir/gpio22/value
       echo 0 > $gpiodir/gpio22/value
}
:
:
#  Program starts
:
reset_ospi
for value in $*
do
       #  set up a data bit and clock it in
       echo $value > $gpiodir/$DAT/value
       sclock
done
bclock
exit 0
:
#  That is all.