Tomat-O-Mat: Difference between revisions

From Wurst-Wasser.net
Jump to navigation Jump to search
Line 4: Line 4:
=== How It Works ===
=== How It Works ===
A [[RaspberryPi]] is connected to a Sony Playstation Camera, a DHT11, some level switches, a relay and a pump.
A [[RaspberryPi]] is connected to a Sony Playstation Camera, a DHT11, some level switches, a relay and a pump.
[[Image:TomatOMatSchematics.png|800px]]
[[Image:TomatOMatSchematics.png|800px]]<br>
==== Parts list ====
* [[RaspberryPi]] Rev 2
* Sony Playstation Camera
* [[DHT11]]
* [[DS18B20]]
* Pump (model to be done)
* Relay (model to be done)
* some cables, wires, boards, and the usual soldering equipment


=== Sources ===
=== Sources ===

Revision as of 08:13, 30 June 2013

The Idea

Since I'm too lazy[1] to water my tomatoes, I thought it would be nice if a Pi would take care of that.

How It Works

A RaspberryPi is connected to a Sony Playstation Camera, a DHT11, some level switches, a relay and a pump. TomatOMatSchematics.png

Parts list

  • RaspberryPi Rev 2
  • Sony Playstation Camera
  • DHT11
  • DS18B20
  • Pump (model to be done)
  • Relay (model to be done)
  • some cables, wires, boards, and the usual soldering equipment

Sources

00_check_and_init.sh

#!/bin/bash
#set -x

# File: 00_check_and_init.sh
# Author: Heiko Kretschmer, watering-o-matic@wurst-wasser.net
# Purpose: Checking wether the gpio are set up, and if not, setting 'em up.

# Globals
GDIRBASE="/home/pi/Development/giess-o-mat"

# UnMain
"${GDIRBASE}/"01_check_init.sh || sudo "${GDIRBASE}/"02_init.sh

# EOF

01_check_init.sh

#!/bin/bash
#set -x

# Author: Heiko Kretschmer, watering-o-matic@wurst-wasser.net
# Purpose: Checking wether GPIO pins are set up

#############
# Check GPIOs
#############

if [ ! -f "/sys/class/gpio/gpio17/value" ]; then
  echo "control file for gpio17 is missing, need to call 02_init.sh!"
  exit 1
fi

if [ ! -f "/sys/class/gpio/gpio22/value" ]; then
  echo "control file for gpio22 is missing, need to call 02_init.sh!"
  exit 1
fi

if [ ! -f "/sys/class/gpio/gpio24/value" ]; then
  echo "control file for gpio24 is missing, need to call 02_init.sh!"
  exit 1
fi

if [ ! -f "/sys/class/gpio/gpio25/value" ]; then
  echo "control file for gpio25 is missing, need to call 02_init.sh!"
  exit 1
fi

exit 0

# EOF

02_init.sh

#!/bin/bash
#set -x

# File: 02_init.sh
# Author: Heiko Kretschmer, watering-o-matic@wurst-wasser.net
# Purpose: Initiating GPIO pins for in-/output
# Important note: MUST be run as root at boot-time - the "device"-files disappear on reboot!

# GPIO numbers should be from this list
# 0, 1, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 21, 22, 23, 24, 25

# Note that the GPIO numbers that you program here refer to the pins
# of the BCM2835 and *not* the numbers on the pin header. 
# So, if you want to activate Pin 07 on the header you should be 
# using GPIO4 in this script. Likewise if you want to activate Pin 11
# on the header you should be using GPIO17 here.

# Check permissions (Actually, just export as root, then set permissions for device files, done)
test "`whoami`" != "root" && echo "Must be root (or use sudo)." && exit 1

# The Pins we use, and how: (Details man gpio)
#
# GPIO 17 (Pin 11) - Output (c), delivering signal for the pump
# GPIO 25 (Pin 22) - Input (g), reading signal for upper water level measurement of reservoir
# GPIO 22 (Pin 15) - Input (e), reading signal for lower water level measurement of reservoir
# GPIO 24 (Pin 18) - Input (a), reading signal for lower water level measurement of flowerbed

echo "Initializing GPIOs..."

#############
# Setup GPIOs
#############

# Set up GPIO 17 (Pin 11) and set to output (Pump starts if 0)
echo 17 > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio17/direction
chown pi /sys/class/gpio/gpio17/value

# Set up GPIO 25 (Pin 22) and set to input (should read 0, if water over mark)
NR="25"
echo "${NR}" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio${NR}/direction

# Set up GPIO 22 (Pin 15) and set to input (should read 0, if water over mark)
NR="22"
echo "${NR}" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio${NR}/direction

# Set up GPIO 24 (Pin 18) and set to input (should read 0, if water over mark)
NR="24"
echo "${NR}" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio${NR}/direction

############
# Init GPIOs
############

# Set output to default (disabling pump and putting safety on, since the relay triggers on low signal)
echo "1" > /sys/class/gpio/gpio17/value # Pin 11 -> High, Pump disabled

# Clean up
#echo "17" > /sys/class/gpio/unexport
#echo "21" > /sys/class/gpio/unexport
#echo "22" > /sys/class/gpio/unexport
#echo "23" > /sys/class/gpio/unexport
#echo "24" > /sys/class/gpio/unexport

# EOF

04_check_uninit.sh

#!/bin/bash
#set -x

# Author: Heiko Kretschmer, watering-o-matic@wurst-wasser.net
# Purpose: Checking wether GPIO pins are set up

#############
# Check GPIOs
#############

if [ ! -f "/sys/class/gpio/gpio17/value" -a ! -f "/sys/class/gpio/gpio25/value" -a ! -f "/sys/class/gpio/gpio22/value" -a ! -f "/sys/class/gpio/gpio24/value" ]; then
 exit 0 # All GPIOs are gone
fi

exit 1 # One ore more GPIOs are still there

# EOF

09_uninit.sh

#!/bin/bash
#set -x

# File: 09_uninit.sh
# Author: Heiko Kretschmer, watering-o-matic@wurst-wasser.net
# Purpose: Un-Initiating GPIO pins for in-/output
# Important note: MUST be run as root!

# Globals
GDIRBASE="/home/pi/Development/giess-o-mat"

# UnMain
"${GDIRBASE}/"04_check_uninit.sh && echo "We already did uninit. Exiting." && exit 1

# Check permissions (Actually, just export as root, then set permissions for device files, done)
test "`whoami`" != "root" && echo "Must be root (or use sudo)." && exit 1

# Set output to default (disabling pump and putting safety on, since the relay triggers on low signal)
echo "1" > /sys/class/gpio/gpio17/value # Pin 11 -> High

# Clean up (Levels on GPIO output pins remain unchanged)
echo "17" > /sys/class/gpio/unexport
echo "25" > /sys/class/gpio/unexport
echo "22" > /sys/class/gpio/unexport
echo "24" > /sys/class/gpio/unexport

# EOF

10_check_level.sh

#!/bin/bash
#set -x

# File: 10_check_level.sh
# Author: Heiko Kretschmer, watering-o-matic@wurst-wasser.net
# Purpose: Reading GPIO pins to get current water level

# Globals
GDIRBASE="/home/pi/Development/giess-o-mat"

# Check if we already set up the GPIOs
"${GDIRBASE}/"00_check_and_init.sh

# Read from level-input (lower and upper water mark of reservoir)
RESERVOIRUPPERWATERMARKVALUE="`cat /sys/class/gpio/gpio25/value`"
RESERVOIRLOWERWATERMARKVALUE="`cat /sys/class/gpio/gpio22/value`"

# Read from level-input (lower water mark of flowerbed)
FLOWERBEDLOWERWATERMARKVALUE="`cat /sys/class/gpio/gpio24/value`"

# Say what's up!
echo ""
echo "Reservoir water level:"
echo "----------------"
echo "RESERVOIRUPPERWATERMARKVALUE: ${RESERVOIRUPPERWATERMARKVALUE} (0=water higher, 1=water lower than watermark)"
echo "RESERVOIRLOWERWATERMARKVALUE: ${RESERVOIRLOWERWATERMARKVALUE} (0=water higher, 1=water lower than watermark)"
echo ""
echo "Flowerbed water level:"
echo "----------------------"
echo "FLOWERBEDLOWERWATERMARKVALUE: ${FLOWERBEDLOWERWATERMARKVALUE} (0=water higher, 1=water lower than watermark)"
echo ""

# Reset pump
echo "1" > /sys/class/gpio/gpio17/value

# EOF

15_pump_10s.sh

#!/bin/bash
#set -x

# Script: 15_pump_10s.sh
# Author: Heiko Kretschmer, watering-o-matic@wurst-wasser.net
# Purpose: Set GPIO pins to start pump

# Globals
GDIRBASE="/home/pi/Development/giess-o-mat"

# Check if we already set up the GPIOs
"${GDIRBASE}/"00_check_and_init.sh

# Check permissions (Actually, just export as root, then set permissions for device files, done)
#test "`whoami`" != "root" && echo "Must be root (or use sudo)." && exit 1

# Start pump
echo "0" > /sys/class/gpio/gpio17/value # start pump

# Run for 10 Seconds
sleep 10

# Stop Pump
echo "1" > /sys/class/gpio/gpio17/value # stop pump

MORE LATER

Some thoughts

FAQ

Choice of programming language
  • Q: Why don't you use python, C or any other higher language?
  • A: I wanted to KISS. Shell is easy to code, easy to maintain, easy to learn, and easy to automate[2]
GPIO protection
  • Q: Why do you connect the GPIOs directly? You should use something like GertBoard to protect your Pi!
  • A: You are so right. I see this as PoC, next time I will do better, I promise. Next winter comes[TM].

  1. ...and I tend to forget...
  2. Okay, if you need more exact timing, shell is not the environment of choice, but in this case it fits perfectly.